2017-08-24 14:41:20 +00:00
|
|
|
use std::ops::{Index, IndexMut};
|
|
|
|
|
2017-09-04 15:32:12 +00:00
|
|
|
use color;
|
2017-08-23 09:45:56 +00:00
|
|
|
use super::cell::Cell;
|
2017-09-06 15:31:13 +00:00
|
|
|
use super::item::Item;
|
2017-08-26 20:17:09 +00:00
|
|
|
use sys::pango as sys_pango;
|
2017-08-24 14:41:20 +00:00
|
|
|
use pango;
|
|
|
|
|
2017-08-23 09:45:56 +00:00
|
|
|
pub struct Line {
|
2017-08-24 14:41:20 +00:00
|
|
|
pub line: Box<[Cell]>,
|
2017-08-25 15:32:30 +00:00
|
|
|
|
|
|
|
// format of item line is
|
|
|
|
// [Item1, Item2, None, None, Item3]
|
|
|
|
// Item2 take 3 cells and renders as one
|
|
|
|
pub item_line: Box<[Option<Item>]>,
|
2017-09-06 15:31:13 +00:00
|
|
|
cell_to_item: Box<[i32]>,
|
2017-08-25 15:32:30 +00:00
|
|
|
|
|
|
|
pub dirty_line: bool,
|
2017-08-23 09:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Line {
|
|
|
|
pub fn new(columns: usize) -> Self {
|
2017-08-24 14:41:20 +00:00
|
|
|
Line {
|
2017-09-09 19:44:28 +00:00
|
|
|
line: vec![Cell::new(' '); columns].into_boxed_slice(),
|
|
|
|
item_line: vec![None; columns].into_boxed_slice(),
|
2017-08-27 19:29:43 +00:00
|
|
|
cell_to_item: vec![-1; columns].into_boxed_slice(),
|
2017-08-31 15:37:55 +00:00
|
|
|
dirty_line: true,
|
2017-08-23 09:45:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-24 14:41:20 +00:00
|
|
|
|
2017-09-01 10:14:16 +00:00
|
|
|
pub fn copy_to(&self, target: &mut Self, left: usize, right: usize) {
|
|
|
|
target.line[left..right + 1].clone_from_slice(&self.line[left..right + 1]);
|
2017-09-04 15:32:12 +00:00
|
|
|
target.item_line[left..right + 1].clone_from_slice(&self.item_line[left..right + 1]);
|
|
|
|
target.cell_to_item[left..right + 1].copy_from_slice(&self.cell_to_item[left..right + 1]);
|
|
|
|
target.dirty_line = self.dirty_line;
|
2017-09-01 10:14:16 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 14:41:20 +00:00
|
|
|
pub fn clear(&mut self, left: usize, right: usize) {
|
|
|
|
for cell in &mut self.line[left..right + 1] {
|
|
|
|
cell.clear();
|
|
|
|
}
|
2017-09-09 19:44:28 +00:00
|
|
|
for item in &mut self.item_line[left..right + 1] {
|
|
|
|
item.clone_from(&None);
|
|
|
|
}
|
|
|
|
for i in left..right + 1 {
|
|
|
|
self.cell_to_item[i] = -1;
|
|
|
|
}
|
2017-08-31 15:37:55 +00:00
|
|
|
self.dirty_line = true;
|
2017-08-25 15:32:30 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 14:48:10 +00:00
|
|
|
pub fn clear_glyphs(&mut self) {
|
2017-09-01 13:49:10 +00:00
|
|
|
for i in 0..self.item_line.len() {
|
|
|
|
self.item_line[i] = None;
|
|
|
|
self.cell_to_item[i] = -1;
|
|
|
|
}
|
|
|
|
self.dirty_line = true;
|
|
|
|
}
|
|
|
|
|
2017-08-31 15:37:55 +00:00
|
|
|
fn set_cell_to_empty(&mut self, cell_idx: usize) -> bool {
|
2017-09-07 15:01:04 +00:00
|
|
|
if self.is_binded_to_item(cell_idx) {
|
2017-08-31 15:37:55 +00:00
|
|
|
self.item_line[cell_idx] = None;
|
|
|
|
self.cell_to_item[cell_idx] = -1;
|
|
|
|
self.line[cell_idx].dirty = true;
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 10:14:16 +00:00
|
|
|
fn set_cell_to_item(&mut self, new_item: &PangoItemPosition) -> bool {
|
|
|
|
let start_item_idx = self.cell_to_item(new_item.start_cell);
|
2017-09-12 09:56:40 +00:00
|
|
|
let start_item_cells_count = if start_item_idx >= 0 {
|
2017-11-13 09:51:22 +00:00
|
|
|
self.item_line[start_item_idx as usize].as_ref().map_or(
|
|
|
|
-1,
|
|
|
|
|item| {
|
|
|
|
item.cells_count as i32
|
|
|
|
},
|
|
|
|
)
|
2017-09-01 10:14:16 +00:00
|
|
|
} else {
|
|
|
|
-1
|
|
|
|
};
|
|
|
|
|
|
|
|
let end_item_idx = self.cell_to_item(new_item.end_cell);
|
2017-08-31 15:37:55 +00:00
|
|
|
|
2017-09-01 10:14:16 +00:00
|
|
|
// start_item == idx of item start cell
|
2017-08-31 15:37:55 +00:00
|
|
|
// in case different item length was in previous iteration
|
|
|
|
// mark all item as dirty
|
2017-09-01 10:14:16 +00:00
|
|
|
if start_item_idx != new_item.start_cell as i32 ||
|
2017-11-13 09:51:22 +00:00
|
|
|
new_item.cells_count() != start_item_cells_count ||
|
|
|
|
start_item_idx == -1 || end_item_idx == -1
|
2017-09-01 10:14:16 +00:00
|
|
|
{
|
|
|
|
self.initialize_cell_item(new_item.start_cell, new_item.end_cell, new_item.item);
|
2017-08-31 15:37:55 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
// update only if cell marked as dirty
|
2017-09-01 10:14:16 +00:00
|
|
|
if self.line[new_item.start_cell..new_item.end_cell + 1]
|
2017-08-31 15:37:55 +00:00
|
|
|
.iter()
|
2017-09-11 15:31:15 +00:00
|
|
|
.any(|c| c.dirty)
|
2017-08-31 15:37:55 +00:00
|
|
|
{
|
2017-09-01 10:14:16 +00:00
|
|
|
self.item_line[new_item.start_cell]
|
2017-08-31 15:37:55 +00:00
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
2017-09-01 10:14:16 +00:00
|
|
|
.update(new_item.item.clone());
|
|
|
|
self.line[new_item.start_cell].dirty = true;
|
2017-08-31 15:37:55 +00:00
|
|
|
true
|
2017-08-25 15:32:30 +00:00
|
|
|
} else {
|
2017-08-31 15:37:55 +00:00
|
|
|
false
|
2017-08-25 15:32:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-31 15:37:55 +00:00
|
|
|
}
|
2017-08-25 15:32:30 +00:00
|
|
|
|
2017-08-31 15:37:55 +00:00
|
|
|
pub fn merge(&mut self, old_items: &StyledLine, pango_items: &[sys_pango::Item]) {
|
|
|
|
let mut pango_item_iter = pango_items.iter().map(|item| {
|
|
|
|
PangoItemPosition::new(old_items, item)
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut next_item = pango_item_iter.next();
|
|
|
|
let mut move_to_next_item = false;
|
|
|
|
|
|
|
|
let mut cell_idx = 0;
|
|
|
|
while cell_idx < self.line.len() {
|
|
|
|
let dirty = match next_item {
|
|
|
|
None => self.set_cell_to_empty(cell_idx),
|
2017-09-04 09:20:03 +00:00
|
|
|
Some(ref new_item) => {
|
|
|
|
if cell_idx < new_item.start_cell {
|
2017-08-31 15:37:55 +00:00
|
|
|
self.set_cell_to_empty(cell_idx)
|
2017-09-04 09:20:03 +00:00
|
|
|
} else if cell_idx == new_item.start_cell {
|
2017-08-31 15:37:55 +00:00
|
|
|
move_to_next_item = true;
|
2017-09-04 09:20:03 +00:00
|
|
|
self.set_cell_to_item(new_item)
|
2017-08-31 15:37:55 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.dirty_line = self.dirty_line || dirty;
|
|
|
|
if move_to_next_item {
|
2017-09-04 09:20:03 +00:00
|
|
|
let new_item = next_item.unwrap();
|
|
|
|
cell_idx += new_item.end_cell - new_item.start_cell + 1;
|
2017-08-31 15:37:55 +00:00
|
|
|
next_item = pango_item_iter.next();
|
|
|
|
move_to_next_item = false;
|
|
|
|
} else {
|
|
|
|
cell_idx += 1;
|
|
|
|
}
|
|
|
|
}
|
2017-08-25 15:32:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-27 19:29:43 +00:00
|
|
|
fn initialize_cell_item(
|
|
|
|
&mut self,
|
|
|
|
start_cell: usize,
|
|
|
|
end_cell: usize,
|
|
|
|
new_item: &sys_pango::Item,
|
|
|
|
) {
|
|
|
|
for i in start_cell..end_cell + 1 {
|
2017-08-25 15:32:30 +00:00
|
|
|
self.line[i].dirty = true;
|
2017-08-27 19:29:43 +00:00
|
|
|
self.cell_to_item[i] = start_cell as i32;
|
2017-08-25 15:32:30 +00:00
|
|
|
}
|
2017-09-04 09:47:23 +00:00
|
|
|
for i in start_cell + 1..end_cell + 1 {
|
2017-08-25 15:32:30 +00:00
|
|
|
self.item_line[i] = None;
|
|
|
|
}
|
2017-09-08 15:26:16 +00:00
|
|
|
self.item_line[start_cell] = Some(Item::new(new_item.clone(), end_cell - start_cell + 1));
|
2017-08-25 15:32:30 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 14:48:10 +00:00
|
|
|
pub fn get_item(&self, cell_idx: usize) -> Option<&Item> {
|
|
|
|
let item_idx = self.cell_to_item(cell_idx);
|
|
|
|
if item_idx >= 0 {
|
|
|
|
self.item_line[item_idx as usize].as_ref()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-06 15:31:13 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn cell_to_item(&self, cell_idx: usize) -> i32 {
|
2017-08-27 19:29:43 +00:00
|
|
|
self.cell_to_item[cell_idx]
|
2017-08-24 14:41:20 +00:00
|
|
|
}
|
2017-09-06 14:18:30 +00:00
|
|
|
|
2017-09-11 19:00:26 +00:00
|
|
|
pub fn item_len_from_idx(&self, start_idx: usize) -> usize {
|
2017-11-13 09:51:22 +00:00
|
|
|
debug_assert!(
|
|
|
|
start_idx < self.line.len(),
|
|
|
|
"idx={}, len={}",
|
|
|
|
start_idx,
|
|
|
|
self.line.len()
|
|
|
|
);
|
2017-09-06 14:18:30 +00:00
|
|
|
|
2017-09-11 19:00:26 +00:00
|
|
|
let item_idx = self.cell_to_item(start_idx);
|
2017-09-06 14:18:30 +00:00
|
|
|
|
2017-09-11 19:00:26 +00:00
|
|
|
if item_idx >= 0 {
|
|
|
|
let item_idx = item_idx as usize;
|
|
|
|
self.item_line[item_idx].as_ref().unwrap().cells_count - (start_idx - item_idx)
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
}
|
2017-09-06 14:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn is_binded_to_item(&self, cell_idx: usize) -> bool {
|
|
|
|
self.cell_to_item[cell_idx] >= 0
|
|
|
|
}
|
2017-08-24 14:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Index<usize> for Line {
|
|
|
|
type Output = Cell;
|
|
|
|
|
|
|
|
fn index(&self, index: usize) -> &Cell {
|
|
|
|
&self.line[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IndexMut<usize> for Line {
|
|
|
|
fn index_mut(&mut self, index: usize) -> &mut Cell {
|
|
|
|
&mut self.line[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 15:37:55 +00:00
|
|
|
struct PangoItemPosition<'a> {
|
|
|
|
item: &'a sys_pango::Item,
|
|
|
|
start_cell: usize,
|
|
|
|
end_cell: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PangoItemPosition<'a> {
|
|
|
|
pub fn new(styled_line: &StyledLine, item: &'a sys_pango::Item) -> Self {
|
|
|
|
let (offset, length, _) = item.offset();
|
|
|
|
let start_cell = styled_line.cell_to_byte[offset];
|
|
|
|
let end_cell = styled_line.cell_to_byte[offset + length - 1];
|
|
|
|
|
|
|
|
PangoItemPosition {
|
|
|
|
item,
|
|
|
|
start_cell,
|
|
|
|
end_cell,
|
|
|
|
}
|
|
|
|
}
|
2017-09-12 09:56:40 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn cells_count(&self) -> i32 {
|
|
|
|
(self.end_cell - self.start_cell) as i32 + 1
|
|
|
|
}
|
2017-08-31 15:37:55 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 14:41:20 +00:00
|
|
|
pub struct StyledLine {
|
|
|
|
pub line_str: String,
|
|
|
|
cell_to_byte: Box<[usize]>,
|
|
|
|
pub attr_list: pango::AttrList,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StyledLine {
|
2017-09-04 15:32:12 +00:00
|
|
|
pub fn from(line: &Line, color_model: &color::ColorModel) -> Self {
|
2017-08-24 14:41:20 +00:00
|
|
|
let mut line_str = String::new();
|
|
|
|
let mut cell_to_byte = Vec::new();
|
|
|
|
let attr_list = pango::AttrList::new();
|
|
|
|
let mut byte_offset = 0;
|
2017-09-06 09:05:12 +00:00
|
|
|
let mut style_attr = StyleAttr::new();
|
2017-08-24 14:41:20 +00:00
|
|
|
|
|
|
|
for (cell_idx, cell) in line.line.iter().enumerate() {
|
2017-08-28 15:05:58 +00:00
|
|
|
if cell.attrs.double_width {
|
2017-08-24 14:41:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
line_str.push(cell.ch);
|
2017-08-26 16:53:37 +00:00
|
|
|
let len = line_str.len() - byte_offset;
|
2017-08-24 14:41:20 +00:00
|
|
|
|
2017-08-26 16:53:37 +00:00
|
|
|
for _ in 0..len {
|
2017-08-24 14:41:20 +00:00
|
|
|
cell_to_byte.push(cell_idx);
|
|
|
|
}
|
|
|
|
|
2017-09-06 09:05:12 +00:00
|
|
|
let next = style_attr.next(byte_offset, byte_offset + len, cell, color_model);
|
|
|
|
if let Some(next) = next {
|
|
|
|
style_attr.insert(&attr_list);
|
|
|
|
style_attr = next;
|
2017-08-28 15:05:58 +00:00
|
|
|
}
|
2017-08-24 14:41:20 +00:00
|
|
|
|
|
|
|
byte_offset += len;
|
|
|
|
}
|
|
|
|
|
2017-09-06 09:05:12 +00:00
|
|
|
style_attr.insert(&attr_list);
|
|
|
|
|
2017-08-24 14:41:20 +00:00
|
|
|
StyledLine {
|
|
|
|
line_str,
|
|
|
|
cell_to_byte: cell_to_byte.into_boxed_slice(),
|
|
|
|
attr_list,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-06 09:05:12 +00:00
|
|
|
struct StyleAttr<'c> {
|
|
|
|
italic: bool,
|
|
|
|
bold: bool,
|
|
|
|
foreground: Option<&'c color::Color>,
|
2017-09-06 14:18:30 +00:00
|
|
|
background: Option<&'c color::Color>,
|
2017-09-06 09:05:12 +00:00
|
|
|
empty: bool,
|
|
|
|
|
|
|
|
start_idx: usize,
|
|
|
|
end_idx: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'c> StyleAttr<'c> {
|
|
|
|
fn new() -> Self {
|
|
|
|
StyleAttr {
|
|
|
|
italic: false,
|
|
|
|
bold: false,
|
|
|
|
foreground: None,
|
2017-09-06 14:18:30 +00:00
|
|
|
background: None,
|
2017-09-06 09:05:12 +00:00
|
|
|
empty: true,
|
|
|
|
|
|
|
|
start_idx: 0,
|
|
|
|
end_idx: 0,
|
|
|
|
}
|
2017-08-24 14:41:20 +00:00
|
|
|
}
|
2017-09-05 14:03:20 +00:00
|
|
|
|
2017-09-06 09:05:12 +00:00
|
|
|
fn from(
|
|
|
|
start_idx: usize,
|
|
|
|
end_idx: usize,
|
|
|
|
cell: &'c Cell,
|
|
|
|
color_model: &'c color::ColorModel,
|
|
|
|
) -> Self {
|
|
|
|
StyleAttr {
|
|
|
|
italic: cell.attrs.italic,
|
|
|
|
bold: cell.attrs.bold,
|
|
|
|
foreground: color_model.cell_fg(cell),
|
2017-09-06 14:18:30 +00:00
|
|
|
background: color_model.cell_bg(cell),
|
2017-09-06 09:05:12 +00:00
|
|
|
empty: false,
|
|
|
|
|
|
|
|
start_idx,
|
|
|
|
end_idx,
|
|
|
|
}
|
2017-08-24 14:41:20 +00:00
|
|
|
}
|
2017-09-05 14:03:20 +00:00
|
|
|
|
2017-09-06 09:05:12 +00:00
|
|
|
fn next(
|
|
|
|
&mut self,
|
|
|
|
start_idx: usize,
|
|
|
|
end_idx: usize,
|
|
|
|
cell: &'c Cell,
|
|
|
|
color_model: &'c color::ColorModel,
|
|
|
|
) -> Option<StyleAttr<'c>> {
|
|
|
|
let style_attr = Self::from(start_idx, end_idx, cell, color_model);
|
|
|
|
|
|
|
|
if self != &style_attr {
|
|
|
|
Some(style_attr)
|
|
|
|
} else {
|
|
|
|
self.end_idx = end_idx;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert(&self, attr_list: &pango::AttrList) {
|
|
|
|
if self.empty {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.italic {
|
|
|
|
self.insert_attr(
|
|
|
|
attr_list,
|
|
|
|
pango::Attribute::new_style(pango::Style::Italic).unwrap(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.bold {
|
|
|
|
self.insert_attr(
|
|
|
|
attr_list,
|
|
|
|
pango::Attribute::new_weight(pango::Weight::Bold).unwrap(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(fg) = self.foreground {
|
|
|
|
let (r, g, b) = fg.to_u16();
|
|
|
|
self.insert_attr(
|
|
|
|
attr_list,
|
|
|
|
pango::Attribute::new_foreground(r, g, b).unwrap(),
|
|
|
|
);
|
|
|
|
}
|
2017-09-06 14:18:30 +00:00
|
|
|
|
|
|
|
if let Some(bg) = self.background {
|
|
|
|
let (r, g, b) = bg.to_u16();
|
|
|
|
self.insert_attr(
|
|
|
|
attr_list,
|
|
|
|
pango::Attribute::new_background(r, g, b).unwrap(),
|
|
|
|
);
|
|
|
|
}
|
2017-09-06 09:05:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn insert_attr(&self, attr_list: &pango::AttrList, mut attr: pango::Attribute) {
|
|
|
|
attr.set_start_index(self.start_idx as u32);
|
|
|
|
attr.set_end_index(self.end_idx as u32);
|
2017-09-05 14:03:20 +00:00
|
|
|
attr_list.insert(attr);
|
|
|
|
}
|
2017-08-23 09:45:56 +00:00
|
|
|
}
|
2017-08-26 16:53:37 +00:00
|
|
|
|
2017-09-06 09:05:12 +00:00
|
|
|
impl<'c> PartialEq for StyleAttr<'c> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.italic == other.italic && self.bold == other.bold &&
|
2017-09-06 14:18:30 +00:00
|
|
|
self.foreground == other.foreground && self.empty == other.empty &&
|
|
|
|
self.background == other.background
|
2017-09-06 09:05:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-26 16:53:37 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_styled_line() {
|
|
|
|
let mut line = Line::new(3);
|
|
|
|
line[0].ch = 'a';
|
|
|
|
line[1].ch = 'b';
|
|
|
|
line[2].ch = 'c';
|
|
|
|
|
2017-09-05 14:03:20 +00:00
|
|
|
let styled_line = StyledLine::from(&line, &color::ColorModel::new());
|
2017-08-26 16:53:37 +00:00
|
|
|
assert_eq!("abc", styled_line.line_str);
|
|
|
|
assert_eq!(3, styled_line.cell_to_byte.len());
|
|
|
|
assert_eq!(0, styled_line.cell_to_byte[0]);
|
|
|
|
assert_eq!(1, styled_line.cell_to_byte[1]);
|
|
|
|
assert_eq!(2, styled_line.cell_to_byte[2]);
|
|
|
|
}
|
|
|
|
}
|