Struct StyledBuffer

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

Memory representation of the lines and styles

Implementations§

Source§

impl StyledBuffer

Source

pub fn insert_char(&mut self, ch: char)

Insert character at the current position with default style

Source

pub fn insert_styled_char(&mut self, ch: char, style: Style)

Insert character at the current position with style

Source

pub fn insert_string(&mut self, str: &str)

Insert string at the current position with default style

Examples found in repository?
examples/custom_prompt.rs (line 16)
9    fn prompt(&self) -> StyledBuffer {
10        let path = if let Ok(current_dir) = std::env::current_dir() {
11            current_dir.to_string_lossy().to_string()
12        } else {
13            "".to_owned()
14        };
15        let mut styled_buffer = StyledBuffer::default();
16        styled_buffer.insert_string(&format!("📁 {}> ", path));
17        styled_buffer
18    }
Source

pub fn insert_styled_string(&mut self, str: &str, style: Style)

Insert string at the current position with style

Examples found in repository?
examples/debug.rs (line 36)
27    fn hint(&self, buffer: &mut StyledBuffer) -> Option<StyledBuffer> {
28        if let Some(keyword) = buffer.last_alphabetic_keyword() {
29            let keyword_lower = keyword.to_lowercase();
30            for word in GITQL_RESERVED_KEYWORDS {
31                if word.starts_with(&keyword_lower) {
32                    let hint = &word[keyword.len()..];
33                    let mut styled_buffer = StyledBuffer::default();
34                    let mut style = Style::default();
35                    style.set_foreground_color(Color::DarkGrey);
36                    styled_buffer.insert_styled_string(hint, style);
37                    return Some(styled_buffer);
38                }
39            }
40        }
41        None
42    }
More examples
Hide additional examples
examples/keyword_hinter.rs (line 28)
19    fn hint(&self, buffer: &mut StyledBuffer) -> Option<StyledBuffer> {
20        if let Some(keyword) = buffer.last_alphabetic_keyword() {
21            let keyword_lower = keyword.to_lowercase();
22            for word in GITQL_RESERVED_KEYWORDS {
23                if word.starts_with(&keyword_lower) {
24                    let hint = &word[keyword.len()..];
25                    let mut styled_buffer = StyledBuffer::default();
26                    let mut style = Style::default();
27                    style.set_foreground_color(Color::DarkGrey);
28                    styled_buffer.insert_styled_string(hint, style);
29                    return Some(styled_buffer);
30                }
31            }
32        }
33        None
34    }
Source

pub fn move_char_right(&mut self)

Safe Move the cursor position to the right

Source

pub fn move_char_left(&mut self)

Safe Move the cursor position to the left

Source

pub fn move_word_right(&mut self)

Move the cursor to the begin of the next right word

Source

pub fn move_word_left(&mut self)

Move the cursor to the begin of the next right word

Source

pub fn move_to_start(&mut self)

Move cursor to the start of the buffer

Source

pub fn move_to_end(&mut self)

Move cursor to the end of the buffer

Source

pub fn delete_right_char(&mut self)

Deletes one character to the right

Source

pub fn delete_left_char(&mut self)

Deletes one character to the left

Source

pub fn delete_range(&mut self, from: usize, to: usize)

Deletes range of characters and styles from buffer

Source

pub fn buffer(&mut self) -> &Vec<char>

Get current Buffer

