papergrid/records/
cell_info.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! A [`Cell`] implementation for [`VecRecords`].
//!
//! [`VecRecords`]: crate::records::vec_records::VecRecords

use std::{borrow::Cow, cmp::max};

use crate::{
    records::vec_records::{Cell, CellMut},
    util::{count_lines, get_lines},
    width::WidthFunc,
};

/// The struct is a [Cell] implementation which keeps width information pre allocated.
#[derive(Debug, Default)]
pub struct CellInfo<'a> {
    text: Cow<'a, str>,
    width: usize,
    lines: Vec<StrWithWidth<'a>>,
    count_lines: usize,
}

impl<'a> CellInfo<'a> {
    /// Creates a new instance of the structure.
    pub fn new<S, W>(text: S, width_ctrl: W) -> Self
    where
        S: Into<Cow<'a, str>>,
        W: WidthFunc,
    {
        create_cell_info(text.into(), width_ctrl)
    }

    /// Checks if the containing string is empty.
    pub fn is_empty(&self) -> bool {
        self.text.is_empty()
    }
}

impl Cell for CellInfo<'_> {
    fn get_line(&self, i: usize) -> &str {
        if i == 0 && self.lines.is_empty() {
            return &self.text;
        }

        &self.lines[i].text
    }

    fn count_lines(&self) -> usize {
        self.count_lines
    }

    fn width<W>(&self, _: W) -> usize
    where
        W: WidthFunc,
    {
        self.width
    }

    fn line_width<W>(&self, i: usize, _: W) -> usize
    where
        W: WidthFunc,
    {
        if i == 0 && self.lines.is_empty() {
            return self.width;
        }

        self.lines[i].width
    }
}

impl<'a, T> CellMut<T> for CellInfo<'a>
where
    T: Into<Cow<'a, str>>,
{
    fn update<W>(&mut self, width_ctrl: W)
    where
        W: WidthFunc,
    {
        self.width = 0;
        update_cell_info(self, width_ctrl);
    }

    fn set<W>(&mut self, text: T, width_ctrl: W)
    where
        W: WidthFunc,
    {
        let text = text.into();
        *self = create_cell_info(text, width_ctrl);
    }
}

impl AsRef<str> for CellInfo<'_> {
    fn as_ref(&self) -> &str {
        &self.text
    }
}

impl Clone for CellInfo<'_> {
    fn clone(&self) -> Self {
        let mut cell = Self {
            text: self.text.clone(),
            width: self.width,
            lines: vec![StrWithWidth::default(); self.lines.len()],
            count_lines: self.count_lines,
        };

        for (i, line) in self.lines.iter().enumerate() {
            cell.lines[i].width = line.width;

            cell.lines[i].text = match &line.text {
                Cow::Owned(line) => Cow::Owned(line.clone()),
                Cow::Borrowed(s) => {
                    // We need to redirect pointers to the original string.
                    //
                    // # Safety
                    //
                    // It must be safe because the referenced string and the references are dropped at the same time.
                    // And the referenced String is guaranted to not be changed.
                    let text = unsafe {
                        let text_ptr = self.text.as_ptr();
                        let line_ptr = s.as_ptr();
                        let text_shift = line_ptr as isize - text_ptr as isize;

                        let new_text_shifted_ptr = cell.text.as_ptr().offset(text_shift);

                        std::str::from_utf8_unchecked(std::slice::from_raw_parts(
                            new_text_shifted_ptr,
                            s.len(),
                        ))
                    };

                    Cow::Borrowed(text)
                }
            }
        }

        cell
    }
}

#[derive(Debug, Clone, Default)]
struct StrWithWidth<'a> {
    text: Cow<'a, str>,
    width: usize,
}

impl<'a> StrWithWidth<'a> {
    fn new(text: Cow<'a, str>, width: usize) -> Self {
        Self { text, width }
    }
}

fn create_cell_info<W>(text: Cow<'_, str>, width_fn: W) -> CellInfo<'_>
where
    W: WidthFunc,
{
    let mut info = CellInfo {
        text,
        count_lines: 1,
        ..Default::default()
    };

    // Here we do a small optimization.
    // We check if there's only 1 line in which case we don't allocate lines Vec
    let count_lines = count_lines(&info.text);
    if count_lines < 2 {
        info.width = width_fn.width(&info.text);
        return info;
    }

    // In case `Cow::Borrowed` we want to not allocate a String.
    // It's currerently not possible due to a lifetime issues. (It's known as self-referential struct)
    //
    // Here we change the lifetime of text.
    //
    // # Safety
    //
    // It must be safe because the referenced string and the references are dropped at the same time.
    // And the referenced String is guaranted to not be changed.
    let text = unsafe {
        std::str::from_utf8_unchecked(std::slice::from_raw_parts(
            info.text.as_ptr(),
            info.text.len(),
        ))
    };

    info.count_lines = count_lines;
    info.lines = vec![StrWithWidth::new(Cow::Borrowed(""), 0); count_lines];
    for (line, i) in get_lines(text).zip(info.lines.iter_mut()) {
        i.width = width_fn.width(line.as_ref());
        i.text = line;
        info.width = max(info.width, i.width);
    }

    info
}

fn update_cell_info<W>(info: &mut CellInfo<'_>, width_fn: W)
where
    W: WidthFunc,
{
    if info.text.is_empty() {
        return;
    }

    if info.lines.is_empty() && !info.text.is_empty() {
        info.width = width_fn.width(&info.text);
        return;
    }

    for line in &mut info.lines {
        line.width = width_fn.width(&line.text);
        info.width = max(info.width, line.width);
    }
}