From afcb3d73177364c24199bbcc654861a12c1bf9a0 Mon Sep 17 00:00:00 2001 From: rhysd Date: Thu, 6 Jun 2024 19:25:01 +0900 Subject: [PATCH] add `--no-restore` command option to avoid restoring window temporarily --- v2/src/cli.rs | 8 ++++++++ v2/src/persistent.rs | 10 ++++++++++ v2/src/shiba.rs | 9 +++++++++ 3 files changed, 27 insertions(+) diff --git a/v2/src/cli.rs b/v2/src/cli.rs index 8583510..977c61e 100644 --- a/v2/src/cli.rs +++ b/v2/src/cli.rs @@ -41,6 +41,7 @@ pub struct Options { pub init_file: Option, pub watch_paths: Vec, pub watch: bool, + pub restore: bool, pub theme: Option, pub gen_config_file: bool, pub config_dir: Option, @@ -54,6 +55,7 @@ impl Default for Options { init_file: None, watch_paths: vec![], watch: true, + restore: true, theme: None, gen_config_file: false, config_dir: None, @@ -71,6 +73,7 @@ editor, designed for simplicity, performance, and keyboard-friendliness. Options: -t, --theme THEME Window theme ("system" (default), "dark" or "light") --no-watch Disable to watch file changes + --no-restore Do not restore the previous window state --generate-config-file Generate the default config file overwriting an existing file --config-dir PATH Change the config directory path --data-dir PATH Change the application data directory path @@ -103,6 +106,7 @@ Document: Long("version") => return Ok(Parsed::Version(env!("CARGO_PKG_VERSION"))), Short('t') | Long("theme") => opts.theme = Some(parser.value()?.parse()?), Long("no-watch") => opts.watch = false, + Long("no-restore") => opts.restore = false, Long("generate-config-file") => opts.gen_config_file = true, Long("config-dir") => opts.config_dir = Some(path_value(&mut parser)?), Long("data-dir") => opts.data_dir = Some(path_value(&mut parser)?), @@ -180,6 +184,10 @@ mod tests { &["--no-watch"][..], Options { watch: false, ..Default::default() }, ), + ( + &["--no-restore"][..], + Options { restore: false, ..Default::default() }, + ), ( &["--debug"][..], Options { debug: true, ..Default::default() }, diff --git a/v2/src/persistent.rs b/v2/src/persistent.rs index edfbbcc..f7eba29 100644 --- a/v2/src/persistent.rs +++ b/v2/src/persistent.rs @@ -74,6 +74,16 @@ impl DataDir { .with_context(|| format!("Could not save persistent data to file {path:?}")) } + pub fn delete(&self) -> Result<()> { + let Some(dir) = &self.path else { + return Ok(()); + }; + let path = dir.join(D::FILE); + log::debug!("Delete persistent data at {path:?}"); + fs::remove_file(&path) + .with_context(|| format!("Could not delete persistent data at {path:?}")) + } + pub fn load_recent_files(&self, max_files: usize) -> Vec { if max_files == 0 { return vec![]; diff --git a/v2/src/shiba.rs b/v2/src/shiba.rs index 3dcc1a2..e2149ec 100644 --- a/v2/src/shiba.rs +++ b/v2/src/shiba.rs @@ -3,6 +3,7 @@ use crate::config::{Config, SearchMatcher}; use crate::dialog::Dialog; use crate::markdown::{DisplayText, MarkdownContent, MarkdownParser}; use crate::opener::Opener; +use crate::persistent::WindowState; use crate::renderer::{ Event, EventHandler, MenuItem, MessageFromRenderer, MessageToRenderer, Renderer, Rendering, RenderingFlow, @@ -229,10 +230,18 @@ where log::debug!("Application options: {:?}", options); let watch_paths = mem::take(&mut options.watch_paths); let init_file = mem::take(&mut options.init_file); + let no_restore = !options.restore; let config = Config::load(options)?; log::debug!("Application config: {:?}", config); + if no_restore { + if let Err(err) = config.data_dir().delete::() { + let inner = err.source().unwrap(); + log::debug!("Window state was not deleted: {err}: {inner}"); + } + } + let renderer = rendering.create_renderer(&config)?; let filter = PathFilter::new(config.watch());