summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorJeff Zhao <jeff.no.zhao@gmail.com>2021-01-31 12:02:20 -0500
committerJeff Zhao <jeff.no.zhao@gmail.com>2021-01-31 12:02:20 -0500
commita600e132ddf1a0421eb2f0893366cc17a31200a5 (patch)
tree9b8cbe4b5bea7f94d28f90a73d1acee073bcdab4 /src/main.rs
parent9ec999970ceb101febaa675ee4986ec26c37f2d9 (diff)
add --lastdir option to output pwd upon quitting
- joshuto will create a file and output the pwd of the last tab they were on when quitting - this allows users to write the pwd and go to that directory upon exiting using scripts
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs48
1 files changed, 37 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs
index 4962cb6..bd48146 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -11,15 +11,19 @@ mod ui;
mod util;
use lazy_static::lazy_static;
+use std::fs::File;
+use std::io::prelude::*;
use std::path::PathBuf;
use std::process;
use structopt::StructOpt;
-use config::{
+use crate::config::{
ConfigStructure, JoshutoCommandMapping, JoshutoConfig, JoshutoMimetype, JoshutoPreview,
JoshutoTheme,
};
-use run::run;
+use crate::error::{JoshutoError, JoshutoErrorKind};
+use crate::run::run;
+use crate::context::JoshutoContext;
const PROGRAM_NAME: &str = "joshuto";
const CONFIG_FILE: &str = "joshuto.toml";
@@ -51,23 +55,25 @@ lazy_static! {
static ref HOSTNAME: String = whoami::hostname();
}
-#[derive(StructOpt, Debug)]
+#[derive(Clone, Debug, StructOpt)]
pub struct Args {
#[structopt(long = "path", parse(from_os_str))]
path: Option<PathBuf>,
#[structopt(short = "v", long = "version")]
version: bool,
+ #[structopt(long = "lastdir", parse(from_os_str))]
+ last_dir: Option<PathBuf>,
}
-fn main() {
- let args = Args::from_args();
-
+fn run_joshuto(args: Args) -> Result<(), JoshutoError> {
if args.version {
let version = env!("CARGO_PKG_VERSION");
println!("{}", version);
- return;
+ let err = JoshutoError::new(JoshutoErrorKind::EnvVarNotPresent,
+ "CARGO_PKG_VERSION variable not found".to_string());
+ return Err(err);
}
- if let Some(p) = args.path {
+ if let Some(p) = args.path.as_ref() {
match std::env::set_current_dir(p.as_path()) {
Ok(_) => {}
Err(e) => {
@@ -80,10 +86,30 @@ fn main() {
let config = JoshutoConfig::get_config();
let keymap = JoshutoCommandMapping::get_config();
- match run(config, keymap) {
- Ok(_) => {}
+ let mut context = JoshutoContext::new(config);
+
+ {
+ let mut backend: ui::TuiBackend = ui::TuiBackend::new()?;
+ run(&mut backend, &mut context, keymap)?;
+ }
+
+ if let Some(p) = args.last_dir {
+ let curr_path = std::env::current_dir()?;
+ let mut file = File::create(p)?;
+ file.write_all(curr_path.into_os_string().as_os_str().to_string_lossy().as_bytes())?;
+ file.write_all("\n".as_bytes())?;
+ }
+
+ Ok(())
+}
+
+fn main() {
+ let args = Args::from_args();
+
+ match run_joshuto(args) {
+ Ok(_) => {},
Err(e) => {
- eprintln!("{}", e);
+ eprintln!("{}", e.to_string());
process::exit(1);
}
}