From f42ab48281bd36dda9ba0511fb55dfbbf86ff566 Mon Sep 17 00:00:00 2001 From: Harry Cheng Date: Tue, 16 Aug 2022 13:10:40 +0800 Subject: [PATCH] `no_ui` option --- src/main.rs | 35 +++++++++++++++++++++++++++++++++++ src/settings.rs | 10 ++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/main.rs b/src/main.rs index 5d07bc9e..039d0da7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,6 +50,41 @@ fn handle_addition(settings: &Settings) { fn handle_search(settings: &Settings) { let history = History::load(settings.history_format); + + if settings.no_ui { + history.build_cache_table( + &settings.dir.to_owned(), + &Some(settings.session_id.to_owned()), + None, + None, + None, + settings.limit.to_owned(), + ); + let matches = history.find_matches( + &settings.command, + settings.results as i16, + settings.fuzzy, + &mcfly::settings::ResultSort::Rank, + ); + + let cmd = match matches.first() { + Some(cmd) => cmd.cmd.to_owned(), + None => settings.command.to_owned(), + }; + let cmd_t = cmd.trim(); + if cmd_t.is_empty() { + return; + } + + let out = format!("mode display\ncommandline {}\n", cmd_t); + if let Some(path) = &settings.output_selection { + fs::write(path, out) + .unwrap_or_else(|err| panic!("McFly error: unable to write to {}: {}", path, err)); + } + + return; + } + let result = Interface::new(settings, &history).display(); if let Some(cmd) = result.selection { if let Some(path) = &settings.output_selection { diff --git a/src/settings.rs b/src/settings.rs index 0dc4cacf..a881b5b0 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -87,6 +87,7 @@ pub struct Settings { pub interface_view: InterfaceView, pub result_sort: ResultSort, pub disable_menu: bool, + pub no_ui: bool, } impl Default for Settings { @@ -116,6 +117,7 @@ impl Default for Settings { interface_view: InterfaceView::Top, result_sort: ResultSort::Rank, disable_menu: false, + no_ui: false, } } } @@ -204,6 +206,9 @@ impl Settings { .arg(Arg::with_name("delete_without_confirm") .long("delete_without_confirm") .help("Delete entry without confirm")) + .arg(Arg::with_name("no_ui") + .long("no-ui") + .help("Don't show the UI; requires --output-selection")) .arg(Arg::with_name("output_selection") .short("o") .long("output-selection") @@ -419,6 +424,11 @@ impl Settings { settings.output_selection = search_matches .value_of("output_selection") .map(|s| s.to_owned()); + settings.no_ui = search_matches.is_present("no_ui"); + + if settings.no_ui && settings.output_selection.is_none() { + panic!("McFly error: no_ui requires output_selection"); + } if let Some(values) = search_matches.values_of("command") { settings.command = values.collect::>().join(" ");