From 22309937ab0aabea61c418c975662d1a05479966 Mon Sep 17 00:00:00 2001 From: darcy Date: Sun, 24 Nov 2024 21:48:09 +1100 Subject: [PATCH] feat: use user cache dir for debugger history file --- .gitignore | 2 -- Cargo.lock | 66 ++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/debugger/source.rs | 48 +++++++++++++++++++++++------- 4 files changed, 105 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 914c338..ea8c4bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1 @@ /target - -**/.debugger_history diff --git a/Cargo.lock b/Cargo.lock index 5c4f057..ba8af6f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -246,6 +246,27 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" +[[package]] +name = "dirs-next" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" +dependencies = [ + "cfg-if", + "dirs-sys-next", +] + +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "doc-comment" version = "0.3.3" @@ -322,6 +343,17 @@ dependencies = [ "byteorder", ] +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "gimli" version = "0.31.0" @@ -405,6 +437,7 @@ dependencies = [ "clap", "colored", "console", + "dirs-next", "fxhash", "hotwatch", "lazy_static", @@ -664,6 +697,17 @@ dependencies = [ "bitflags 2.6.0", ] +[[package]] +name = "redox_users" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + [[package]] name = "regex" version = "1.10.6" @@ -906,6 +950,22 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + [[package]] name = "winapi-util" version = "0.1.9" @@ -915,6 +975,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index 2560e2b..583815a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ miette = { version = "7.2.0", features = ["fancy"] } fxhash = "0.2.1" hotwatch = "0.5.0" console = "0.15.8" +dirs-next = "2.0.0" [dev-dependencies] assert_cmd = "2.0.14" diff --git a/src/debugger/source.rs b/src/debugger/source.rs index 91ac2ac..46521c6 100644 --- a/src/debugger/source.rs +++ b/src/debugger/source.rs @@ -159,16 +159,11 @@ impl SourceReader for Stdin { impl Terminal { pub fn new() -> Self { - let mut history_file = fs::OpenOptions::new() - .create(true) - .read(true) - .append(true) - .open(Self::FILENAME) - .unwrap(); + // TODO(feat/error): Handle file errors better - let mut buffer = String::new(); - history_file.read_to_string(&mut buffer).unwrap(); - let history: Vec<_> = buffer.lines().map(|s| s.to_string()).collect(); + let mut history_file = Self::get_history_file(); + + let history = Self::read_history_file(&mut history_file); let history_index = history.len(); Self { @@ -182,7 +177,40 @@ impl Terminal { } } - const FILENAME: &'static str = ".debugger_history"; + const FILE_NAME: &str = "lace-debugger-history"; + + fn get_history_file() -> File { + let parent_dir = dirs_next::cache_dir().unwrap_or_else(|| { + panic!("Cannot retrieve user cache directory"); + }); + assert!( + parent_dir.is_dir(), + "History file parent is not a directory" + ); + + let file_path = parent_dir.join(Self::FILE_NAME); + assert!( + !file_path.exists() || file_path.is_file(), + "History file exists but is not a file" + ); + + let file = fs::OpenOptions::new() + .create(true) + .read(true) + .append(true) + .open(file_path) + .unwrap(); + + file + } + + fn read_history_file(file: &mut File) -> Vec { + // TODO(opt): Avoid allocating extra string + let mut buffer = String::new(); + file.read_to_string(&mut buffer) + .expect("Failed to read history file"); + buffer.lines().map(|s| s.to_string()).collect() + } fn is_next(&self) -> bool { debug_assert!(