diff options
| author | Brooks Rady <b.j.rady@gmail.com> | 2021-03-26 13:20:06 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-26 13:20:06 +0000 |
| commit | d818661c727a59312e1a43432e84fbeec79fc145 (patch) | |
| tree | e8ab04124ee7f5c87cd88be63c1867c7d9b2289c /default-tiles | |
| parent | fix(render): various rendering issues (#238) (diff) | |
| parent | Fix some overzealous format!() removals and a stray comment (diff) | |
| download | sl0ppij-d818661c727a59312e1a43432e84fbeec79fc145.tar.gz sl0ppij-d818661c727a59312e1a43432e84fbeec79fc145.tar.xz sl0ppij-d818661c727a59312e1a43432e84fbeec79fc145.zip | |
feat(plugin): refactored to a new and more general plugin API
- Changed `init()` to `load()`
- Deduplicated structs that are in both `zellij` and `zellij-tile`
- WASM thread now keeps a mapping of plugins and event subscriptions
- Added host-functions for subscribing and unsubscribing from events
- Introduced the `update(event)` callback into the API (which is called to handle plugin events)
- Made mode changes send an event to the WASM thread
- Removed the `AppInstruction::Get/SetState` calls (and any global state)
- Removed the `get_help()` host function
- Added an event system for key-presses
- Added an event for updating tabs
- Ported support for renaming tabs
- Removed every plugin callback other than `load()`, `update(event)`, and `draw(rows, cols)`
- Removed every WASM thread event other than Load, Draw, Update, Unload, and Quit
- Calls render after a plugin is updated
- Switched back to upstream `termion`
- Loads of lint fixing
- Shrunk and simplified the code!
Diffstat (limited to 'default-tiles')
| -rw-r--r-- | default-tiles/status-bar/src/first_line.rs | 100 | ||||
| -rw-r--r-- | default-tiles/status-bar/src/main.rs | 22 | ||||
| -rw-r--r-- | default-tiles/status-bar/src/second_line.rs | 18 | ||||
| -rw-r--r-- | default-tiles/strider/src/main.rs | 73 | ||||
| -rw-r--r-- | default-tiles/strider/src/state.rs | 2 | ||||
| -rw-r--r-- | default-tiles/tab-bar/src/line.rs | 6 | ||||
| -rw-r--r-- | default-tiles/tab-bar/src/main.rs | 63 |
7 files changed, 120 insertions, 164 deletions
diff --git a/default-tiles/status-bar/src/first_line.rs b/default-tiles/status-bar/src/first_line.rs index bd9c5425..0e4ec53b 100644 --- a/default-tiles/status-bar/src/first_line.rs +++ b/default-tiles/status-bar/src/first_line.rs @@ -1,5 +1,5 @@ use ansi_term::{ANSIStrings, Style}; -use zellij_tile::*; +use zellij_tile::prelude::*; use crate::colors::{BLACK, BRIGHT_GRAY, GRAY, GREEN, RED, WHITE}; use crate::{LinePart, ARROW_SEPARATOR}; @@ -70,19 +70,19 @@ fn unselected_mode_shortcut(letter: char, text: &str) -> LinePart { .fg(BLACK) .on(BRIGHT_GRAY) .bold() - .paint(format!(" <")); + .paint(" <"); let char_shortcut = Style::new() .bold() .fg(RED) .on(BRIGHT_GRAY) .bold() - .paint(format!("{}", letter)); + .paint(letter.to_string()); let char_right_separator = Style::new() .bold() .fg(BLACK) .on(BRIGHT_GRAY) .bold() - .paint(format!(">")); + .paint(">"); let styled_text = Style::new() .fg(BLACK) .on(BRIGHT_GRAY) @@ -90,41 +90,29 @@ fn unselected_mode_shortcut(letter: char, text: &str) -> LinePart { .paint(format!("{} ", text)); let suffix_separator = Style::new().fg(BRIGHT_GRAY).on(GRAY).paint(ARROW_SEPARATOR); LinePart { - part: format!( - "{}", - ANSIStrings(&[ - prefix_separator, - char_left_separator, - char_shortcut, - char_right_separator, - styled_text, - suffix_separator - ]) - ), + part: ANSIStrings(&[ + prefix_separator, + char_left_separator, + char_shortcut, + char_right_separator, + styled_text, + suffix_separator, + ]) + .to_string(), len: text.chars().count() + 6, // 2 for the arrows, 3 for the char separators, 1 for the character } } fn selected_mode_shortcut(letter: char, text: &str) -> LinePart { let prefix_separator = Style::new().fg(GRAY).on(GREEN).paint(ARROW_SEPARATOR); - let char_left_separator = Style::new() - .bold() - .fg(BLACK) - .on(GREEN) - .bold() - .paint(format!(" <")); + let char_left_separator = Style::new().bold().fg(BLACK).on(GREEN).bold().paint(" <"); let char_shortcut = Style::new() .bold() .fg(RED) .on(GREEN) .bold() - .paint(format!("{}", letter)); - let char_right_separator = Style::new() - .bold() - .fg(BLACK) - .on(GREEN) - .bold() - .paint(format!(">")); + .paint(letter.to_string()); + let char_right_separator = Style::new().bold().fg(BLACK).on(GREEN).bold().paint(">"); let styled_text = Style::new() .fg(BLACK) .on(GREEN) @@ -132,17 +120,15 @@ fn selected_mode_shortcut(letter: char, text: &str) -> LinePart { .paint(format!("{} ", text)); let suffix_separator = Style::new().fg(GREEN).on(GRAY).paint(ARROW_SEPARATOR); LinePart { - part: format!( - "{}", - ANSIStrings(&[ - prefix_separator, - char_left_separator, - char_shortcut, - char_right_separator, - styled_text, - suffix_separator - ]) - ), + part: ANSIStrings(&[ + prefix_separator, + char_left_separator, + char_shortcut, + char_right_separator, + styled_text, + suffix_separator, + ]) + .to_string(), len: text.chars().count() + 6, // 2 for the arrows, 3 for the char separators, 1 for the character } } @@ -173,10 +159,7 @@ fn selected_mode_shortcut_single_letter(letter: char) -> LinePart { .paint(char_shortcut_text); let suffix_separator = Style::new().fg(GREEN).on(GRAY).paint(ARROW_SEPARATOR); LinePart { - part: format!( - "{}", - ANSIStrings(&[prefix_separator, char_shortcut, suffix_separator]) - ), + part: ANSIStrings(&[prefix_separator, char_shortcut, suffix_separator]).to_string(), len, } } @@ -193,10 +176,7 @@ fn unselected_mode_shortcut_single_letter(letter: char) -> LinePart { .paint(char_shortcut_text); let suffix_separator = Style::new().fg(BRIGHT_GRAY).on(GRAY).paint(ARROW_SEPARATOR); LinePart { - part: format!( - "{}", - ANSIStrings(&[prefix_separator, char_shortcut, suffix_separator]) - ), + part: ANSIStrings(&[prefix_separator, char_shortcut, suffix_separator]).to_string(), len, } } @@ -225,12 +205,8 @@ fn shortened_ctrl_key(key: &CtrlKeyShortcut) -> LinePart { _ => shortened_text, }; match key.mode { - CtrlKeyMode::Unselected => { - unselected_mode_shortcut(letter_shortcut, &format!("{}", shortened_text)) - } - CtrlKeyMode::Selected => { - selected_mode_shortcut(letter_shortcut, &format!("{}", shortened_text)) - } + CtrlKeyMode::Unselected => unselected_mode_shortcut(letter_shortcut, &shortened_text), + CtrlKeyMode::Selected => selected_mode_shortcut(letter_shortcut, &shortened_text), CtrlKeyMode::Disabled => { disabled_mode_shortcut(&format!(" <{}>{}", letter_shortcut, shortened_text)) } @@ -282,16 +258,16 @@ pub fn superkey() -> LinePart { let prefix_text = " Ctrl + "; let prefix = Style::new().fg(WHITE).on(GRAY).bold().paint(prefix_text); LinePart { - part: format!("{}", prefix), + part: prefix.to_string(), len: prefix_text.chars().count(), } } -pub fn ctrl_keys(help: &Help, max_len: usize) -> LinePart { +pub fn ctrl_keys(help: &ModeInfo, max_len: usize) -> LinePart { match &help.mode { InputMode::Locked => key_indicators( max_len, - &vec![ + &[ CtrlKeyShortcut::new(CtrlKeyMode::Selected, CtrlKeyAction::Lock), CtrlKeyShortcut::new(CtrlKeyMode::Disabled, CtrlKeyAction::Pane), CtrlKeyShortcut::new(CtrlKeyMode::Disabled, CtrlKeyAction::Tab), @@ -302,7 +278,7 @@ pub fn ctrl_keys(help: &Help, max_len: usize) -> LinePart { ), InputMode::Resize => key_indicators( max_len, - &vec![ + &[ CtrlKeyShortcut::new(CtrlKeyMode::Unselected, CtrlKeyAction::Lock), CtrlKeyShortcut::new(CtrlKeyMode::Unselected, CtrlKeyAction::Pane), CtrlKeyShortcut::new(CtrlKeyMode::Unselected, CtrlKeyAction::Tab), @@ -313,7 +289,7 @@ pub fn ctrl_keys(help: &Help, max_len: usize) -> LinePart { ), InputMode::Pane => key_indicators( max_len, - &vec![ + &[ CtrlKeyShortcut::new(CtrlKeyMode::Unselected, CtrlKeyAction::Lock), CtrlKeyShortcut::new(CtrlKeyMode::Selected, CtrlKeyAction::Pane), CtrlKeyShortcut::new(CtrlKeyMode::Unselected, CtrlKeyAction::Tab), @@ -324,7 +300,7 @@ pub fn ctrl_keys(help: &Help, max_len: usize) -> LinePart { ), InputMode::Tab | InputMode::RenameTab => key_indicators( max_len, - &vec![ + &[ CtrlKeyShortcut::new(CtrlKeyMode::Unselected, CtrlKeyAction::Lock), CtrlKeyShortcut::new(CtrlKeyMode::Unselected, CtrlKeyAction::Pane), CtrlKeyShortcut::new(CtrlKeyMode::Selected, CtrlKeyAction::Tab), @@ -335,7 +311,7 @@ pub fn ctrl_keys(help: &Help, max_len: usize) -> LinePart { ), InputMode::Scroll => key_indicators( max_len, - &vec![ + &[ CtrlKeyShortcut::new(CtrlKeyMode::Unselected, CtrlKeyAction::Lock), CtrlKeyShortcut::new(CtrlKeyMode::Unselected, CtrlKeyAction::Pane), CtrlKeyShortcut::new(CtrlKeyMode::Unselected, CtrlKeyAction::Tab), @@ -344,9 +320,9 @@ pub fn ctrl_keys(help: &Help, max_len: usize) -> LinePart { CtrlKeyShortcut::new(CtrlKeyMode::Unselected, CtrlKeyAction::Quit), ], ), - InputMode::Normal | _ => key_indicators( + InputMode::Normal => key_indicators( max_len, - &vec![ + &[ CtrlKeyShortcut::new(CtrlKeyMode::Unselected, CtrlKeyAction::Lock), CtrlKeyShortcut::new(CtrlKeyMode::Unselected, CtrlKeyAction::Pane), CtrlKeyShortcut::new(CtrlKeyMode::Unselected, CtrlKeyAction::Tab), diff --git a/default-tiles/status-bar/src/main.rs b/default-tiles/status-bar/src/main.rs index 6d41fb7b..8daa158e 100644 --- a/default-tiles/status-bar/src/main.rs +++ b/default-tiles/status-bar/src/main.rs @@ -2,7 +2,7 @@ mod first_line; mod second_line; use std::fmt::{Display, Error, Formatter}; -use zellij_tile::*; +use zellij_tile::prelude::*; use first_line::{ctrl_keys, superkey}; use second_line::keybinds; @@ -23,7 +23,9 @@ static ARROW_SEPARATOR: &str = ""; static MORE_MSG: &str = " ... "; #[derive(Default)] -struct State {} +struct State { + mode_info: ModeInfo, +} register_tile!(State); @@ -40,19 +42,25 @@ impl Display for LinePart { } impl ZellijTile for State { - fn init(&mut self) { + fn load(&mut self) { set_selectable(false); set_invisible_borders(true); set_max_height(2); + subscribe(&[EventType::ModeUpdate]); + } + + fn update(&mut self, event: Event) { + if let Event::ModeUpdate(mode_info) = event { + self.mode_info = mode_info; + } } - fn draw(&mut self, _rows: usize, cols: usize) { - let help = get_help(); + fn render(&mut self, _rows: usize, cols: usize) { let superkey = superkey(); - let ctrl_keys = ctrl_keys(&help, cols - superkey.len); + let ctrl_keys = ctrl_keys(&self.mode_info, cols - superkey.len); let first_line = format!("{}{}", superkey, ctrl_keys); - let second_line = keybinds(&help, cols); + let second_line = keybinds(&self.mode_info, cols); // [48;5;238m is gray background, [0K is so that it fills the rest of the line // [48;5;16m is black background, [0K is so that it fills the rest of the line diff --git a/default-tiles/status-bar/src/second_line.rs b/default-tiles/status-bar/src/second_line.rs index 45c33a8e..ebcb93dd 100644 --- a/default-tiles/status-bar/src/second_line.rs +++ b/default-tiles/status-bar/src/second_line.rs @@ -1,6 +1,6 @@ // use colored::*; use ansi_term::{ANSIStrings, Style}; -use zellij_tile::*; +use zellij_tile::prelude::*; use crate::colors::{BLACK, GREEN, ORANGE, WHITE}; use crate::{LinePart, MORE_MSG}; @@ -97,7 +97,7 @@ fn select_pane_shortcut(is_first_shortcut: bool) -> LinePart { } } -fn full_shortcut_list(help: &Help) -> LinePart { +fn full_shortcut_list(help: &ModeInfo) -> LinePart { match help.mode { InputMode::Normal => LinePart::default(), InputMode::Locked => locked_interface_indication(), @@ -108,7 +108,7 @@ fn full_shortcut_list(help: &Help) -> LinePart { line_part.len += shortcut.len; line_part.part = format!("{}{}", line_part.part, shortcut,); } - let select_pane_shortcut = select_pane_shortcut(help.keybinds.len() == 0); + let select_pane_shortcut = select_pane_shortcut(help.keybinds.is_empty()); line_part.len += select_pane_shortcut.len; line_part.part = format!("{}{}", line_part.part, select_pane_shortcut,); line_part @@ -116,7 +116,7 @@ fn full_shortcut_list(help: &Help) -> LinePart { } } -fn shortened_shortcut_list(help: &Help) -> LinePart { +fn shortened_shortcut_list(help: &ModeInfo) -> LinePart { match help.mode { InputMode::Normal => LinePart::default(), InputMode::Locked => locked_interface_indication(), @@ -127,7 +127,7 @@ fn shortened_shortcut_list(help: &Help) -> LinePart { line_part.len += shortcut.len; line_part.part = format!("{}{}", line_part.part, shortcut,); } - let select_pane_shortcut = select_pane_shortcut(help.keybinds.len() == 0); + let select_pane_shortcut = select_pane_shortcut(help.keybinds.is_empty()); line_part.len += select_pane_shortcut.len; line_part.part = format!("{}{}", line_part.part, select_pane_shortcut,); line_part @@ -135,7 +135,7 @@ fn shortened_shortcut_list(help: &Help) -> LinePart { } } -fn best_effort_shortcut_list(help: &Help, max_len: usize) -> LinePart { +fn best_effort_shortcut_list(help: &ModeInfo, max_len: usize) -> LinePart { match help.mode { InputMode::Normal => LinePart::default(), InputMode::Locked => { @@ -159,7 +159,7 @@ fn best_effort_shortcut_list(help: &Help, max_len: usize) -> LinePart { line_part.len += shortcut.len; line_part.part = format!("{}{}", line_part.part, shortcut,); } - let select_pane_shortcut = select_pane_shortcut(help.keybinds.len() == 0); + let select_pane_shortcut = select_pane_shortcut(help.keybinds.is_empty()); if line_part.len + select_pane_shortcut.len <= max_len { line_part.len += select_pane_shortcut.len; line_part.part = format!("{}{}", line_part.part, select_pane_shortcut,); @@ -169,7 +169,7 @@ fn best_effort_shortcut_list(help: &Help, max_len: usize) -> LinePart { } } -pub fn keybinds(help: &Help, max_width: usize) -> LinePart { +pub fn keybinds(help: &ModeInfo, max_width: usize) -> LinePart { let full_shortcut_list = full_shortcut_list(help); if full_shortcut_list.len <= max_width { return full_shortcut_list; @@ -178,5 +178,5 @@ pub fn keybinds(help: &Help, max_width: usize) -> LinePart { if shortened_shortcut_list.len <= max_width { return shortened_shortcut_list; } - return best_effort_shortcut_list(help, max_width); + best_effort_shortcut_list(help, max_width) } diff --git a/default-tiles/strider/src/main.rs b/default-tiles/strider/src/main.rs index 3213b2af..c2867fe4 100644 --- a/default-tiles/strider/src/main.rs +++ b/default-tiles/strider/src/main.rs @@ -3,16 +3,51 @@ mod state; use colored::*; use state::{FsEntry, State}; use std::{cmp::min, fs::read_dir}; -use zellij_tile::*; +use zellij_tile::prelude::*; register_tile!(State); impl ZellijTile for State { - fn init(&mut self) { + fn load(&mut self) { refresh_directory(self); + subscribe(&[EventType::KeyPress]); } - fn draw(&mut self, rows: usize, cols: usize) { + fn update(&mut self, event: Event) { + if let Event::KeyPress(key) = event { + match key { + Key::Up | Key::Char('k') => { + *self.selected_mut() = self.selected().saturating_sub(1); + } + Key::Down | Key::Char('j') => { + let next = self.selected().saturating_add(1); + *self.selected_mut() = min(self.files.len() - 1, next); + } + Key::Right | Key::Char('\n') | Key::Char('l') => { + match self.files[self.selected()].clone() { + FsEntry::Dir(p, _) => { + self.path = p; + refresh_directory(self); + } + FsEntry::File(p, _) => open_file(&p), + } + } + Key::Left | Key::Char('h') => { + self.path.pop(); + refresh_directory(self); + } + + Key::Char('.') => { + self.toggle_hidden_files(); + refresh_directory(self); + } + + _ => (), + }; + } + } + + fn render(&mut self, rows: usize, cols: usize) { for i in 0..rows { if self.selected() < self.scroll() { *self.scroll_mut() = self.selected(); @@ -38,38 +73,6 @@ impl ZellijTile for State { } } } - - fn handle_key(&mut self, key: Key) { - match key { - Key::Up | Key::Char('k') => { - *self.selected_mut() = self.selected().saturating_sub(1); - } - Key::Down | Key::Char('j') => { - let next = self.selected().saturating_add(1); - *self.selected_mut() = min(self.files.len() - 1, next); - } - Key::Right | Key::Char('\n') | Key::Char('l') => { - match self.files[self.selected()].clone() { - FsEntry::Dir(p, _) => { - self.path = p; - refresh_directory(self); - } - FsEntry::File(p, _) => open_file(&p), - } - } - Key::Left | Key::Char('h') => { - self.path.pop(); - refresh_directory(self); - } - - Key::Char('.') => { - self.toggle_hidden_files(); - refresh_directory(self); - } - - _ => (), - }; - } } fn refresh_directory(state: &mut State) { diff --git a/default-tiles/strider/src/state.rs b/default-tiles/strider/src/state.rs index 2a9230e8..0d96ae0b 100644 --- a/default-tiles/strider/src/state.rs +++ b/default-tiles/strider/src/state.rs @@ -58,6 +58,6 @@ impl FsEntry { } pub fn is_hidden_file(&self) -> bool { - self.name().chars().nth(0).unwrap() == '.'.into() + self.name().starts_with('.') } } diff --git a/default-tiles/tab-bar/src/line.rs b/default-tiles/tab-bar/src/line.rs index beca8b42..1d1c33ec 100644 --- a/default-tiles/tab-bar/src/line.rs +++ b/default-tiles/tab-bar/src/line.rs @@ -57,7 +57,7 @@ fn left_more_message(tab_count_to_the_left: usize) -> LinePart { let more_text = if tab_count_to_the_left < 10000 { format!(" ← +{} ", tab_count_to_the_left) } else { - format!(" ← +many ") + " ← +many ".to_string() }; // 238 let more_text_len = more_text.chars().count() + 2; // 2 for the arrows @@ -84,7 +84,7 @@ fn right_more_message(tab_count_to_the_right: usize) -> LinePart { let more_text = if tab_count_to_the_right < 10000 { format!(" +{} → ", tab_count_to_the_right) } else { - format!(" +many → ") + " +many → ".to_string() }; let more_text_len = more_text.chars().count() + 1; // 2 for the arrow let left_separator = Style::new().fg(GRAY).on(ORANGE).paint(ARROW_SEPARATOR); @@ -130,7 +130,7 @@ fn add_next_tabs_msg( } fn tab_line_prefix() -> LinePart { - let prefix_text = format!(" Zellij "); + let prefix_text = " Zellij ".to_string(); let prefix_text_len = prefix_text.chars().count(); let prefix_styled_text = Style::new().fg(WHITE).on(GRAY).bold().paint(prefix_text); LinePart { diff --git a/default-tiles/tab-bar/src/main.rs b/default-tiles/tab-bar/src/main.rs index 8c025336..4a1f2933 100644 --- a/default-tiles/tab-bar/src/main.rs +++ b/default-tiles/tab-bar/src/main.rs @@ -1,7 +1,7 @@ mod line; mod tab; -use zellij_tile::*; +use zellij_tile::prelude::*; use crate::line::tab_line; use crate::tab::tab_style; @@ -12,25 +12,10 @@ pub struct LinePart { len: usize, } -#[derive(PartialEq)] -enum BarMode { - Normal, - Rename, -} - -impl Default for BarMode { - fn default() -> Self { - BarMode::Normal - } -} - #[derive(Default)] struct State { - active_tab_index: usize, - num_tabs: usize, - tabs: Vec<TabData>, - mode: BarMode, - new_name: String, + tabs: Vec<TabInfo>, + mode: InputMode, } static ARROW_SEPARATOR: &str = ""; @@ -49,17 +34,22 @@ pub mod colors { register_tile!(State); impl ZellijTile for State { - fn init(&mut self) { + fn load(&mut self) { set_selectable(false); set_invisible_borders(true); set_max_height(1); - self.active_tab_index = 0; - self.num_tabs = 0; - self.mode = BarMode::Normal; - self.new_name = String::new(); + subscribe(&[EventType::TabUpdate, EventType::ModeUpdate]); + } + + fn update(&mut self, event: Event) { + match event { + Event::ModeUpdate(mode_info) => self.mode = mode_info.mode, + Event::TabUpdate(tabs) => self.tabs = tabs, + _ => unimplemented!(), // FIXME: This should be unreachable, but this could be cleaner + } } - fn draw(&mut self, _rows: usize, cols: usize) { + fn render(&mut self, _rows: usize, cols: usize) { if self.tabs.is_empty() { return; } @@ -67,11 +57,9 @@ impl ZellijTile for State { let mut active_tab_index = 0; for t in self.tabs.iter_mut() { let mut tabname = t.name.clone(); - if t.active && self.mode == BarMode::Rename { - if self.new_name.is_empty() { + if t.active && self.mode == InputMode::RenameTab { + if tabname.is_empty() { tabname = String::from("Enter name..."); - } else { - tabname = self.new_name.clone(); } active_tab_index = t.position; } else if t.active { @@ -87,23 +75,4 @@ impl ZellijTile for State { } println!("{}\u{1b}[48;5;238m\u{1b}[0K", s); } - - fn update_tabs(&mut self) { - self.tabs = get_tabs(); - } - - fn handle_tab_rename_keypress(&mut self, key: Key) { - self.mode = BarMode::Rename; - match key { - Key::Char('\n') | Key::Esc => { - self.mode = BarMode::Normal; - self.new_name.clear(); - } - Key::Char(c) => self.new_name = format!("{}{}", self.new_name, c), - Key::Backspace | Key::Delete => { - self.new_name.pop(); - } - _ => {} - } - } } |
