Skip to content

Commit

Permalink
add --no-restore command option to avoid restoring window temporarily
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Jun 6, 2024
1 parent 7c7add4 commit afcb3d7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions v2/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct Options {
pub init_file: Option<PathBuf>,
pub watch_paths: Vec<PathBuf>,
pub watch: bool,
pub restore: bool,
pub theme: Option<ThemeOption>,
pub gen_config_file: bool,
pub config_dir: Option<PathBuf>,
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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)?),
Expand Down Expand Up @@ -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() },
Expand Down
10 changes: 10 additions & 0 deletions v2/src/persistent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ impl DataDir {
.with_context(|| format!("Could not save persistent data to file {path:?}"))
}

pub fn delete<D: PersistentData>(&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<PathBuf> {
if max_files == 0 {
return vec![];
Expand Down
9 changes: 9 additions & 0 deletions v2/src/shiba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::<WindowState>() {
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());
Expand Down

0 comments on commit afcb3d7

Please sign in to comment.