Skip to content

Commit

Permalink
add completion_char feature
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianalexa committed Jan 3, 2024
1 parent be86aa2 commit f758f61
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions crates/libtiny_tui/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub(crate) struct Config {

#[serde(default)]
pub(crate) key_map: Option<KeyMap>,

#[serde(default)]
pub(crate) completion_char: Option<String>,
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
Expand Down
14 changes: 12 additions & 2 deletions crates/libtiny_tui/src/input_area/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ impl InputArea {
}

impl InputArea {
pub(crate) fn autocomplete(&mut self, dict: &Trie) {
pub(crate) fn autocomplete(&mut self, dict: &Trie, completion_char: &Option<String>) {
if self.in_autocomplete() {
// scroll next if you hit the KeyAction::InputAutoComplete key again
self.completion_prev_entry();
Expand Down Expand Up @@ -746,7 +746,17 @@ impl InputArea {
}
};

dict.drop_pfx(&mut word.iter().cloned())
let mut completions = dict.drop_pfx(&mut word.iter().cloned());

if let Some(completion_suffix) = &completion_char {
if cursor_left == 0 {
for completion in completions.iter_mut() {
completion.push_str(&format!("{} ", completion_suffix));
}
}
}

completions
};

if !completions.is_empty() {
Expand Down
12 changes: 11 additions & 1 deletion crates/libtiny_tui/src/messaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub(crate) struct MessagingUI {

/// Last timestamp added to the UI.
last_ts: Option<Timestamp>,

// Autocompletion character to be used when at the start of the line. Set with `set_completion_char`.
completion_char: Option<String>,
}

/// Length of ": " suffix of nicks in messages
Expand Down Expand Up @@ -92,6 +95,7 @@ impl MessagingUI {
height: i32,
scrollback: usize,
msg_layout: Layout,
completion_char: Option<String>,
) -> MessagingUI {
MessagingUI {
msg_area: MsgArea::new(width, height - 1, scrollback, msg_layout),
Expand All @@ -102,6 +106,7 @@ impl MessagingUI {
nicks: Trie::new(),
last_activity_line: None,
last_ts: None,
completion_char,
}
}

Expand Down Expand Up @@ -164,7 +169,7 @@ impl MessagingUI {
}
KeyAction::InputAutoComplete => {
if self.exit_dialogue.is_none() {
self.input_field.autocomplete(&self.nicks);
self.input_field.autocomplete(&self.nicks, &self.completion_char);
}
WidgetRet::KeyHandled
}
Expand Down Expand Up @@ -218,6 +223,11 @@ impl MessagingUI {
self.input_field.set(str)
}

/// Set completion char.
pub(crate) fn set_completion_char(&mut self, completion_char: Option<String>) {
self.completion_char = completion_char;
}

/// Set cursor location in the input field.
pub(crate) fn set_cursor(&mut self, cursor: i32) {
self.input_field.set_cursor(cursor);
Expand Down
14 changes: 14 additions & 0 deletions crates/libtiny_tui/src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ pub struct TUI {

/// TabConfig settings loaded from config file
tab_configs: TabConfigs,

// Autocompletion character to be used when at the start of the line.
completion_char: Option<String>,
}

pub(crate) enum CmdResult {
Expand Down Expand Up @@ -175,6 +178,7 @@ impl TUI {
key_map: KeyMap::default(),
config_path,
tab_configs: TabConfigs::default(),
completion_char: None,
};

// Init "mentions" tab. This needs to happen right after creating the TUI to be able to
Expand Down Expand Up @@ -365,9 +369,11 @@ impl TUI {
max_nick_length,
key_map,
layout,
completion_char,
..
} = config;
self.set_colors(colors);
self.set_completion_char(completion_char);
self.scrollback = scrollback.max(1);
self.key_map.load(&key_map.unwrap_or_default());
if let Some(layout) = layout {
Expand All @@ -394,6 +400,13 @@ impl TUI {
self.colors = colors;
}

fn set_completion_char(&mut self, completion_char: Option<String>) {
self.completion_char = completion_char;
for tab in &mut self.tabs {
tab.widget.set_completion_char((&self.completion_char).clone());
}
}

fn new_tab(&mut self, idx: usize, src: MsgSource, alias: Option<String>) {
let visible_name = alias.unwrap_or_else(|| match &src {
MsgSource::Serv { serv } => serv.to_owned(),
Expand Down Expand Up @@ -446,6 +459,7 @@ impl TUI {
self.height - 1,
self.scrollback,
self.msg_layout,
self.completion_char.clone(),
),
src,
style: TabStyle::Normal,
Expand Down

0 comments on commit f758f61

Please sign in to comment.