Skip to main content

CharGrid

Struct CharGrid 

Source
pub struct CharGrid { /* private fields */ }
Expand description

字符网格的数据:行 × 列的格子、插入点、选区。是一个句柄,clone 出来的是同一个网格; 用 char_grid 显示。写的时候记下哪几行变了,下一帧只重新生成这几行的图元。

只能在主线程用(和信号一样)。

一格放一个字素簇:带变音符号的字、组合的 emoji(ZWJ、肤色、国旗、键帽、VS16)都在一格或两格里,不拆开。 占几格按 unicode-width 对整个簇的算法,最多两格:普通的字一格,汉字、emoji 两格,变音符号不加宽度。写到一半宽字上时, 被拆开的另一半变成空格;最后一列放不下两格的簇时写空格。

组合字符接到前一格上:Self::set、Self::write 写的开头如果和前一格的簇连得成一个簇(变音符号、ZWJ 和它后面的字、 第二个地区指示符),接上去,样式还是前一格的。所以逐字符写和一次写结果一样。接不上的零宽字符丢掉,控制字符也丢掉

Implementations§

Source§

impl CharGrid

Source

pub fn new(cols: usize, rows: usize) -> CharGrid

cols 列、rows 行的空网格,插入点在左上角

Source

pub fn size(&self) -> [usize; 2]

[列数, 行数]

Source

pub fn set( &self, row: usize, col: usize, ch: char, style: CharCellStyle, ) -> usize

第 row 行第 col 列写一个字,返回写完后的列;宽字占这一格和下一格(最后一列放不下宽字时写空格)。越界的忽略。 组合字符(变音符号、ZWJ 和它后面的字、肤色……)接到前一格的簇上,这时 style 不起作用:

let grid = CharGrid::new(10, 1);
let mut col = 0;
for ch in "e\u{301}x".chars() {
    col = grid.set(0, col, ch, CharCellStyle::default()); // 终端逐字符写
}
assert_eq!(col, 2);
assert_eq!(grid.row_text(0), "e\u{301}x");
Source

pub fn write( &self, row: usize, col: usize, text: &str, style: CharCellStyle, ) -> usize

从第 row 行第 col 列开始写一串字(不换行,超出行尾的截掉),按字素簇切开,返回写完后的列

Source

pub fn set_cells( &self, row: usize, cols: Range<usize>, ch: char, style: CharCellStyle, )

把第 row 行从 cols 范围里的格子填成同一个字(' ' 就是清掉)

Source

pub fn clear_row(&self, row: usize, style: CharCellStyle)

清空一行(空格,样式是 style:可以带底色)

Source

pub fn clear(&self)

清空整个网格

Source

pub fn scroll_up(&self, n: usize)

整体上移 n 行(终端滚屏):顶上的 n 行移出去,底下补 n 行空行。只挪行,没变的行不用重新生成图元; 选区跟着上移,移出去的部分去掉

Source

pub fn resize(&self, cols: usize, rows: usize)

改成 cols 列、rows 行:多出来的格子是空的,少了的截掉;元素下一帧重新布局

Source

pub fn cell(&self, row: usize, col: usize) -> Option<CharCell>

一格的内容;越界时是 None

Source

pub fn row_text(&self, row: usize) -> String

一行的文字(宽字只算一次,去掉行尾的空格)

Source

pub fn caret(&self) -> CharGridCaret

插入点

Source

pub fn set_caret(&self, caret: CharGridCaret)

放插入点(闪烁的插入点移动之后先亮着)

Source

pub fn selection(&self) -> Option<(CharGridPosition, CharGridPosition)>

选区:从 start 到 end(按阅读顺序,end 不含)。也可以用指针在网格上拖出来,⌘C 复制。 画和复制时都按整个簇:一端落在宽字中间时扩到整个字

Source

pub fn set_selection( &self, selection: Option<(CharGridPosition, CharGridPosition)>, )

设置选区(None 清掉)

Source

pub fn selected_text(&self) -> Option<String>

选中的文字:行与行之间换行,每行去掉行尾的空格

Source

pub fn stats(&self) -> CharGridStats

统计:一共重新生成过多少行的图元、整形过多少段字

Trait Implementations§

Source§

impl Clone for CharGrid

Source§

fn clone(&self) -> CharGrid

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more