Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add completion_char feature #427

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 12 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>,
iulianalexa marked this conversation as resolved.
Show resolved Hide resolved
}

/// 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,8 @@ 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 +224,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
15 changes: 15 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,14 @@ 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 +460,7 @@ impl TUI {
self.height - 1,
self.scrollback,
self.msg_layout,
self.completion_char.clone(),
),
src,
style: TabStyle::Normal,
Expand Down