Examples found in repository?
examples/debug.rs (line 77)
76    fn highlight(&self, buffer: &mut StyledBuffer) {
77        let lines = buffer.buffer().clone();
78        let mut i: usize = 0;
79
80        let mut keyword_style = Style::default();
81        keyword_style.set_foreground_color(Color::Magenta);
82
83        let mut string_style = Style::default();
84        string_style.set_foreground_color(Color::Yellow);
85
86        loop {
87            if i >= lines.len() {
88                break;
89            }
90
91            // Highlight String literal
92            if lines[i] == '"' {
93                buffer.style_char(i, string_style.clone());
94                i += 1;
95
96                while i < lines.len() && lines[i] != '"' {
97                    buffer.style_char(i, string_style.clone());
98                    i += 1;
99                }
100
101                if i < lines.len() && lines[i] == '"' {
102                    buffer.style_char(i, string_style.clone());
103                    i += 1;
104                }
105
106                continue;
107            }
108
109            // Highlight reserved keyword
110            if lines[i].is_alphabetic() {
111                let start = i;
112                let mut keyword = String::new();
113                while i < lines.len() && (lines[i].is_alphanumeric() || lines[i] == '_') {
114                    keyword.push(lines[i]);
115                    i += 1;
116                }
117
118                keyword = keyword.to_lowercase();
119                if GITQL_RESERVED_KEYWORDS.contains(&keyword.as_str()) {
120                    buffer.style_range(start, i, keyword_style.clone())
121                }
122                continue;
123            }
124
125            i += 1;
126        }
127    }
128}
129
130#[derive(Default)]
131pub struct MatchingBracketsHighlighter;
132
133impl Highlighter for MatchingBracketsHighlighter {
134    fn highlight(&self, buffer: &mut StyledBuffer) {
135        let colors = vec![Color::Red, Color::Blue, Color::Yellow, Color::Green];
136        let mut brackets_stack: Vec<Color> = vec![];
137        let mut current_color_index = 0;
138
139        let lines = buffer.buffer().clone();
140        let mut i: usize = 0;
141        loop {
142            if i >= lines.len() {
143                break;
144            }
145
146            if lines[i] == '"' {
147                i += 1;
148                while i < lines.len() && lines[i] != '"' {
149                    i += 1;
150                }
151
152                if i < lines.len() {
153                    i += 1;
154                }
155                continue;
156            }
157
158            if lines[i] == '(' || lines[i] == '<' || lines[i] == '[' || lines[i] == '{' {
159                if current_color_index >= colors.len() {
160                    current_color_index = 0;
161                }
162
163                let color = colors[current_color_index];
164                current_color_index += 1;
165
166                brackets_stack.push(color);
167
168                let mut style = Style::default();
169                style.set_foreground_color(color);
170                buffer.style_char(i, style);
171                i += 1;
172                continue;
173            }
174
175            if lines[i] == ')' || lines[i] == '>' || lines[i] == ']' || lines[i] == '}' {
176                let color = if brackets_stack.is_empty() {
177                    colors[0]
178                } else {
179                    brackets_stack.pop().unwrap()
180                };
181
182                let mut style = Style::default();
183                style.set_foreground_color(color);
184                buffer.style_char(i, style);
185
186                i += 1;
187                continue;
188            }
189            i += 1;
190        }
191    }
More examples
Hide additional examples
examples/keyword_highlighter.rs (line 20)
19    fn highlight(&self, buffer: &mut StyledBuffer) {
20        let lines = buffer.buffer().clone();
21        let mut i: usize = 0;
22
23        let mut keyword_style = Style::default();
24        keyword_style.set_foreground_color(Color::Magenta);
25
26        let mut string_style = Style::default();
27        string_style.set_foreground_color(Color::Yellow);
28
29        loop {
30            if i >= lines.len() {
31                break;
32            }
33
34            // Highlight String literal
35            if lines[i] == '"' {
36                buffer.style_char(i, string_style.clone());
37                i += 1;
38
39                while i < lines.len() && lines[i] != '"' {
40                    buffer.style_char(i, string_style.clone());
41                    i += 1;
42                }
43
44                if i < lines.len() && lines[i] == '"' {
45                    buffer.style_char(i, string_style.clone());
46                    i += 1;
47                }
48
49                continue;
50            }
51
52            // Highlight reserved keyword
53            if lines[i].is_alphabetic() {
54                let start = i;
55                let mut keyword = String::new();
56                while i < lines.len() && (lines[i].is_alphanumeric() || lines[i] == '_') {
57                    keyword.push(lines[i]);
58                    i += 1;
59                }
60
61                keyword = keyword.to_lowercase();
62                if GITQL_RESERVED_KEYWORDS.contains(&keyword.as_str()) {
63                    buffer.style_range(start, i, keyword_style.clone())
64                }
65                continue;
66            }
67
68            i += 1;
69        }
70    }
examples/hex_color_highlighter.rs (line 14)
13    fn highlight(&self, buffer: &mut StyledBuffer) {
14        let lines = buffer.buffer().clone();
15        let mut i: usize = 0;
16
17        loop {
18            if i >= lines.len() {
19                break;
20            }
21
22            // Highlight String literal
23            if lines[i] == '"' {
24                i += 1;
25
26                while i < lines.len() && lines[i] != '"' {
27                    i += 1;
28                }
29
30                if i < lines.len() && lines[i] == '"' {
31                    i += 1;
32                }
33
34                continue;
35            }
36
37            // Highlight hex value background with it value
38            if lines[i] == '#' && i + 6 < lines.len() {
39                let start = i;
40                i += 1;
41                let hex_value = &lines[i..i + 6];
42                for ch in hex_value {
43                    if !ch.is_ascii_hexdigit() {
44                        return;
45                    }
46                }
47                let hex_string = hex_value.iter().cloned().collect::<String>();
48                if let Ok(hex_value) = usize::from_str_radix(&hex_string, 16) {
49                    let red = (hex_value >> 16) & 0xFF;
50                    let green = (hex_value >> 8) & 0xFF;
51                    let blue = hex_value & 0xFF;
52                    let mut style = Style::default();
53                    style.set_background_color(Color::Rgb {
54                        r: red as u8,
55                        g: green as u8,
56                        b: blue as u8,
57                    });
58                    buffer.style_range(start, start + 7, style);
59                }
60            }
61
62            i += 1;
63        }
64    }
examples/matching_brackets_highlighters.rs (line 18)
13    fn highlight(&self, buffer: &mut StyledBuffer) {
14        let colors = vec![Color::Red, Color::Blue, Color::Yellow, Color::Green];
15        let mut brackets_stack: Vec<Color> = vec![];
16        let mut current_color_index = 0;
17
18        let lines = buffer.buffer().clone();
19        let mut i: usize = 0;
20        loop {
21            if i >= lines.len() {
22                break;
23            }
24
25            if lines[i] == '"' {
26                i += 1;
27                while i < lines.len() && lines[i] != '"' {
28                    i += 1;
29                }
30
31                if i < lines.len() {
32                    i += 1;
33                }
34                continue;
35            }
36
37            if lines[i] == '(' || lines[i] == '<' || lines[i] == '[' || lines[i] == '{' {
38                if current_color_index >= colors.len() {
39                    current_color_index = 0;
40                }
41
42                let color = colors[current_color_index];
43                current_color_index += 1;
44
45                brackets_stack.push(color);
46
47                let mut style = Style::default();
48                style.set_foreground_color(color);
49                buffer.style_char(i, style);
50                i += 1;
51                continue;
52            }
53
54            if lines[i] == ')' || lines[i] == '>' || lines[i] == ']' || lines[i] == '}' {
55                let color = if brackets_stack.is_empty() {
56                    colors[0]
57                } else {
58                    brackets_stack.pop().unwrap()
59                };
60
61                let mut style = Style::default();
62                style.set_foreground_color(color);
63                buffer.style_char(i, style);
64
65                i += 1;
66                continue;
67            }
68            i += 1;
69        }
70    }
Source

pub fn literal(&self) -> String

Get the literal value of StyledBuffer without styles

Source

pub fn char_at(&self, position: usize) -> Option<char>

Get char at position

Source

pub fn sub_string(&self, start: usize, end: usize) -> Option<String>

Get the sub string from the provided range, or None if range is invalid

Source

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

Return the last keyword that contains alphabetic characters on the buffer or None

Examples found in repository?
examples/debug.rs (line 28)
27    fn hint(&self, buffer: &mut StyledBuffer) -> Option<StyledBuffer> {
28        if let Some(keyword) = buffer.last_alphabetic_keyword() {
29            let keyword_lower = keyword.to_lowercase();
30            for word in GITQL_RESERVED_KEYWORDS {
31                if word.starts_with(&keyword_lower) {
32                    let hint = &word[keyword.len()..];
33                    let mut styled_buffer = StyledBuffer::default();
34                    let mut style = Style::default();
35                    style.set_foreground_color(Color::DarkGrey);
36                    styled_buffer.insert_styled_string(hint, style);
37                    return Some(styled_buffer);
38                }
39            }
40        }
41        None
42    }
43}
44
45pub struct FixedCompleter;
46
47impl Completer for FixedCompleter {
48    fn complete(&self, input: &StyledBuffer) -> Vec<Suggestion> {
49        let mut suggestions: Vec<Suggestion> = vec![];
50        if input.position() != input.len() {
51            return suggestions;
52        }
53
54        if let Some(keyword) = input.last_alphabetic_keyword() {
55            for reserved_keyword in GITQL_RESERVED_KEYWORDS {
56                if reserved_keyword.starts_with(&keyword) {
57                    let suggestion = Suggestion {
58                        content: StyledBuffer::from(reserved_keyword),
59                        span: Span {
60                            start: input.len() - keyword.len(),
61                            end: input.len(),
62                        },
63                    };
64                    suggestions.push(suggestion);
65                }
66            }
67        }
68        suggestions
69    }
More examples
Hide additional examples
examples/keyword_hinter.rs (line 20)
19    fn hint(&self, buffer: &mut StyledBuffer) -> Option<StyledBuffer> {
20        if let Some(keyword) = buffer.last_alphabetic_keyword() {
21            let keyword_lower = keyword.to_lowercase();
22            for word in GITQL_RESERVED_KEYWORDS {
23                if word.starts_with(&keyword_lower) {
24                    let hint = &word[keyword.len()..];
25                    let mut styled_buffer = StyledBuffer::default();
26                    let mut style = Style::default();
27                    style.set_foreground_color(Color::DarkGrey);
28                    styled_buffer.insert_styled_string(hint, style);
29                    return Some(styled_buffer);
30                }
31            }
32        }
33        None
34    }
examples/drop_down_auto_complete.rs (line 27)
21    fn complete(&self, input: &StyledBuffer) -> Vec<Suggestion> {
22        let mut suggestions: Vec<Suggestion> = vec![];
23        if input.position() != input.len() {
24            return suggestions;
25        }
26
27        if let Some(keyword) = input.last_alphabetic_keyword() {
28            for reserved_keyword in GITQL_RESERVED_KEYWORDS {
29                if reserved_keyword.starts_with(&keyword) {
30                    let suggestion = Suggestion {
31                        content: StyledBuffer::from(reserved_keyword),
32                        span: Span {
33                            start: input.len() - keyword.len(),
34                            end: input.len(),
35                        },
36                    };
37                    suggestions.push(suggestion);
38                }
39            }
40        }
41        suggestions
42    }
Source

pub fn styles(&self) -> &Vec<Style>

Get current Styles

Source

pub fn set_styles(&mut self, styles: &mut Vec<Style>)

Update the current list of styles

Source

pub fn style_char(&mut self, position: usize, style: Style)

Set style for one character

Examples found in repository?
examples/debug.rs (line 93)
76    fn highlight(&self, buffer: &mut StyledBuffer) {
77        let lines = buffer.buffer().clone();
78        let mut i: usize = 0;
79
80        let mut keyword_style = Style::default();
81        keyword_style.set_foreground_color(Color::Magenta);
82
83        let mut string_style = Style::default();
84        string_style.set_foreground_color(Color::Yellow);
85
86        loop {
87            if i >= lines.len() {
88                break;
89            }
90
91            // Highlight String literal
92            if lines[i] == '"' {
93                buffer.style_char(i, string_style.clone());
94                i += 1;
95
96                while i < lines.len() && lines[i] != '"' {
97                    buffer.style_char(i, string_style.clone());
98                    i += 1;
99                }
100
101                if i < lines.len() && lines[i] == '"' {
102                    buffer.style_char(i, string_style.clone());
103                    i += 1;
104                }
105
106                continue;
107            }
108
109            // Highlight reserved keyword
110            if lines[i].is_alphabetic() {
111                let start = i;
112                let mut keyword = String::new();
113                while i < lines.len() && (lines[i].is_alphanumeric() || lines[i] == '_') {
114                    keyword.push(lines[i]);
115                    i += 1;
116                }
117
118                keyword = keyword.to_lowercase();
119                if GITQL_RESERVED_KEYWORDS.contains(&keyword.as_str()) {
120                    buffer.style_range(start, i, keyword_style.clone())
121                }
122                continue;
123            }
124
125            i += 1;
126        }
127    }
128}
129
130#[derive(Default)]
131pub struct MatchingBracketsHighlighter;
132
133impl Highlighter for MatchingBracketsHighlighter {
134    fn highlight(&self, buffer: &mut StyledBuffer) {
135        let colors = vec![Color::Red, Color::Blue, Color::Yellow, Color::Green];
136        let mut brackets_stack: Vec<Color> = vec![];
137        let mut current_color_index = 0;
138
139        let lines = buffer.buffer().clone();
140        let mut i: usize = 0;
141        loop {
142            if i >= lines.len() {
143                break;
144            }
145
146            if lines[i] == '"' {
147                i += 1;
148                while i < lines.len() && lines[i] != '"' {
149                    i += 1;
150                }
151
152                if i < lines.len() {
153                    i += 1;
154                }
155                continue;
156            }
157
158            if lines[i] == '(' || lines[i] == '<' || lines[i] == '[' || lines[i] == '{' {
159                if current_color_index >= colors.len() {
160                    current_color_index = 0;
161                }
162
163                let color = colors[current_color_index];
164                current_color_index += 1;
165
166                brackets_stack.push(color);
167
168                let mut style = Style::default();
169                style.set_foreground_color(color);
170                buffer.style_char(i, style);
171                i += 1;
172                continue;
173            }
174
175            if lines[i] == ')' || lines[i] == '>' || lines[i] == ']' || lines[i] == '}' {
176                let color = if brackets_stack.is_empty() {
177                    colors[0]
178                } else {
179                    brackets_stack.pop().unwrap()
180                };
181
182                let mut style = Style::default();
183                style.set_foreground_color(color);
184                buffer.style_char(i, style);
185
186                i += 1;
187                continue;
188            }
189            i += 1;
190        }
191    }
More examples
Hide additional examples
examples/keyword_highlighter.rs (line 36)
19    fn highlight(&self, buffer: &mut StyledBuffer) {
20        let lines = buffer.buffer().clone();
21        let mut i: usize = 0;
22
23        let mut keyword_style = Style::default();
24        keyword_style.set_foreground_color(Color::Magenta);
25
26        let mut string_style = Style::default();
27        string_style.set_foreground_color(Color::Yellow);
28
29        loop {
30            if i >= lines.len() {
31                break;
32            }
33
34            // Highlight String literal
35            if lines[i] == '"' {
36                buffer.style_char(i, string_style.clone());
37                i += 1;
38
39                while i < lines.len() && lines[i] != '"' {
40                    buffer.style_char(i, string_style.clone());
41                    i += 1;
42                }
43
44                if i < lines.len() && lines[i] == '"' {
45                    buffer.style_char(i, string_style.clone());
46                    i += 1;
47                }
48
49                continue;
50            }
51
52            // Highlight reserved keyword
53            if lines[i].is_alphabetic() {
54                let start = i;
55                let mut keyword = String::new();
56                while i < lines.len() && (lines[i].is_alphanumeric() || lines[i] == '_') {
57                    keyword.push(lines[i]);
58                    i += 1;
59                }
60
61                keyword = keyword.to_lowercase();
62                if GITQL_RESERVED_KEYWORDS.contains(&keyword.as_str()) {
63                    buffer.style_range(start, i, keyword_style.clone())
64                }
65                continue;
66            }
67
68            i += 1;
69        }
70    }
examples/matching_brackets_highlighters.rs (line 49)
13    fn highlight(&self, buffer: &mut StyledBuffer) {
14        let colors = vec![Color::Red, Color::Blue, Color::Yellow, Color::Green];
15        let mut brackets_stack: Vec<Color> = vec![];
16        let mut current_color_index = 0;
17
18        let lines = buffer.buffer().clone();
19        let mut i: usize = 0;
20        loop {
21            if i >= lines.len() {
22                break;
23            }
24
25            if lines[i] == '"' {
26                i += 1;
27                while i < lines.len() && lines[i] != '"' {
28                    i += 1;
29                }
30
31                if i < lines.len() {
32                    i += 1;
33                }
34                continue;
35            }
36
37            if lines[i] == '(' || lines[i] == '<' || lines[i] == '[' || lines[i] == '{' {
38                if current_color_index >= colors.len() {
39                    current_color_index = 0;
40                }
41
42                let color = colors[current_color_index];
43                current_color_index += 1;
44
45                brackets_stack.push(color);
46
47                let mut style = Style::default();
48                style.set_foreground_color(color);
49                buffer.style_char(i, style);
50                i += 1;
51                continue;
52            }
53
54            if lines[i] == ')' || lines[i] == '>' || lines[i] == ']' || lines[i] == '}' {
55                let color = if brackets_stack.is_empty() {
56                    colors[0]
57                } else {
58                    brackets_stack.pop().unwrap()
59                };
60
61                let mut style = Style::default();
62                style.set_foreground_color(color);
63                buffer.style_char(i, style);
64
65                i += 1;
66                continue;
67            }
68            i += 1;
69        }
70    }
Source

pub fn style_range(&mut self, start: usize, end: usize, style: Style)

Set style for a range of characters

Examples found in repository?
examples/debug.rs (line 120)
76    fn highlight(&self, buffer: &mut StyledBuffer) {
77        let lines = buffer.buffer().clone();
78        let mut i: usize = 0;
79
80        let mut keyword_style = Style::default();
81        keyword_style.set_foreground_color(Color::Magenta);
82
83        let mut string_style = Style::default();
84        string_style.set_foreground_color(Color::Yellow);
85
86        loop {
87            if i >= lines.len() {
88                break;
89            }
90
91            // Highlight String literal
92            if lines[i] == '"' {
93                buffer.style_char(i, string_style.clone());
94                i += 1;
95
96                while i < lines.len() && lines[i] != '"' {
97                    buffer.style_char(i, string_style.clone());
98                    i += 1;
99                }
100
101                if i < lines.len() && lines[i] == '"' {
102                    buffer.style_char(i, string_style.clone());
103                    i += 1;
104                }
105
106                continue;
107            }
108
109            // Highlight reserved keyword
110            if lines[i].is_alphabetic() {
111                let start = i;
112                let mut keyword = String::new();
113                while i < lines.len() && (lines[i].is_alphanumeric() || lines[i] == '_') {
114                    keyword.push(lines[i]);
115                    i += 1;
116                }
117
118                keyword = keyword.to_lowercase();
119                if GITQL_RESERVED_KEYWORDS.contains(&keyword.as_str()) {
120                    buffer.style_range(start, i, keyword_style.clone())
121                }
122                continue;
123            }
124
125            i += 1;
126        }
127    }
More examples
Hide additional examples
examples/keyword_highlighter.rs (line 63)
19    fn highlight(&self, buffer: &mut StyledBuffer) {
20        let lines = buffer.buffer().clone();
21        let mut i: usize = 0;
22
23        let mut keyword_style = Style::default();
24        keyword_style.set_foreground_color(Color::Magenta);
25
26        let mut string_style = Style::default();
27        string_style.set_foreground_color(Color::Yellow);
28
29        loop {
30            if i >= lines.len() {
31                break;
32            }
33
34            // Highlight String literal
35            if lines[i] == '"' {
36                buffer.style_char(i, string_style.clone());
37                i += 1;
38
39                while i < lines.len() && lines[i] != '"' {
40                    buffer.style_char(i, string_style.clone());
41                    i += 1;
42                }
43
44                if i < lines.len() && lines[i] == '"' {
45                    buffer.style_char(i, string_style.clone());
46                    i += 1;
47                }
48
49                continue;
50            }
51
52            // Highlight reserved keyword
53            if lines[i].is_alphabetic() {
54                let start = i;
55                let mut keyword = String::new();
56                while i < lines.len() && (lines[i].is_alphanumeric() || lines[i] == '_') {
57                    keyword.push(lines[i]);
58                    i += 1;
59                }
60
61                keyword = keyword.to_lowercase();
62                if GITQL_RESERVED_KEYWORDS.contains(&keyword.as_str()) {
63                    buffer.style_range(start, i, keyword_style.clone())
64                }
65                continue;
66            }
67
68            i += 1;
69        }
70    }
examples/hex_color_highlighter.rs (line 58)
13    fn highlight(&self, buffer: &mut StyledBuffer) {
14        let lines = buffer.buffer().clone();
15        let mut i: usize = 0;
16
17        loop {
18            if i >= lines.len() {
19                break;
20            }
21
22            // Highlight String literal
23            if lines[i] == '"' {
24                i += 1;
25
26                while i < lines.len() && lines[i] != '"' {
27                    i += 1;
28                }
29
30                if i < lines.len() && lines[i] == '"' {
31                    i += 1;
32                }
33
34                continue;
35            }
36
37            // Highlight hex value background with it value
38            if lines[i] == '#' && i + 6 < lines.len() {
39                let start = i;
40                i += 1;
41                let hex_value = &lines[i..i + 6];
42                for ch in hex_value {
43                    if !ch.is_ascii_hexdigit() {
44                        return;
45                    }
46                }
47                let hex_string = hex_value.iter().cloned().collect::<String>();
48                if let Ok(hex_value) = usize::from_str_radix(&hex_string, 16) {
49                    let red = (hex_value >> 16) & 0xFF;
50                    let green = (hex_value >> 8) & 0xFF;
51                    let blue = hex_value & 0xFF;
52                    let mut style = Style::default();
53                    style.set_background_color(Color::Rgb {
54                        r: red as u8,
55                        g: green as u8,
56                        b: blue as u8,
57                    });
58                    buffer.style_range(start, start + 7, style);
59                }
60            }
61
62            i += 1;
63        }
64    }
Source

pub fn style_all(&mut self, style: Style)

Set one style for all characters

Source

pub fn reset_styles(&mut self)

Reset all styles to the default one

Source

pub fn clear(&mut self)

Empty buffer, styles and reset cursor position

Source

pub fn set_position(&mut self, pos: usize)

Source

pub fn position(&self) -> usize

Get cursor position

Examples found in repository?
examples/debug.rs (line 50)
48    fn complete(&self, input: &StyledBuffer) -> Vec<Suggestion> {
49        let mut suggestions: Vec<Suggestion> = vec![];
50        if input.position() != input.len() {
51            return suggestions;
52        }
53
54        if let Some(keyword) = input.last_alphabetic_keyword() {
55            for reserved_keyword in GITQL_RESERVED_KEYWORDS {
56                if reserved_keyword.starts_with(&keyword) {
57                    let suggestion = Suggestion {
58                        content: StyledBuffer::from(reserved_keyword),
59                        span: Span {
60                            start: input.len() - keyword.len(),
61                            end: input.len(),
62                        },
63                    };
64                    suggestions.push(suggestion);
65                }
66            }
67        }
68        suggestions
69    }
More examples
Hide additional examples
examples/drop_down_auto_complete.rs (line 23)
21    fn complete(&self, input: &StyledBuffer) -> Vec<Suggestion> {
22        let mut suggestions: Vec<Suggestion> = vec![];
23        if input.position() != input.len() {
24            return suggestions;
25        }
26
27        if let Some(keyword) = input.last_alphabetic_keyword() {
28            for reserved_keyword in GITQL_RESERVED_KEYWORDS {
29                if reserved_keyword.starts_with(&keyword) {
30                    let suggestion = Suggestion {
31                        content: StyledBuffer::from(reserved_keyword),
32                        span: Span {
33                            start: input.len() - keyword.len(),
34                            end: input.len(),
35                        },
36                    };
37                    suggestions.push(suggestion);
38                }
39            }
40        }
41        suggestions
42    }
Source

pub fn is_cursor_at_the_end(&self) -> bool

Returns true if the cursor position is at the end of the buffer

Source

pub fn len(&self) -> usize

Length of the buffer

Examples found in repository?
examples/debug.rs (line 50)
48    fn complete(&self, input: &StyledBuffer) -> Vec<Suggestion> {
49        let mut suggestions: Vec<Suggestion> = vec![];
50        if input.position() != input.len() {
51            return suggestions;
52        }
53
54        if let Some(keyword) = input.last_alphabetic_keyword() {
55            for reserved_keyword in GITQL_RESERVED_KEYWORDS {
56                if reserved_keyword.starts_with(&keyword) {
57                    let suggestion = Suggestion {
58                        content: StyledBuffer::from(reserved_keyword),
59                        span: Span {
60                            start: input.len() - keyword.len(),
61                            end: input.len(),
62                        },
63                    };
64                    suggestions.push(suggestion);
65                }
66            }
67        }
68        suggestions
69    }
More examples
Hide additional examples
examples/drop_down_auto_complete.rs (line 23)
21    fn complete(&self, input: &StyledBuffer) -> Vec<Suggestion> {
22        let mut suggestions: Vec<Suggestion> = vec![];
23        if input.position() != input.len() {
24            return suggestions;
25        }
26
27        if let Some(keyword) = input.last_alphabetic_keyword() {
28            for reserved_keyword in GITQL_RESERVED_KEYWORDS {
29                if reserved_keyword.starts_with(&keyword) {
30                    let suggestion = Suggestion {
31                        content: StyledBuffer::from(reserved_keyword),
32                        span: Span {
33                            start: input.len() - keyword.len(),
34                            end: input.len(),
35                        },
36                    };
37                    suggestions.push(suggestion);
38                }
39            }
40        }
41        suggestions
42    }
Source

pub fn is_empty(&self) -> bool

Returns true if the buffer contains no elements.

Trait Implementations§

Source§

impl Default for StyledBuffer

Create default instance of StyledBuffer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<&str> for StyledBuffer

Create instance of StyledBuffer from String

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.