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
impl CharGrid
Sourcepub fn set(
&self,
row: usize,
col: usize,
ch: char,
style: CharCellStyle,
) -> usize
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");Sourcepub fn write(
&self,
row: usize,
col: usize,
text: &str,
style: CharCellStyle,
) -> usize
pub fn write( &self, row: usize, col: usize, text: &str, style: CharCellStyle, ) -> usize
从第 row 行第 col 列开始写一串字(不换行,超出行尾的截掉),按字素簇切开,返回写完后的列
Sourcepub fn set_cells(
&self,
row: usize,
cols: Range<usize>,
ch: char,
style: CharCellStyle,
)
pub fn set_cells( &self, row: usize, cols: Range<usize>, ch: char, style: CharCellStyle, )
把第 row 行从 cols 范围里的格子填成同一个字(' ' 就是清掉)
Sourcepub fn clear_row(&self, row: usize, style: CharCellStyle)
pub fn clear_row(&self, row: usize, style: CharCellStyle)
清空一行(空格,样式是 style:可以带底色)
Sourcepub fn scroll_up(&self, n: usize)
pub fn scroll_up(&self, n: usize)
整体上移 n 行(终端滚屏):顶上的 n 行移出去,底下补 n 行空行。只挪行,没变的行不用重新生成图元;
选区跟着上移,移出去的部分去掉
Sourcepub fn caret(&self) -> CharGridCaret
pub fn caret(&self) -> CharGridCaret
插入点
Sourcepub fn set_caret(&self, caret: CharGridCaret)
pub fn set_caret(&self, caret: CharGridCaret)
放插入点(闪烁的插入点移动之后先亮着)
Sourcepub fn selection(&self) -> Option<(CharGridPosition, CharGridPosition)>
pub fn selection(&self) -> Option<(CharGridPosition, CharGridPosition)>
选区:从 start 到 end(按阅读顺序,end 不含)。也可以用指针在网格上拖出来,⌘C 复制。
画和复制时都按整个簇:一端落在宽字中间时扩到整个字
Sourcepub fn set_selection(
&self,
selection: Option<(CharGridPosition, CharGridPosition)>,
)
pub fn set_selection( &self, selection: Option<(CharGridPosition, CharGridPosition)>, )
设置选区(None 清掉)
Sourcepub fn selected_text(&self) -> Option<String>
pub fn selected_text(&self) -> Option<String>
选中的文字:行与行之间换行,每行去掉行尾的空格
Sourcepub fn stats(&self) -> CharGridStats
pub fn stats(&self) -> CharGridStats
统计:一共重新生成过多少行的图元、整形过多少段字
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for CharGrid
impl !Send for CharGrid
impl !Sync for CharGrid
impl !UnwindSafe for CharGrid
impl Forget for CharGrid
impl Freeze for CharGrid
impl Unpin for CharGrid
impl UnsafeUnpin for CharGrid
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.