summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff Zhao <jeff.no.zhao@gmail.com>2022-04-22 16:53:04 -0400
committerJeff Zhao <jeff.no.zhao@gmail.com>2022-04-22 16:53:04 -0400
commit9cc4f10fdb3e31723289892be5929ccd0a1c48d7 (patch)
tree29fed413a9236b546b477a521e21eb6c9d2bf3f4
parent763815418ac3acd56aa7e936c48861ae6e73ca40 (diff)
combine --output-dir and --choosefiles into a single argument
--output-file is used for all output by joshuto and can be specified what to output by: `quit --output-current-directory` `quit --output-selected-files`
-rw-r--r--config/keymap.toml2
-rw-r--r--docs/configuration/keymap.toml.md38
-rw-r--r--src/commands/open_file.rs5
-rw-r--r--src/commands/quit.rs44
-rw-r--r--src/commands/tab_ops.rs4
-rw-r--r--src/context/app_context.rs14
-rw-r--r--src/key_command/command.rs6
-rw-r--r--src/key_command/constants.rs2
-rw-r--r--src/key_command/impl_appcommand.rs4
-rw-r--r--src/key_command/impl_appexecute.rs4
-rw-r--r--src/key_command/impl_comment.rs4
-rw-r--r--src/key_command/impl_from_str.rs18
-rw-r--r--src/main.rs37
-rw-r--r--src/run.rs5
14 files changed, 97 insertions, 90 deletions
diff --git a/config/keymap.toml b/config/keymap.toml
index 1361086..21d3a4b 100644
--- a/config/keymap.toml
+++ b/config/keymap.toml
@@ -4,7 +4,7 @@ mapcommand = [
{ keys = [ "W" ], command = "close_tab" },
{ keys = [ "ctrl+w" ], command = "close_tab" },
{ keys = [ "q" ], command = "close_tab" },
- { keys = [ "Q" ], command = "quit_to_cwd" },
+ { keys = [ "Q" ], command = "quit --output-current-directory" },
{ keys = [ "R" ], command = "reload_dirlist" },
{ keys = [ "z", "h" ], command = "toggle_hidden" },
diff --git a/docs/configuration/keymap.toml.md b/docs/configuration/keymap.toml.md
index c4fd8e4..db1f001 100644
--- a/docs/configuration/keymap.toml.md
+++ b/docs/configuration/keymap.toml.md
@@ -56,22 +56,34 @@ f12
## General
- `quit`: quit joshuto
- will not quit if there are pending IO work (paste jobs)
+ - `quit --force`: does ***NOT*** wait for pending IO work
+ - `quit --output-current-directory`: if `--output-file` argument is set, output the current directory to it
+ - `quit --output-selected-files`: if `--output-file` argument is set, output the selected files to it
- - `force_quit`: forcefully quits joshuto
- - will ***NOT*** wait for IO operations to complete
-
- - `quit_to_cwd`: quits joshuto and writes the current directory to a file specified by `--lastdir` command line option
- - works with the following bash function:
+`quit` works best with the following bash script
```bash
function joshuto() {
- TIMESTAMP=$(date '+%Y%m%d-%H%M%S')
- CWD_FILE="/tmp/$USER/joshuto-cwd-$TIMESTAMP"
- env joshuto --last-dir "$CWD_FILE" $@
-
- if [ -e "$CWD_FILE" ]; then
- JOSHUTO_CWD=$(cat "$CWD_FILE")
- rm "$CWD_FILE" && cd "$JOSHUTO_CWD"
- fi
+ ID="$$"
+ OUTPUT_FILE="/tmp/$USER/joshuto-cwd-$ID"
+ env joshuto --output-file "$OUTPUT_FILE" $@
+ exit_code=$?
+
+ case "$exit_code" in
+ # regular exit
+ 0)
+ ;;
+ # output contains current directory
+ 101)
+ JOSHUTO_CWD=$(cat "$OUTPUT_FILE")
+ cd "$JOSHUTO_CWD"
+ ;;
+ # output selected files
+ 102)
+ ;;
+ *)
+ echo "Exit code: $exit_code"
+ ;;
+ esac
}
```
diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs
index bf89627..48c0d0b 100644
--- a/src/commands/open_file.rs
+++ b/src/commands/open_file.rs
@@ -3,7 +3,7 @@ use std::path;
use crate::commands::reload;
use crate::config::AppMimetypeEntry;
-use crate::context::{AppContext, QuitType};
+use crate::context::AppContext;
use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult};
use crate::ui::views::TuiTextField;
use crate::ui::TuiBackend;
@@ -35,9 +35,6 @@ pub fn open(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult
change_directory::cd(path.as_path(), context)?;
reload::soft_reload(context.tab_context_ref().index, context)?;
}
- Some(_) if context.args.choosefiles.is_some() => {
- context.quit = QuitType::ChooseFiles;
- }
Some(_) => {
let paths = curr_list.map_or_else(Vec::new, |s| s.get_selected_paths());
let path = paths.get(0).ok_or_else(|| {
diff --git a/src/commands/quit.rs b/src/commands/quit.rs
index 0d3d31a..ccc9a8b 100644
--- a/src/commands/quit.rs
+++ b/src/commands/quit.rs
@@ -1,22 +1,35 @@
use std::io;
-use crate::context::{AppContext, QuitType};
+use crate::context::AppContext;
use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult};
-pub fn quit(context: &mut AppContext) -> JoshutoResult {
- let worker_context = context.worker_context_ref();
- if worker_context.is_busy() || !worker_context.is_empty() {
- Err(JoshutoError::new(
- JoshutoErrorKind::Io(io::ErrorKind::Other),
- String::from("operations running in background, use force_quit to quit"),
- ))
- } else {
- context.quit = QuitType::Normal;
- Ok(())
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub enum QuitAction {
+ DoNot,
+ Noop,
+ Force,
+ OutputCurrentDirectory,
+ OutputSelectedFiles,
+}
+
+impl QuitAction {
+ pub const fn exit_code(&self) -> i32 {
+ match *self {
+ Self::Noop => 0,
+ Self::DoNot => 10,
+ Self::Force => 100,
+ Self::OutputCurrentDirectory => 101,
+ Self::OutputSelectedFiles => 102,
+ }
}
}
-pub fn quit_to_current_directory(context: &mut AppContext) -> JoshutoResult {
+pub fn quit_with_action(context: &mut AppContext, quit_action: QuitAction) -> JoshutoResult {
+ if quit_action == QuitAction::Force {
+ context.quit = quit_action;
+ return Ok(());
+ }
+
let worker_context = context.worker_context_ref();
if worker_context.is_busy() || !worker_context.is_empty() {
Err(JoshutoError::new(
@@ -24,12 +37,7 @@ pub fn quit_to_current_directory(context: &mut AppContext) -> JoshutoResult {
String::from("operations running in background, use force_quit to quit"),
))
} else {
- context.quit = QuitType::ToCurrentDirectory;
+ context.quit = quit_action;
Ok(())
}
}
-
-pub fn force_quit(context: &mut AppContext) -> JoshutoResult {
- context.quit = QuitType::Force;
- Ok(())
-}
diff --git a/src/commands/tab_ops.rs b/src/commands/tab_ops.rs
index ef87e7a..32bfeb1 100644
--- a/src/commands/tab_ops.rs
+++ b/src/commands/tab_ops.rs
@@ -7,7 +7,7 @@ use crate::tab::{JoshutoTab, TabHomePage};
use crate::HOME_DIR;
-use super::quit;
+use super::quit::{quit_with_action, QuitAction};
fn _tab_switch(new_index: usize, context: &mut AppContext) -> std::io::Result<()> {
context.tab_context_mut().index = new_index;
@@ -110,7 +110,7 @@ pub fn new_tab(context: &mut AppContext) -> JoshutoResult {
pub fn close_tab(context: &mut AppContext) -> JoshutoResult {
if context.tab_context_ref().len() <= 1 {
- return quit::quit(context);
+ return quit_with_action(context, QuitAction::Noop);
}
let mut tab_index = context.tab_context_ref().index;
diff --git a/src/context/app_context.rs b/src/context/app_context.rs
index 4427611..b2b70e1 100644
--- a/src/context/app_context.rs
+++ b/src/context/app_context.rs
@@ -4,6 +4,7 @@ use std::sync::mpsc;
use std::thread;
use tui::layout::Rect;
+use crate::commands::quit::QuitAction;
use crate::config;
use crate::context::{
CommandLineContext, LocalStateContext, MessageQueue, PreviewContext, TabContext, WorkerContext,
@@ -16,22 +17,13 @@ use crate::Args;
use notify::{RecursiveMode, Watcher};
use std::path;
-#[derive(Clone, Copy, Debug, PartialEq)]
-pub enum QuitType {
- DoNot,
- Normal,
- Force,
- ToCurrentDirectory,
- ChooseFiles,
-}
-
#[derive(Clone, Debug, PartialEq)]
pub struct UiContext {
pub layout: Vec<Rect>,
}
pub struct AppContext {
- pub quit: QuitType,
+ pub quit: QuitAction,
// event loop querying
pub events: Events,
// args from the command line
@@ -81,7 +73,7 @@ impl AppContext {
let watched_paths = HashSet::with_capacity(3);
Self {
- quit: QuitType::DoNot,
+ quit: QuitAction::DoNot,
events,
args,
tab_context: TabContext::new(),
diff --git a/src/key_command/command.rs b/src/key_command/command.rs
index 0829a09..77f2316 100644
--- a/src/key_command/command.rs
+++ b/src/key_command/command.rs
@@ -1,5 +1,6 @@
use std::path;
+use crate::commands::quit::QuitAction;
use crate::config::option::{LineNumberStyle, SelectOption, SortType};
use crate::io::IoWorkerOptions;
@@ -45,9 +46,8 @@ pub enum Command {
OpenFile,
OpenFileWith(Option<usize>),
- Quit,
- QuitToCurrentDirectory,
- ForceQuit,
+ Quit(QuitAction),
+
ReloadDirList,
RenameFile(path::PathBuf),
RenameFileAppend,
diff --git a/src/key_command/constants.rs b/src/key_command/constants.rs
index 74ada44..558790c 100644
--- a/src/key_command/constants.rs
+++ b/src/key_command/constants.rs
@@ -16,8 +16,6 @@ macro_rules! cmd_constants {
cmd_constants![
(CMD_QUIT, "quit"),
- (CMD_QUIT_TO_CURRENT_DIRECTORY, "quit_to_cwd"),
- (CMD_FORCE_QUIT, "force_quit"),
(CMD_BULK_RENAME, "bulk_rename"),
(CMD_CHANGE_DIRECTORY, "cd"),
(CMD_PARENT_DIRECTORY, "cd .."),
diff --git a/src/key_command/impl_appcommand.rs b/src/key_command/impl_appcommand.rs
index 478b8e1..6229289 100644
--- a/src/key_command/impl_appcommand.rs
+++ b/src/key_command/impl_appcommand.rs
@@ -6,9 +6,7 @@ impl AppCommand for Command {
match self {
Self::Help => CMD_HELP,
- Self::Quit => CMD_QUIT,
- Self::QuitToCurrentDirectory => CMD_QUIT_TO_CURRENT_DIRECTORY,
- Self::ForceQuit => CMD_FORCE_QUIT,
+ Self::Quit(_) => CMD_QUIT,
Self::BulkRename => CMD_BULK_RENAME,
diff --git a/src/key_command/impl_appexecute.rs b/src/key_command/impl_appexecute.rs
index bb2e752..8996a7c 100644
--- a/src/key_command/impl_appexecute.rs
+++ b/src/key_command/impl_appexecute.rs
@@ -64,9 +64,7 @@ impl AppExecute for Command {
Self::OpenFileWith(None) => open_file::open_with_interactive(context, backend),
Self::OpenFileWith(Some(i)) => open_file::open_with_index(context, backend, *i),
- Self::Quit => quit::quit(context),
- Self::QuitToCurrentDirectory => quit::quit_to_current_directory(context),
- Self::ForceQuit => quit::force_quit(context),
+ Self::Quit(action) => quit::quit_with_action(context, *action),
Self::ReloadDirList => reload::reload_dirlist(context),
Self::RenameFile(p) => rename_file::rename_file(context, p.as_path()),
diff --git a/src/key_command/impl_comment.rs b/src/key_command/impl_comment.rs
index b7d3cc5..b52e3a2 100644
--- a/src/key_command/impl_comment.rs
+++ b/src/key_command/impl_comment.rs
@@ -62,9 +62,7 @@ impl CommandComment for Command {
Self::OpenFile => "Open a file",
Self::OpenFileWith(_) => "Open using selected program",
- Self::Quit => "Quit the program",
- Self::QuitToCurrentDirectory => "Quit to current directory",
- Self::ForceQuit => "Force quit",
+ Self::Quit(_) => "Quit the program",
Self::ReloadDirList => "Reload current dir listing",
Self::RenameFile(_) => "Rename file",
Self::TouchFile(_) => "Touch file",
diff --git a/src/key_command/impl_from_str.rs b/src/key_command/impl_from_str.rs
index 5fd86ac..ba791e2 100644
--- a/src/key_command/impl_from_str.rs
+++ b/src/key_command/impl_from_str.rs
@@ -3,6 +3,7 @@ use std::path;
use dirs_next::home_dir;
use shellexpand::tilde_with_context;
+use crate::commands::quit::QuitAction;
use crate::config::option::{LineNumberStyle, SelectOption, SortType};
use crate::error::{JoshutoError, JoshutoErrorKind};
use crate::io::IoWorkerOptions;
@@ -33,14 +34,6 @@ impl std::str::FromStr for Command {
None => (s, ""),
};
- simple_command_conversion_case!(command, CMD_QUIT, Self::Quit);
- simple_command_conversion_case!(
- command,
- CMD_QUIT_TO_CURRENT_DIRECTORY,
- Self::QuitToCurrentDirectory
- );
- simple_command_conversion_case!(command, CMD_FORCE_QUIT, Self::ForceQuit);
-
simple_command_conversion_case!(command, CMD_NEW_TAB, Self::NewTab);
simple_command_conversion_case!(command, CMD_CLOSE_TAB, Self::CloseTab);
@@ -88,7 +81,14 @@ impl std::str::FromStr for Command {
simple_command_conversion_case!(command, CMD_TOGGLE_HIDDEN, Self::ToggleHiddenFiles);
simple_command_conversion_case!(command, CMD_BULK_RENAME, Self::BulkRename);
- if command == CMD_CHANGE_DIRECTORY {
+ if command == CMD_QUIT {
+ match arg {
+ "--force" => Ok(Self::Quit(QuitAction::Force)),
+ "--output-current-directory" => Ok(Self::Quit(QuitAction::OutputCurrentDirectory)),
+ "--output-selected-files" => Ok(Self::Quit(QuitAction::OutputSelectedFiles)),
+ _ => Ok(Self::Quit(QuitAction::Noop)),
+ }
+ } else if command == CMD_CHANGE_DIRECTORY {
match arg {
"" => match HOME_DIR.as_ref() {
Some(s) => Ok(Self::ChangeDirectory(s.clone())),
diff --git a/src/main.rs b/src/main.rs
index ecc8e6b..0223fc2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -20,10 +20,11 @@ use std::path::PathBuf;
use std::process;
use structopt::StructOpt;
+use crate::commands::quit::QuitAction;
use crate::config::{
AppConfig, AppKeyMapping, AppMimetypeRegistry, AppTheme, JoshutoPreview, TomlConfigFile,
};
-use crate::context::{AppContext, QuitType};
+use crate::context::AppContext;
use crate::error::JoshutoError;
use crate::run::run;
@@ -78,19 +79,17 @@ lazy_static! {
pub struct Args {
#[structopt(long = "path", parse(from_os_str))]
path: Option<PathBuf>,
- #[structopt(long)]
- choosefiles: Option<PathBuf>,
#[structopt(short = "v", long = "version")]
version: bool,
- #[structopt(long = "last-dir", parse(from_os_str))]
- last_dir: Option<PathBuf>,
+ #[structopt(long = "output-file", parse(from_os_str))]
+ output_file: Option<PathBuf>,
}
-fn run_joshuto(args: Args) -> Result<(), JoshutoError> {
+fn run_joshuto(args: Args) -> Result<i32, JoshutoError> {
if args.version {
let version = env!("CARGO_PKG_VERSION");
println!("{}-{}", PROGRAM_NAME, version);
- return Ok(());
+ return Ok(0);
}
if let Some(p) = args.path.as_ref() {
if let Err(e) = std::env::set_current_dir(p.as_path()) {
@@ -107,10 +106,14 @@ fn run_joshuto(args: Args) -> Result<(), JoshutoError> {
let mut backend: ui::TuiBackend = ui::TuiBackend::new()?;
run(&mut backend, &mut context, keymap)?;
}
+ run_quit(&args, &context)?;
+ Ok(context.quit.exit_code())
+}
+fn run_quit(args: &Args, context: &AppContext) -> Result<(), JoshutoError> {
match context.quit {
- QuitType::ToCurrentDirectory => {
- if let Some(p) = args.last_dir {
+ QuitAction::OutputCurrentDirectory => {
+ if let Some(p) = &args.output_file {
let curr_path = std::env::current_dir()?;
let mut file = File::create(p)?;
file.write_all(
@@ -123,8 +126,8 @@ fn run_joshuto(args: Args) -> Result<(), JoshutoError> {
file.write_all("\n".as_bytes())?;
}
}
- QuitType::ChooseFiles => {
- if let Some(path) = args.choosefiles {
+ QuitAction::OutputSelectedFiles => {
+ if let Some(path) = &args.output_file {
let curr_tab = context.tab_context_ref().curr_tab_ref();
let final_selection = curr_tab
.curr_list_ref()
@@ -136,18 +139,20 @@ fn run_joshuto(args: Args) -> Result<(), JoshutoError> {
}
}
}
- QuitType::Force => {}
+ QuitAction::Force => {}
_ => {}
}
-
Ok(())
}
fn main() {
let args = Args::from_args();
- if let Err(e) = run_joshuto(args) {
- eprintln!("{}", e);
- process::exit(1);
+ match run_joshuto(args) {
+ Ok(exit_code) => process::exit(exit_code),
+ Err(e) => {
+ eprintln!("{}", e);
+ process::exit(1);
+ }
}
}
diff --git a/src/run.rs b/src/run.rs
index 066e5fe..ea1974a 100644
--- a/src/run.rs
+++ b/src/run.rs
@@ -1,6 +1,7 @@
use crate::commands::numbered_command;
+use crate::commands::quit::QuitAction;
use crate::config::AppKeyMapping;
-use crate::context::{AppContext, QuitType};
+use crate::context::AppContext;
use crate::event::AppEvent;
use crate::key_command::{AppExecute, CommandKeybind};
use crate::preview::preview_default;
@@ -33,7 +34,7 @@ pub fn run(
preview_default::load_preview(context);
}
- while context.quit == QuitType::DoNot {
+ while context.quit == QuitAction::DoNot {
// do the ui
if let Ok(area) = backend.terminal_ref().size() {
// pre-calculate some ui attributes