Repository: cantino/mcfly (language: rust)

Issue:
Deleting a history entry on Zsh rewrites history file in Bash format
1. Let's say `$HISTFILE` contains these entries:

```
$ cat ~/.zsh_history
: 1641675734:0;ls -la
: 1641675848:0;cat ~/.zsh_history
```

2. Press `^R` to activate `mcfly`, select `ls -la` entry, press `F2` to delete it and press `ESC` to exit out of `mcfly`

Expected result (zsh-style history preserved):

```
$ cat ~/.zsh_history
: 1641675848:0;cat ~/.zsh_history
```

Actual result (bash-style history is written back):

```
$ cat ~/.zsh_history
cat ~/.zsh_history
```

Next command executed (`uname -a` in the example) would be written in zsh style (with timestamp) again:

```
$ uname -a
...
$ cat ~/.zsh_history 
cat ~/.zsh_history
: 1641676115:0;uname -a
```



Discussion / hints:

Fix patch:
diff --git a/mcfly.zsh b/mcfly.zsh
index 79c4139a..a02527f8 100644
--- a/mcfly.zsh
+++ b/mcfly.zsh
@@ -34,6 +34,13 @@ if [[ ! -f "${MCFLY_HISTORY}" ]]; then
   export MCFLY_HISTORY=$(command mktemp -t mcfly.XXXXXXXX)
 fi
 
+# Check if we need to use extended history
+if [[ -o extendedhistory ]]; then
+  export MCFLY_HISTORY_FORMAT="zsh-extended"
+else
+  export MCFLY_HISTORY_FORMAT="zsh"
+fi
+
 # Setup a function to be used by $PROMPT_COMMAND.
 function mcfly_prompt_command {
   local exit_code=$? # Record exit status of previous command.
@@ -49,7 +56,7 @@ function mcfly_prompt_command {
 
   # Run mcfly with the saved code. It fill find the text of the last command in $MCFLY_HISTORY and save it to the database.
   [ -n "$MCFLY_DEBUG" ] && echo "mcfly.zsh: Run mcfly add --exit ${exit_code}"
-  $MCFLY_PATH --history_format zsh add --exit ${exit_code}
+  $MCFLY_PATH --history_format $MCFLY_HISTORY_FORMAT add --exit ${exit_code}
   return ${exit_code} # Restore the original exit code by returning it.
 }
 precmd_functions+=(mcfly_prompt_command)
@@ -68,7 +75,7 @@ if [[ $- =~ .*i.* ]]; then
       echoti rmkx
       exec </dev/tty
       local mcfly_output=$(mktemp -t mcfly.output.XXXXXXXX)
-      $MCFLY_PATH --history_format zsh search -o "${mcfly_output}" "${LBUFFER}"
+      $MCFLY_PATH --history_format $MCFLY_HISTORY_FORMAT search -o "${mcfly_output}" "${LBUFFER}"
       echoti smkx
 
       # Interpret commandline/run requests from McFly
diff --git a/src/settings.rs b/src/settings.rs
index 448d802f..4b1cccb7 100644
--- a/src/settings.rs
+++ b/src/settings.rs
@@ -141,7 +141,7 @@ impl Settings {
                 .takes_value(true))
             .arg(Arg::with_name("history_format")
                 .long("history_format")
-                .help("Shell history file format, 'bash', 'zsh', or 'fish' (defaults to 'bash')")
+                .help("Shell history file format, 'bash', 'zsh', 'zsh-extended' or 'fish' (defaults to 'bash')")
                 .value_name("FORMAT")
                 .takes_value(true))
             .subcommand(SubCommand::with_name("add")
@@ -156,9 +156,6 @@ impl Settings {
                 .arg(Arg::with_name("append_to_histfile")
                     .long("append-to-histfile")
                     .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"))
                 .arg(Arg::with_name("when")
                     .short("w")
                     .long("when")
@@ -314,6 +311,9 @@ impl Settings {
             Some("zsh") => HistoryFormat::Zsh {
                 extended_history: false,
             },
+            Some("zsh-extended") => HistoryFormat::Zsh {
+                extended_history: true,
+            },
             Some("fish") => HistoryFormat::Fish,
             Some(format) => panic!("McFly error: unknown history format '{}'", format),
         };
@@ -334,12 +334,6 @@ impl Settings {
                 );
 
                 settings.append_to_histfile = add_matches.is_present("append_to_histfile");
-                if add_matches.is_present("zsh_extended_history") {
-                    match settings.history_format {
-                        HistoryFormat::Zsh { .. } => settings.history_format = HistoryFormat::Zsh { extended_history: true },
-                        HistoryFormat::Bash | HistoryFormat::Fish => panic!("McFly error: cannot specify zsh extended history with non-zsh history format"),
-                    }
-                }
 
                 if add_matches.value_of("exit").is_some() {
                     settings.exit_code =


Test patch: