Skip to content

Commit

Permalink
Merge pull request #204 from cantino/mcfly_histfile
Browse files Browse the repository at this point in the history
Add optional MCFLY_HISTFILE to avoid clobbering bash from fish
  • Loading branch information
cantino committed Nov 4, 2021
2 parents 4924280 + a43099b commit f5d035e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
8 changes: 4 additions & 4 deletions mcfly.fish
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ set -g __MCFLY_LOADED "loaded"

# Note: we only use the history file for the session when this file was sourced.
# Would have to reset this before calling mcfly if you want commands from another session later.
if not set -q HISTFILE
set -gx HISTFILE (set -q XDG_DATA_HOME; and echo $XDG_DATA_HOME; or echo $HOME/.local/share)/fish/(set -q fish_history; and echo $fish_history; or echo fish)_history
if not set -q MCFLY_HISTFILE
set -gx MCFLY_HISTFILE (set -q XDG_DATA_HOME; and echo $XDG_DATA_HOME; or echo $HOME/.local/share)/fish/(set -q fish_history; and echo $fish_history; or echo fish)_history
end
if not test -r "$HISTFILE"
echo "McFly: $HISTFILE does not exist or is not readable. Please fix this or set HISTFILE to something else before using McFly." >&2
if not test -r "$MCFLY_HISTFILE"
echo "McFly: $MCFLY_HISTFILE does not exist or is not readable. Please fix this or set MCFLY_HISTFILE to something else before using McFly." >&2
exit 1
end

Expand Down
15 changes: 11 additions & 4 deletions src/history_cleaner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ pub fn clean(settings: &Settings, history: &History, command: &str) {
// directory.
clean_temporary_files(&settings.mcfly_history, settings.history_format, command);

// Clean up HISTFILE.
let histfile = PathBuf::from(env::var("HISTFILE").unwrap_or_else(|err| {
panic!("McFly error: Please ensure that HISTFILE is set ({})", err)
}));
// Clean up HISTFILE/MCFLY_HISTFILE.
let histfile = PathBuf::from(
env::var("HISTFILE")
.or_else(|_| env::var("MCFLY_HISTFILE"))
.unwrap_or_else(|err| {
panic!(
"McFly error: Please ensure that HISTFILE/MCFLY_HISTFILE is set ({})",
err
)
}),
);
shell_history::delete_lines(&histfile, settings.history_format, command)
}
// Fish integration does not use a MCFLY_HISTORY file because we can get the last command
Expand Down
15 changes: 11 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ fn handle_addition(settings: &Settings) {
);

if settings.append_to_histfile {
let histfile = PathBuf::from(env::var("HISTFILE").unwrap_or_else(|err| {
panic!("McFly error: Please ensure that HISTFILE is set ({})", err)
}));
let histfile = PathBuf::from(
env::var("HISTFILE")
.or_else(|_| env::var("MCFLY_HISTFILE"))
.unwrap_or_else(|err| {
panic!(
"McFly error: Please ensure that $HISTFILE/$MCFLY_HISTFILE is set ({})",
err
)
}),
);
let command = shell_history::HistoryCommand::new(
&settings.command,
settings.when_run.unwrap_or(
Expand Down Expand Up @@ -63,7 +70,7 @@ fn handle_search(settings: &Settings) {
out.push('\n');

// Finally, any requests for deletion of commands from shell history, for cases where
// shells need to handle this natively instead of through us editing HISTFILE.
// shells need to handle this natively instead of through us editing HISTFILE/MCFLY_HISTFILE.
for delete_request in result.delete_requests {
out.push_str("delete ");
out.push_str(&delete_request);
Expand Down
2 changes: 1 addition & 1 deletion src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl Settings {
.takes_value(true))
.arg(Arg::with_name("append_to_histfile")
.long("append-to-histfile")
.help("Also append new history to $HISTFILE (e.q., .bash_history)"))
.help("Also append new history to $HISTFILE/$MCFLY_HISTFILE (e.q., .bash_history)"))
.arg(Arg::with_name("zsh_extended_history")
.long("zsh-extended-history")
.help("If appending, use zsh's EXTENDED_HISTORY format"))
Expand Down
21 changes: 15 additions & 6 deletions src/shell_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,19 @@ fn has_leading_timestamp(line: &str) -> bool {
}

pub fn history_file_path() -> PathBuf {
let path = PathBuf::from(env::var("HISTFILE").unwrap_or_else(|err| {
panic!(
"McFly error: Please ensure HISTFILE is set for your shell ({})",
let path = PathBuf::from(
env::var("HISTFILE")
.or_else(|_| env::var("MCFLY_HISTFILE"))
.unwrap_or_else(|err| {
panic!(
"McFly error: Please ensure HISTFILE or MCFLY_HISTFILE is set for your shell ({})",
err
)
}));
}),
);
fs::canonicalize(&path).unwrap_or_else(|err| {
panic!(
"McFly error: The contents of $HISTFILE appear invalid ({})",
"McFly error: The contents of $HISTFILE/$MCFLY_HISTFILE appears invalid ({})",
err
)
})
Expand Down Expand Up @@ -241,7 +245,12 @@ pub fn append_history_entry(command: &HistoryCommand, path: &Path, debug: bool)
.write(true)
.append(true)
.open(path)
.unwrap_or_else(|err| panic!("McFly error: please make sure HISTFILE exists ({})", err));
.unwrap_or_else(|err| {
panic!(
"McFly error: please make sure HISTFILE/MCFLY_HISTFILE exists ({})",
err
)
});

if debug {
println!("McFly: Appended to file '{:?}': {}", &path, command);
Expand Down

0 comments on commit f5d035e

Please sign in to comment.