Skip to content

Commit

Permalink
feat: use user cache dir for debugger history file
Browse files Browse the repository at this point in the history
  • Loading branch information
dxrcy committed Nov 24, 2024
1 parent 4f0b955 commit 2230993
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 12 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
/target

**/.debugger_history
66 changes: 66 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
48 changes: 38 additions & 10 deletions src/debugger/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<String> {
// 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!(
Expand Down

0 comments on commit 2230993

Please sign in to comment.