Skip to content

Commit 0b6d27e

Browse files
committed
add completion_char feature
1 parent 41596ab commit 0b6d27e

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

crates/libtiny_tui/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ pub(crate) struct Config {
3131

3232
#[serde(default)]
3333
pub(crate) key_map: Option<KeyMap>,
34+
35+
#[serde(default)]
36+
pub(crate) completion_char: Option<String>,
3437
}
3538

3639
#[derive(Debug, Deserialize, PartialEq, Eq)]

crates/libtiny_tui/src/input_area/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ impl InputArea {
712712
}
713713

714714
impl InputArea {
715-
pub(crate) fn autocomplete(&mut self, dict: &Trie) {
715+
pub(crate) fn autocomplete(&mut self, dict: &Trie, completion_char: &Option<String>) {
716716
if self.in_autocomplete() {
717717
// scroll next if you hit the KeyAction::InputAutoComplete key again
718718
self.completion_prev_entry();
@@ -746,7 +746,17 @@ impl InputArea {
746746
}
747747
};
748748

749-
dict.drop_pfx(&mut word.iter().cloned())
749+
let mut completions = dict.drop_pfx(&mut word.iter().cloned());
750+
751+
if let Some(completion_suffix) = &completion_char {
752+
if cursor_left == 0 {
753+
for completion in completions.iter_mut() {
754+
completion.push_str(&format!("{} ", completion_suffix));
755+
}
756+
}
757+
}
758+
759+
completions
750760
};
751761

752762
if !completions.is_empty() {

crates/libtiny_tui/src/messaging.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ pub(crate) struct MessagingUI {
3939

4040
/// Last timestamp added to the UI.
4141
last_ts: Option<Timestamp>,
42+
43+
// Autocompletion character to be used when at the start of the line. Set with `set_completion_char`.
44+
completion_char: Option<String>,
4245
}
4346

4447
/// Length of ": " suffix of nicks in messages
@@ -92,6 +95,7 @@ impl MessagingUI {
9295
height: i32,
9396
scrollback: usize,
9497
msg_layout: Layout,
98+
completion_char: Option<String>,
9599
) -> MessagingUI {
96100
MessagingUI {
97101
msg_area: MsgArea::new(width, height - 1, scrollback, msg_layout),
@@ -102,6 +106,7 @@ impl MessagingUI {
102106
nicks: Trie::new(),
103107
last_activity_line: None,
104108
last_ts: None,
109+
completion_char,
105110
}
106111
}
107112

@@ -164,7 +169,7 @@ impl MessagingUI {
164169
}
165170
KeyAction::InputAutoComplete => {
166171
if self.exit_dialogue.is_none() {
167-
self.input_field.autocomplete(&self.nicks);
172+
self.input_field.autocomplete(&self.nicks, &self.completion_char);
168173
}
169174
WidgetRet::KeyHandled
170175
}
@@ -218,6 +223,11 @@ impl MessagingUI {
218223
self.input_field.set(str)
219224
}
220225

226+
/// Set completion char.
227+
pub(crate) fn set_completion_char(&mut self, completion_char: Option<String>) {
228+
self.completion_char = completion_char;
229+
}
230+
221231
/// Set cursor location in the input field.
222232
pub(crate) fn set_cursor(&mut self, cursor: i32) {
223233
self.input_field.set_cursor(cursor);

crates/libtiny_tui/src/tui.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ pub struct TUI {
108108

109109
/// TabConfig settings loaded from config file
110110
tab_configs: TabConfigs,
111+
112+
// Autocompletion character to be used when at the start of the line.
113+
completion_char: Option<String>,
111114
}
112115

113116
pub(crate) enum CmdResult {
@@ -175,6 +178,7 @@ impl TUI {
175178
key_map: KeyMap::default(),
176179
config_path,
177180
tab_configs: TabConfigs::default(),
181+
completion_char: None,
178182
};
179183

180184
// Init "mentions" tab. This needs to happen right after creating the TUI to be able to
@@ -365,9 +369,11 @@ impl TUI {
365369
max_nick_length,
366370
key_map,
367371
layout,
372+
completion_char,
368373
..
369374
} = config;
370375
self.set_colors(colors);
376+
self.set_completion_char(completion_char);
371377
self.scrollback = scrollback.max(1);
372378
self.key_map.load(&key_map.unwrap_or_default());
373379
if let Some(layout) = layout {
@@ -394,6 +400,13 @@ impl TUI {
394400
self.colors = colors;
395401
}
396402

403+
fn set_completion_char(&mut self, completion_char: Option<String>) {
404+
self.completion_char = completion_char;
405+
for tab in &mut self.tabs {
406+
tab.widget.set_completion_char((&self.completion_char).clone());
407+
}
408+
}
409+
397410
fn new_tab(&mut self, idx: usize, src: MsgSource, alias: Option<String>) {
398411
let visible_name = alias.unwrap_or_else(|| match &src {
399412
MsgSource::Serv { serv } => serv.to_owned(),
@@ -446,6 +459,7 @@ impl TUI {
446459
self.height - 1,
447460
self.scrollback,
448461
self.msg_layout,
462+
self.completion_char.clone(),
449463
),
450464
src,
451465
style: TabStyle::Normal,

0 commit comments

Comments
 (0)