From a264033cfa0a6f7390f9cfc6adb1958eb2c9d754 Mon Sep 17 00:00:00 2001 From: Jeff Zhao Date: Sat, 10 Sep 2022 21:46:56 -0400 Subject: [PATCH] code cleanup --- lib/dizi_lib/src/request/client.rs | 1 - src/client/event/process_event.rs | 2 +- src/client/key_command/constants.rs | 78 +++++++++++++++++++--------- src/client/ui/views/tui_textfield.rs | 19 +++++-- src/server/audio/symphonia/stream.rs | 4 +- 5 files changed, 72 insertions(+), 32 deletions(-) diff --git a/lib/dizi_lib/src/request/client.rs b/lib/dizi_lib/src/request/client.rs index c2d69b2..02bca8a 100644 --- a/lib/dizi_lib/src/request/client.rs +++ b/lib/dizi_lib/src/request/client.rs @@ -1,5 +1,4 @@ use std::path::PathBuf; -use std::time; use serde_derive::{Deserialize, Serialize}; diff --git a/src/client/event/process_event.rs b/src/client/event/process_event.rs index 8850439..fe1fb3a 100644 --- a/src/client/event/process_event.rs +++ b/src/client/event/process_event.rs @@ -259,7 +259,7 @@ pub fn process_noninteractive(event: AppEvent, context: &mut AppContext) { pub fn process_dir_preview( context: &mut AppContext, - path: path::PathBuf, + _path: path::PathBuf, res: io::Result, ) { match res { diff --git a/src/client/key_command/constants.rs b/src/client/key_command/constants.rs index aa40acf..06b79d5 100644 --- a/src/client/key_command/constants.rs +++ b/src/client/key_command/constants.rs @@ -1,36 +1,64 @@ -pub const CMD_HELP: &str = "help"; +use rustyline::completion::Pair; -pub const CMD_CLOSE: &str = "close"; - -pub const CMD_CHANGE_DIRECTORY: &str = "cd"; pub const CMD_COMMAND_LINE: &str = ":"; -pub const CMD_CURSOR_MOVE_UP: &str = "cursor_move_up"; -pub const CMD_CURSOR_MOVE_DOWN: &str = "cursor_move_down"; -pub const CMD_CURSOR_MOVE_HOME: &str = "cursor_move_home"; -pub const CMD_CURSOR_MOVE_END: &str = "cursor_move_end"; -pub const CMD_CURSOR_MOVE_PAGEUP: &str = "cursor_move_page_up"; -pub const CMD_CURSOR_MOVE_PAGEDOWN: &str = "cursor_move_page_down"; +macro_rules! cmd_constants { + ($( ($cmd_name:ident, $cmd_value:literal), )*) => { + $( + pub const $cmd_name: &str = $cmd_value; + )* + + pub fn commands() -> Vec<&'static str> { + vec![$($cmd_value,)*] + } + }; +} + +cmd_constants![ + (CMD_CLOSE, "close"), + + (CMD_CHANGE_DIRECTORY, "cd"), + + (CMD_CURSOR_MOVE_UP, "cursor_move_up"), + (CMD_CURSOR_MOVE_DOWN, "cursor_move_down"), + (CMD_CURSOR_MOVE_HOME, "cursor_move_home"), + (CMD_CURSOR_MOVE_END, "cursor_move_end"), + (CMD_CURSOR_MOVE_PAGEUP, "cursor_move_page_up"), + (CMD_CURSOR_MOVE_PAGEDOWN, "cursor_move_page_down"), + + (CMD_GO_TO_PLAYING, "go_to_playing"), + + (CMD_OPEN_FILE, "open"), + + (CMD_PARENT_DIRECTORY, "cd .."), + (CMD_RELOAD_DIRECTORY_LIST, "reload_dirlist"), -pub const CMD_GO_TO_PLAYING: &str = "go_to_playing"; + (CMD_SEARCH_STRING, "search"), + (CMD_SEARCH_GLOB, "search_glob"), + (CMD_SEARCH_SKIM, "search_skim"), + (CMD_SEARCH_NEXT, "search_next"), + (CMD_SEARCH_PREV, "search_prev"), -pub const CMD_OPEN_FILE: &str = "open"; + (CMD_SELECT_FILES, "select"), -pub const CMD_PARENT_DIRECTORY: &str = "cd .."; -pub const CMD_RELOAD_DIRECTORY_LIST: &str = "reload_dirlist"; + (CMD_SERVER_REQUEST, "server_request"), -pub const CMD_SEARCH_STRING: &str = "search"; -pub const CMD_SEARCH_GLOB: &str = "search_glob"; -pub const CMD_SEARCH_SKIM: &str = "search_skim"; -pub const CMD_SEARCH_NEXT: &str = "search_next"; -pub const CMD_SEARCH_PREV: &str = "search_prev"; + (CMD_SORT, "sort"), + (CMD_SORT_REVERSE, "sort reverse"), -pub const CMD_SELECT_FILES: &str = "select"; + (CMD_TOGGLE_HIDDEN, "toggle_hidden"), + (CMD_TOGGLE_VIEW, "toggle_view"), +]; -pub const CMD_SERVER_REQUEST: &str = "server_request"; -pub const CMD_SORT: &str = "sort"; -pub const CMD_SORT_REVERSE: &str = "sort reverse"; -pub const CMD_TOGGLE_HIDDEN: &str = "toggle_hidden"; -pub const CMD_TOGGLE_VIEW: &str = "toggle_view"; +pub fn complete_command(partial_command: &str) -> Vec { + commands() + .into_iter() + .filter(|command| command.starts_with(partial_command)) + .map(|command| Pair { + display: command.to_string(), + replacement: command.to_string(), + }) + .collect() +} diff --git a/src/client/ui/views/tui_textfield.rs b/src/client/ui/views/tui_textfield.rs index 4cbe53d..62f2fed 100644 --- a/src/client/ui/views/tui_textfield.rs +++ b/src/client/ui/views/tui_textfield.rs @@ -14,7 +14,7 @@ use unicode_width::UnicodeWidthStr; use crate::context::AppContext; use crate::event::process_event; use crate::event::AppEvent; -use crate::key_command::Command; +use crate::key_command::{complete_command}; use crate::ui::views::TuiView; use crate::ui::widgets::{TuiMenu, TuiMultilineText}; use crate::ui::AppBackend; @@ -260,7 +260,9 @@ impl<'a> TuiTextField<'a> { Key::Char('\n') => { break; } - Key::Char(c) => line_buffer.insert(c, 1).is_some(), + Key::Char(c) => { + line_buffer.insert(c, 1).is_some() + } _ => false, }; if dirty { @@ -370,6 +372,17 @@ fn get_candidates( line_buffer: &mut LineBuffer, ) -> Option<(usize, Vec)> { let line = line_buffer.as_str().split_once(' '); - let res = completer.complete_path(line_buffer.as_str(), line_buffer.pos()); + let res = match line { + None => Ok((0, complete_command(line_buffer.as_str()))), + + Some((command, _files)) => { + // We want to autocomplete a command if we are inside it. + if line_buffer.pos() <= command.len() { + Ok((0, complete_command(command))) + } else { + completer.complete_path(line_buffer.as_str(), line_buffer.pos()) + } + } + }; res.ok() } diff --git a/src/server/audio/symphonia/stream.rs b/src/server/audio/symphonia/stream.rs index 166ea61..9d002ec 100644 --- a/src/server/audio/symphonia/stream.rs +++ b/src/server/audio/symphonia/stream.rs @@ -1,7 +1,7 @@ use std::path::Path; use std::sync::mpsc; -use std::thread::{self, JoinHandle}; -use std::time::{Duration, SystemTime}; +use std::thread; +use std::time::Duration; use log::{debug, log_enabled, Level}; use symphonia::core::codecs::{DecoderOptions, CODEC_TYPE_NULL};