diff options
-rw-r--r-- | Cargo.lock | 33 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/app.rs | 46 | ||||
-rw-r--r-- | src/commands.rs | 32 | ||||
-rw-r--r-- | src/conf.rs | 14 | ||||
-rw-r--r-- | src/external.rs | 10 | ||||
-rw-r--r-- | src/main.rs | 8 | ||||
-rw-r--r-- | src/verbs.rs | 32 |
8 files changed, 146 insertions, 30 deletions
@@ -10,6 +10,7 @@ dependencies = [ name = "btree" version = "0.1.0" dependencies = [ + "directories 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -21,6 +22,15 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "directories" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "lazy_static" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -106,9 +116,29 @@ name = "version_check" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "winapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [metadata] "checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e" "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" +"checksum directories 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "72d337a64190607d4fcca2cb78982c5dd57f4916e19696b48a575fa746b6cb0f" "checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1" "checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d" "checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16" @@ -121,3 +151,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" "checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737" "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" +"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" @@ -7,3 +7,4 @@ authors = ["Canop <cano.petrole@gmail.com>"] termion = "1.5.1" regex = "1" lazy_static = "1.2" +directories = "1.0" @@ -11,6 +11,8 @@ use tree_build::TreeBuilder; use input::{Input}; use status::{Status}; use tree_views::TreeView; +use external; +use verbs::VerbStore; enum AppStateCmdResult { Quit, @@ -23,8 +25,21 @@ pub struct AppState { pub tree: Tree, } +pub struct App { + pub w: u16, + pub h: u16, + pub states: Vec<AppState>, // stack: the last one is current + verb_store: VerbStore, +} + +pub struct Screen { + pub w: u16, + pub h: u16, + pub stdout: AlternateScreen<RawTerminal<io::Stdout>>, +} + impl AppState { - fn apply(&mut self, cmd: &mut Command) -> AppStateCmdResult { + fn apply(&mut self, cmd: &mut Command, app: &App) -> AppStateCmdResult { match &cmd.action { Action::Back => { AppStateCmdResult::PopState @@ -50,10 +65,21 @@ impl AppState { }, false => { println!("opening file"); + external::open_file(&self.tree.lines[self.tree.selection].path); AppStateCmdResult::Keep }, } }, + Action::VerbSelection(verb_key) => { + match app.verb_store.file_verb(&verb_key) { + Some(verb) => { + // TODO + }, + None => { + // TODO + } + } + }, Action::Quit => { AppStateCmdResult::Quit }, @@ -64,18 +90,6 @@ impl AppState { } } -pub struct App { - pub w: u16, - pub h: u16, - pub states: Vec<AppState>, // stack: the last one is current -} - -pub struct Screen { - pub w: u16, - pub h: u16, - pub stdout: AlternateScreen<RawTerminal<io::Stdout>>, -} - impl Screen { pub fn new(w: u16, h:u16) -> io::Result<Screen> { let stdout = AlternateScreen::from(stdout().into_raw_mode()?); @@ -97,8 +111,9 @@ impl App { pub fn new() -> io::Result<App> { let (w, h) = termion::terminal_size()?; let states = Vec::new(); + let verb_store = VerbStore::new(); Ok(App { - w, h, states + w, h, states, verb_store, }) } @@ -140,6 +155,7 @@ impl App { let mut cmd = Command::new(); for c in keys { cmd.add_key(c?)?; + screen.write_status_text(&format!("{:?}", &cmd.action))?; match self.mut_state().apply(&mut cmd) { AppStateCmdResult::Quit => { break; @@ -157,7 +173,7 @@ impl App { } let state = self.state(); screen.write_tree(&state.tree)?; - screen.write_status(&state)?; + //screen.write_status(&state)?; // TODO pass cmd too screen.writeInput(&cmd)?; } Ok(()) diff --git a/src/commands.rs b/src/commands.rs index a82c3e7..20ed9f3 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -5,16 +5,19 @@ use termion::event::Key; #[derive(Debug)] pub enum Action { - MoveSelection(i16), // up (neg) or down (positive) in the list - Select(String), // select by key - OpenSelection, // open the selected line (which can't be the root by construct) - Back, // back to last app state + MoveSelection(i16), // up (neg) or down (positive) in the list + Select(String), // select by key + OpenSelection, // open the selected line (which can't be the root by construct) + NudeVerb(String), // verb without selection + NudeVerbEdit(String), // verb without selection, unfinished + VerbSelection(String), // verb without selection + VerbSelectionEdit(String), // verb without selection, unfinished + Back, // back to last app state Quit, - Unparsed, // or unparsable + Unparsed, // or unparsable } impl Action { - // Only makes sense when there was no special key pub fn from(raw: &str, finished: bool) -> Action { lazy_static! { static ref RE: Regex = Regex::new(r"(?x) @@ -26,21 +29,20 @@ impl Action { } match RE.captures(raw) { Some(c) => { - let key = match c.name("key") { - Some(key) => String::from(key.as_str()), - None => String::from(""), // should not happen - }; - // TODO handle verb - match finished { - true => Action::OpenSelection, - false => Action::Select(key), + match (c.name("key"), c.name("verb"), finished) { + (Some(key), Some(verb), false) => Action::VerbSelectionEdit(String::from(verb.as_str())), + (Some(key), Some(verb), true) => Action::VerbSelection(String::from(verb.as_str())), + (Some(key), None, false) => Action::Select(String::from(key.as_str())), + (Some(key), None, true) => Action::OpenSelection, + (None, Some(verb), false) => Action::NudeVerbEdit(String::from(verb.as_str())), + (None, Some(verb), true) => Action::NudeVerb(String::from(verb.as_str())), + _ => Action::Unparsed, // exemple: finishes with a space } }, None => { Action::Unparsed } } - } } diff --git a/src/conf.rs b/src/conf.rs new file mode 100644 index 0000000..89427f9 --- /dev/null +++ b/src/conf.rs @@ -0,0 +1,14 @@ + + +// what's needed to handle a verb +#[derive(Debug)] +pub struct VerbConf { + +} + + +// The parsed sanitized conf +#[derive(Debug)] +pub struct Conf { + +} diff --git a/src/external.rs b/src/external.rs new file mode 100644 index 0000000..a2911a3 --- /dev/null +++ b/src/external.rs @@ -0,0 +1,10 @@ +use std::process::Command; +use std::path::{PathBuf}; + +pub fn open_file(path: &PathBuf) { + Command::new("xdg-open") + .arg(String::from(path.to_string_lossy())) + .spawn() + .expect("xdg-open failed to start"); +} + diff --git a/src/main.rs b/src/main.rs index 67ae002..7873dc2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,24 +3,32 @@ extern crate lazy_static; extern crate regex; extern crate termion; +extern crate directories; mod app; mod commands; +mod external; mod flat_tree; mod tree_build; mod input; mod status; mod tree_views; +mod verbs; use app::App; use std::env; use std::path::{PathBuf}; use std::io; use tree_build::{TreeBuilder}; +use directories::{ProjectDirs}; const SHOW_APP: bool = true; fn main() -> io::Result<()> { + if let Some(proj_dirs) = ProjectDirs::from("org", "dystroy", "btree") { + println!("conf dir: {:?}", proj_dirs.config_dir()); + } + let args: Vec<String> = env::args().collect(); let path = match args.len() >= 2 { true => PathBuf::from(&args[1]), diff --git a/src/verbs.rs b/src/verbs.rs new file mode 100644 index 0000000..9c05800 --- /dev/null +++ b/src/verbs.rs @@ -0,0 +1,32 @@ +use std::collections::HashMap; + +#[derive(Debug)] +pub struct Verb { + name: String, + exec_pattern: String, +} + +pub struct VerbStore { + file_verbs: HashMap<&'static str, Verb>, +} + +impl VerbStore { + pub fn new() -> VerbStore { + VerbStore { + file_verbs: HashMap::new(), + } + } + fn add_file_verb(&mut self, verb_key: &'static str, name: &str, exec_pattern: &str) { + self.file_verbs.insert(verb_key, Verb { + name: name.to_owned(), + exec_pattern: exec_pattern.to_owned(), + }); + + } + pub fn set_defaults(&mut self) { + self.add_file_verb("e", "edit", "nvim {file}"); + } + pub fn file_verb(&self, verb_key: &str) -> Option<&Verb> { + self.file_verbs.get(verb_key) + } +} |