From f1766553258881569fb46a5a3aa261b9bb0846ec Mon Sep 17 00:00:00 2001 From: Arijit Basu Date: Thu, 1 Apr 2021 19:34:45 +0530 Subject: [PATCH] Some improvements and cleanup --- Cargo.lock | 156 +++++++++++++++++--------------------------------- Cargo.toml | 5 +- README.md | 2 +- src/app.rs | 84 ++++++++++++++++++--------- src/config.rs | 17 ++++++ src/main.rs | 15 +++-- 6 files changed, 138 insertions(+), 141 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1196c51..4428c33 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,15 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "aho-corasick" -version = "0.7.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5" -dependencies = [ - "memchr", -] - [[package]] name = "arrayref" version = "0.3.6" @@ -110,9 +101,9 @@ checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" [[package]] name = "byteorder" -version = "1.4.2" +version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] name = "cassowary" @@ -260,9 +251,9 @@ dependencies = [ [[package]] name = "csv" -version = "1.1.5" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9d58633299b24b515ac72a3f869f8b91306a3cec616a602843a383acd6f9e97" +checksum = "22813a6dc45b335f9bade10bf7271dc477e81113e89eb251a0bc2a8a81c536e1" dependencies = [ "bstr", "csv-core", @@ -311,9 +302,9 @@ dependencies = [ [[package]] name = "dtoa" -version = "0.4.7" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88d7ed2934d741c6b37e33e3832298e8850b53fd2d2bea03873375596c7cea4e" +checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0" [[package]] name = "either" @@ -329,9 +320,9 @@ checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" [[package]] name = "generic-array" -version = "0.12.3" +version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec" +checksum = "ffdf9f34f1447443d37393cc6c2b8313aebddcd96906caf34e54c68d8e57d7bd" dependencies = [ "typenum", ] @@ -355,9 +346,9 @@ checksum = "62aca2aba2d62b4a7f5b33f3712cb1b0692779a56fb510499d5c0aa594daeaf3" [[package]] name = "handlebars" -version = "3.5.3" +version = "3.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb0867bbc5a3da37a753e78021d5fcf8a4db00e18dd2dd90fd36e24190e162d" +checksum = "580b6f551b29a3a02436318aed09ba1c58eea177dc49e39beac627ad356730a5" dependencies = [ "log", "pest", @@ -411,9 +402,9 @@ checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" [[package]] name = "js-sys" -version = "0.3.48" +version = "0.3.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc9f84f9b115ce7843d60706df1422a916680bfdfcbdb0447c5614ff9d7e4d78" +checksum = "2d99f9e3e84b8f67f846ef5b4cbbc3b1c29f6c759fcbce6f01aa0e73d932a24c" dependencies = [ "wasm-bindgen", ] @@ -426,9 +417,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.86" +version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" +checksum = "56d855069fafbb9b344c0f962150cd2c1187975cb1c22c1522c240d8c4986714" [[package]] name = "linked-hash-map" @@ -468,9 +459,9 @@ checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" [[package]] name = "memoffset" -version = "0.6.1" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "157b4208e3059a8f9e78d559edc658e13df41410cb3ae03979c83130067fdd87" +checksum = "f83fb6581e8ed1f85fd45c116db8405483899489e38406156c25eb743554361d" dependencies = [ "autocfg", ] @@ -493,9 +484,9 @@ dependencies = [ [[package]] name = "mio" -version = "0.7.9" +version = "0.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5dede4e2065b3842b8b0af444119f3aa331cc7cc2dd20388bfb0f5d5a38823a" +checksum = "cf80d3e903b34e0bd7282b218398aec54e082c840d9baf8339e0080a0c542956" dependencies = [ "libc", "log", @@ -506,11 +497,10 @@ dependencies = [ [[package]] name = "miow" -version = "0.3.6" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a33c1b55807fbed163481b5ba66db4b2fa6cde694a5027be10fb724206c5897" +checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" dependencies = [ - "socket2", "winapi", ] @@ -548,12 +538,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef" -[[package]] -name = "once_cell" -version = "1.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea78b9742c52ac729753c1590e9adc5248ea9bdaf974597efd46c74cfaa5fb54" - [[package]] name = "oorandom" version = "11.1.3" @@ -664,9 +648,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.24" +version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec" dependencies = [ "unicode-xid", ] @@ -748,14 +732,11 @@ dependencies = [ [[package]] name = "regex" -version = "1.4.3" +version = "1.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9251239e129e16308e70d853559389de218ac275b515068abc96829d05b948a" +checksum = "957056ecddbeba1b26965114e191d2e8589ce74db242b6ea25fc4062427a5c19" dependencies = [ - "aho-corasick", - "memchr", "regex-syntax", - "thread_local", ] [[package]] @@ -769,9 +750,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.6.22" +version = "0.6.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5eb417147ba9860a96cfe72a0b93bf88fee1744b5636ec99ab20c1aa9376581" +checksum = "24d5f089152e60f62d28b835fbff2cd2e8dc0baf1ac13343bef92ab7eed84548" [[package]] name = "rust-argon2" @@ -832,9 +813,9 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.123" +version = "1.0.125" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" +checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171" dependencies = [ "serde_derive", ] @@ -851,9 +832,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.123" +version = "1.0.125" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31" +checksum = "b093b7a2bb58203b5da3056c05b4ec1fed827dcfdb37347a8841695263b3d06d" dependencies = [ "proc-macro2", "quote", @@ -862,9 +843,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.62" +version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea1c6153794552ea7cf7cf63b1231a25de00ec90db326ba6264440fa08e31486" +checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" dependencies = [ "itoa", "ryu", @@ -895,16 +876,6 @@ dependencies = [ "opaque-debug", ] -[[package]] -name = "shellwords" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89e515aa4699a88148ed5ef96413ceef0048ce95b43fbc955a33bde0a70fcae6" -dependencies = [ - "lazy_static", - "regex", -] - [[package]] name = "signal-hook" version = "0.1.17" @@ -931,22 +902,11 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" -[[package]] -name = "socket2" -version = "0.3.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "122e570113d28d773067fab24266b66753f6ea915758651696b6e35e49f88d6e" -dependencies = [ - "cfg-if", - "libc", - "winapi", -] - [[package]] name = "syn" -version = "1.0.60" +version = "1.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" +checksum = "3ce15dd3ed8aa2f8eeac4716d6ef5ab58b6b9256db41d7e1a0224c2788e8fd87" dependencies = [ "proc-macro2", "quote", @@ -974,20 +934,11 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "thread_local" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8018d24e04c95ac8790716a5987d0fec4f8b27249ffa0f7d33f1369bdfb88cbd" -dependencies = [ - "once_cell", -] - [[package]] name = "tinytemplate" -version = "1.2.0" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2ada8616fad06a2d0c455adc530de4ef57605a8120cc65da9653e0e9623ca74" +checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" dependencies = [ "serde", "serde_json", @@ -1009,9 +960,9 @@ dependencies = [ [[package]] name = "typenum" -version = "1.12.0" +version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" +checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06" [[package]] name = "ucd-trie" @@ -1054,9 +1005,9 @@ checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" [[package]] name = "walkdir" -version = "2.3.1" +version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d" +checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" dependencies = [ "same-file", "winapi", @@ -1071,9 +1022,9 @@ checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" [[package]] name = "wasm-bindgen" -version = "0.2.71" +version = "0.2.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ee1280240b7c461d6a0071313e08f34a60b0365f14260362e5a2b17d1d31aa7" +checksum = "83240549659d187488f91f33c0f8547cbfef0b2088bc470c116d1d260ef623d9" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -1081,9 +1032,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.71" +version = "0.2.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b7d8b6942b8bb3a9b0e73fc79b98095a27de6fa247615e59d096754a3bc2aa8" +checksum = "ae70622411ca953215ca6d06d3ebeb1e915f0f6613e3b495122878d7ebec7dae" dependencies = [ "bumpalo", "lazy_static", @@ -1096,9 +1047,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.71" +version = "0.2.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5ac38da8ef716661f0f36c0d8320b89028efe10c7c0afde65baffb496ce0d3b" +checksum = "3e734d91443f177bfdb41969de821e15c516931c3c3db3d318fa1b68975d0f6f" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1106,9 +1057,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.71" +version = "0.2.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc053ec74d454df287b9374ee8abb36ffd5acb95ba87da3ba5b7d3fe20eb401e" +checksum = "d53739ff08c8a68b0fdbcd54c372b8ab800b1449ab3c9d706503bc7dd1621b2c" dependencies = [ "proc-macro2", "quote", @@ -1119,15 +1070,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.71" +version = "0.2.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d6f8ec44822dd71f5f221a5847fb34acd9060535c1211b70a05844c0f6383b1" +checksum = "d9a543ae66aa233d14bb765ed9af4a33e81b8b58d1584cf1b47ff8cd0b9e4489" [[package]] name = "web-sys" -version = "0.3.48" +version = "0.3.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec600b26223b2948cedfde2a0aa6756dcf1fef616f43d7b3097aaf53a6c4d92b" +checksum = "a905d57e488fec8861446d3393670fb50d27a262344013181c2cdf9fff5481be" dependencies = [ "js-sys", "wasm-bindgen", @@ -1166,7 +1117,7 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "xplr" -version = "0.2.2" +version = "0.2.3" dependencies = [ "criterion", "crossterm", @@ -1176,7 +1127,6 @@ dependencies = [ "serde", "serde_json", "serde_yaml", - "shellwords", "termion", "tui", ] diff --git a/Cargo.toml b/Cargo.toml index b335aae..38b46d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xplr" -version = "0.2.2" # Update app.rs +version = "0.2.3" # Update app.rs authors = ["Arijit Basu "] edition = "2018" description = "An experimental, minimal, configurable TUI file explorer, stealing ideas from nnn and fzf." @@ -19,8 +19,7 @@ dirs = "3.0.1" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" serde_yaml = "0.8" -handlebars = "3.5.3" -shellwords = "1.0.0" +handlebars = "3.5" mime_guess = "2.0.3" [dev-dependencies] diff --git a/README.md b/README.md index 0b1cb80..ef4f28f 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ cargo install xplr ```bash mkdir -p ~/.config/xplr -echo "#" | xplr | yq ".config" -y | tee ~/.config/xplr/config.yml +echo "PrintAppStateAndQuit" | xplr | yq ".config" -y | tee ~/.config/xplr/config.yml ``` 3. Check the key bindings in the config file. diff --git a/src/app.rs b/src/app.rs index 7e35e23..6b231a5 100644 --- a/src/app.rs +++ b/src/app.rs @@ -11,7 +11,7 @@ use std::collections::VecDeque; use std::fs; use std::path::PathBuf; -pub const VERSION: &str = "v0.2.1"; // Update Cargo.toml +pub const VERSION: &str = "v0.2.3"; // Update Cargo.toml pub const TEMPLATE_TABLE_ROW: &str = "TEMPLATE_TABLE_ROW"; @@ -25,14 +25,9 @@ pub struct PipesConfig { pub mode_out: String, } -impl Default for PipesConfig { - fn default() -> Self { - let pipesdir = dirs::runtime_dir() - .unwrap_or("/tmp".into()) - .join("xplr") - .join("session") - .join(std::process::id().to_string()) - .join("pipe"); +impl PipesConfig { + fn from_runtime_path(path: &String) -> Self { + let pipesdir = PathBuf::from(path).join("pipe"); fs::create_dir_all(&pipesdir).unwrap(); @@ -168,6 +163,7 @@ pub enum InternalMsg { #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] pub enum ExternalMsg { Refresh, + ClearScreen, FocusNext, FocusNextByRelativeIndex(usize), FocusNextByRelativeIndexFromInput, @@ -212,6 +208,7 @@ pub struct Command { #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] pub enum MsgOut { Refresh, + ClearScreen, PrintResultAndQuit, PrintAppStateAndQuit, Debug(String), @@ -255,27 +252,46 @@ pub struct App { msg_out: VecDeque, mode: Mode, input_buffer: Option, + pid: u32, + runtime_path: String, pipes: PipesConfig, } impl App { - pub fn new(config: Config, pwd: String) -> Self { - let mode = config - .modes - .get(&"default".to_string()) - .map(|k| k.to_owned()) - .unwrap_or_default(); - - Self { - config, - pwd, - directory_buffers: Default::default(), - tasks: Default::default(), - selected: Default::default(), - msg_out: Default::default(), - mode, - input_buffer: Default::default(), - pipes: Default::default(), + pub fn create(config: Config, pwd: String) -> Result { + if config.version != VERSION { + Err(Error::IncompatibleVersion( + "incompatible config version".into(), + )) + } else { + let mode = config + .modes + .get(&"default".to_string()) + .map(|k| k.to_owned()) + .unwrap_or_default(); + + let pid = std::process::id(); + let runtime_path = dirs::runtime_dir() + .unwrap_or("/tmp".into()) + .join("xplr") + .join("session") + .join(&pid.to_string()) + .to_string_lossy() + .to_string(); + + Ok(Self { + config, + pwd, + directory_buffers: Default::default(), + tasks: Default::default(), + selected: Default::default(), + msg_out: Default::default(), + mode, + input_buffer: Default::default(), + pid, + runtime_path: runtime_path.clone(), + pipes: PipesConfig::from_runtime_path(&runtime_path), + }) } } @@ -309,6 +325,7 @@ impl App { fn handle_external(self, msg: ExternalMsg, key: Option) -> Result { match msg { ExternalMsg::Refresh => self.refresh(), + ExternalMsg::ClearScreen => self.clear_screen(), ExternalMsg::FocusFirst => self.focus_first(), ExternalMsg::FocusLast => self.focus_last(), ExternalMsg::FocusPrevious => self.focus_previous(), @@ -376,6 +393,11 @@ impl App { Ok(self) } + fn clear_screen(mut self) -> Result { + self.msg_out.push_back(MsgOut::ClearScreen); + Ok(self) + } + fn focus_first(mut self) -> Result { if let Some(dir) = self.directory_buffer_mut() { dir.focus = 0; @@ -638,4 +660,14 @@ impl App { pub fn pipes(&self) -> &PipesConfig { &self.pipes } + + /// Get a reference to the app's pid. + pub fn pid(&self) -> &u32 { + &self.pid + } + + /// Get a reference to the app's runtime path. + pub fn runtime_path(&self) -> &String { + &self.runtime_path + } } diff --git a/src/config.rs b/src/config.rs index 29f0520..61241b6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -312,6 +312,23 @@ impl Default for KeyBindings { messages: - PrintResultAndQuit + o: + help: open + messages: + - Call: + command: bash + args: + - -c + - xdg-open "${XPLR_FOCUS_PATH:?}" &> /dev/null + - ClearScreen + - Refresh + + ctrl-l: + help: clear + messages: + - ClearScreen + - Refresh + "#": help: quit with debug messages: diff --git a/src/main.rs b/src/main.rs index 3641229..e341389 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,7 @@ use crossterm::event::Event; use crossterm::terminal as term; use crossterm::{event, execute}; -use handlebars::{handlebars_helper, Handlebars}; -use shellwords; +use handlebars::Handlebars; use std::env; use std::fs; use std::io; @@ -21,9 +20,6 @@ use xplr::explorer; use xplr::input::Key; use xplr::ui; -handlebars_helper!(shellescape: |v: str| format!("{}", shellwords::escape(v))); -handlebars_helper!(readfile: |v: str| fs::read_to_string(v).unwrap_or_default()); - fn main() -> Result<(), Error> { let mut pwd = PathBuf::from(env::args().skip(1).next().unwrap_or(".".into())) .canonicalize() @@ -52,11 +48,9 @@ fn main() -> Result<(), Error> { Config::default() }; - let mut app = app::App::new(config, pwd.clone()); + let mut app = app::App::create(config, pwd.clone())?; let mut hb = Handlebars::new(); - hb.register_helper("shellescape", Box::new(shellescape)); - hb.register_helper("readfile", Box::new(readfile)); hb.register_template_string( app::TEMPLATE_TABLE_ROW, &app.config() @@ -157,6 +151,10 @@ fn main() -> Result<(), Error> { break 'outer; } + app::MsgOut::ClearScreen => { + terminal.clear()?; + } + app::MsgOut::Refresh => { if app.pwd() != &last_pwd { explorer::explore( @@ -242,6 +240,7 @@ fn main() -> Result<(), Error> { .env("XPLR_FOCUS_PATH", focus_path) .env("XPLR_FOCUS_INDEX", focus_index) .env("XPLR_SELECTED", selected) + .env("XPLR_RUNTIME_PATH", app.runtime_path()) .env("XPLR_PIPE_MSG_IN", pipe_msg_in) .env("XPLR_PIPE_SELECTED_OUT", pipe_selected_out) .env("XPLR_PIPE_FOCUS_OUT", pipe_focus_out)