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 all 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
21 changes: 20 additions & 1 deletion crates/libtiny_tui/src/input_area/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ pub(crate) struct InputArea {
/// Current nickname. Not available on initialization (e.g. before registration with the
/// server). Set with `set_nick`.
nick: Option<Nickname>,

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

enum Mode {
Expand Down Expand Up @@ -120,13 +123,19 @@ impl InputArea {
history: Vec::with_capacity(HIST_SIZE),
mode: Mode::Edit,
nick: None,
completion_char: None,
}
}

pub(crate) fn set_nick(&mut self, value: String, color: usize) {
self.nick = Some(Nickname::new(value, color))
}

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

pub(crate) fn get_nick(&self) -> Option<String> {
self.nick.as_ref().map(|nick| nick.value.clone())
}
Expand Down Expand Up @@ -746,7 +755,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) = &self.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
4 changes: 4 additions & 0 deletions crates/libtiny_tui/src/messaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ impl MessagingUI {
self.input_field.set(str)
}

pub(crate) fn set_completion_char(&mut self, completion_char: Option<String>) {
self.input_field.set_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,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
4 changes: 4 additions & 0 deletions crates/tiny/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ defaults:
# Location for chat logs.
log_dir: "{}"

# Optional character(s) that will be appended at the end of a completed
# nick, if at the beginning of the line. Uncomment to enable.
# completion_char: ":"

# Limits the maximum number of messages stored in each tab. Default is
# unlimited.
# scrollback: 512
Expand Down