cantino / mcfly

Fly through your shell history. Great Scott!
MIT License
6.76k stars 176 forks source link

thread 'main' panicked at 'McFly error: Please ensure HISTFILE or MCFLY_HISTFILE is set #313

Closed digika-ding closed 1 year ago

digika-ding commented 1 year ago

mcfly --version: mcfly 0.7.0 zsh --version: zsh 5.8.1 (x86_64-apple-darwin22.0) kitty --version: kitty 0.26.5 created by Kovid Goyal zellij --version: zellij 0.34.3 sw_vers: ProductName: macOS ProductVersion: 13.0.1 BuildVersion: 22A400

echo $HISTFILE: /Users/weibingding/.zsh_history cat --line-range=0:3 $HISTFILE: ls cd Library ls

cat ~/.zshrc:

if type brew &>/dev/null
then
  FPATH="$(brew --prefix)/share/zsh/site-functions:${FPATH}"
  FPATH=$(brew --prefix)/share/zsh-completions:$FPATH

  autoload -Uz compinit
  compinit
fi
eval "$(zellij setup --generate-auto-start zsh)"
eval "$(starship init zsh)"
eval "$(sheldon source)"
function set_win_title(){
    echo -ne "\033]0; $PWD \007"
}
precmd_functions+=(set_win_title)
alias cat='bat --paging=never'
alias ls='exa --long --header --icons --sort=modified --group-directories-first --time-style=long-iso --group --created --modified'
alias lt='ls --tree'
alias ps='procs'
eval "$(mcfly init zsh)"
source $(brew --prefix)/share/zsh-autosuggestions/zsh-autosuggestions.zsh
source $(brew --prefix)/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

ctrl-r: McFly: Importing shell history for the first time. This may take a minute or two...thread 'main' panicked at 'McFly error: Please ensure HISTFILE or MCFLY_HISTFILE is set for your shell (environment variable not found)', src/shell_history.rs:62:17 note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

cantino commented 1 year ago

Hey @digika-ding, thank you for your report. Would it be easy to see if https://github.com/cantino/mcfly/pull/315 fixes this for you?

digika-ding commented 1 year ago

Hey @digika-ding, thank you for your report. Would it be easy to see if #315 fixes this for you?

May I ask how to integrate your patch to my local machine?

cantino commented 1 year ago

The simplest would probably be to edit your .zshrc file, comment out the eval "$(mcfly init zsh)" line, and instead eval a temporary file (eval "$(cat ~/mcfly-debug.zsh)") which contains:

#!/bin/zsh

# Ensure stdin is a tty
# Avoid loading this file more than once
if [[ -o interactive ]] && [[ "$__MCFLY_LOADED" != "loaded" ]]; then
  __MCFLY_LOADED="loaded"

  # Setup MCFLY_HISTFILE and make sure it exists.
  export MCFLY_HISTFILE="${HISTFILE:-$HOME/.zsh_history}"
  if [[ ! -r "${MCFLY_HISTFILE}" ]]; then
    echo "McFly: ${MCFLY_HISTFILE} does not exist or is not readable. Please fix this or set HISTFILE to something else before using McFly."
    return 1
  fi

  # MCFLY_SESSION_ID is used by McFly internally to keep track of the commands from a particular terminal session.
  export MCFLY_SESSION_ID=$(command dd if=/dev/urandom bs=256 count=1 2> /dev/null | LC_ALL=C command tr -dc 'a-zA-Z0-9' | command head -c 24)

  # Find the binary
  MCFLY_PATH=${MCFLY_PATH:-$(command which mcfly)}
  if [[ -z "$MCFLY_PATH" || "$MCFLY_PATH" == "mcfly not found" ]]; then
    echo "Cannot find the mcfly binary, please make sure that mcfly is in your path before sourcing mcfly.zsh."
    return 1
  fi

  # Required for commented out mcfly search commands to work.
  setopt interactive_comments   # allow comments in interactive shells (like Bash does)

  # McFly's temporary, per-session history file.
  if [[ ! -f "${MCFLY_HISTORY}" ]]; then
    export MCFLY_HISTORY=$(command mktemp ${TMPDIR:-/tmp}/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.

    # Populate McFly's temporary, per-session history file from recent commands in the shell's primary HISTFILE.
    if [[ ! -f "${MCFLY_HISTORY}" ]]; then
      export MCFLY_HISTORY=$(command mktemp ${TMPDIR:-/tmp}/mcfly.XXXXXXXX)
      command tail -n100 "${MCFLY_HISTFILE}" >| ${MCFLY_HISTORY}
    fi

    # Write history to $MCFLY_HISTORY.
    fc -W "${MCFLY_HISTORY}"

    # Run mcfly with the saved code. It 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 $MCFLY_HISTORY_FORMAT add --exit ${exit_code}
    return ${exit_code} # Restore the original exit code by returning it.
  }
  precmd_functions+=(mcfly_prompt_command)

  # Cleanup $MCFLY_HISTORY tmp files on exit.
  exit_logger() {
    [ -n "$MCFLY_DEBUG" ] && echo "mcfly.zsh: Exiting and removing $MCFLY_HISTORY"
    command rm -f $MCFLY_HISTORY
  }
  zshexit_functions+=(exit_logger)

  # If this is an interactive shell, take ownership of ctrl-r.
  if [[ $- =~ .*i.* ]]; then
    mcfly-history-widget() {
      () {
        echoti rmkx
        exec </dev/tty
        local mcfly_output=$(mktemp ${TMPDIR:-/tmp}/mcfly.output.XXXXXXXX)
        $MCFLY_PATH --history_format $MCFLY_HISTORY_FORMAT search -o "${mcfly_output}" "${LBUFFER}"
        echoti smkx

        # Interpret commandline/run requests from McFly
        while read -r key val; do
          if [[ "$key" = "mode" ]]; then local mode="$val"; fi
          if [[ "$key" = "commandline" ]]; then local commandline="$val"; fi
        done < "${mcfly_output}"
        command rm -f $mcfly_output

        if [[ -n $commandline ]]; then
          RBUFFER=""
          LBUFFER="${commandline}"
        fi
        if [[ "${mode}" == "run" ]]; then
          zle accept-line
        fi
        zle redisplay
      }
    }
    zle -N mcfly-history-widget
    bindkey '^R' mcfly-history-widget
  fi

fi
digika-ding commented 1 year ago

The simplest would probably be to edit your .zshrc file, comment out the eval "$(mcfly init zsh)" line, and instead eval a temporary file (eval "$(cat ~/mcfly-debug.zsh)") which contains:

#!/bin/zsh

# Ensure stdin is a tty
# Avoid loading this file more than once
if [[ -o interactive ]] && [[ "$__MCFLY_LOADED" != "loaded" ]]; then
  __MCFLY_LOADED="loaded"

  # Setup MCFLY_HISTFILE and make sure it exists.
  export MCFLY_HISTFILE="${HISTFILE:-$HOME/.zsh_history}"
  if [[ ! -r "${MCFLY_HISTFILE}" ]]; then
    echo "McFly: ${MCFLY_HISTFILE} does not exist or is not readable. Please fix this or set HISTFILE to something else before using McFly."
    return 1
  fi

  # MCFLY_SESSION_ID is used by McFly internally to keep track of the commands from a particular terminal session.
  export MCFLY_SESSION_ID=$(command dd if=/dev/urandom bs=256 count=1 2> /dev/null | LC_ALL=C command tr -dc 'a-zA-Z0-9' | command head -c 24)

  # Find the binary
  MCFLY_PATH=${MCFLY_PATH:-$(command which mcfly)}
  if [[ -z "$MCFLY_PATH" || "$MCFLY_PATH" == "mcfly not found" ]]; then
    echo "Cannot find the mcfly binary, please make sure that mcfly is in your path before sourcing mcfly.zsh."
    return 1
  fi

  # Required for commented out mcfly search commands to work.
  setopt interactive_comments   # allow comments in interactive shells (like Bash does)

  # McFly's temporary, per-session history file.
  if [[ ! -f "${MCFLY_HISTORY}" ]]; then
    export MCFLY_HISTORY=$(command mktemp ${TMPDIR:-/tmp}/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.

    # Populate McFly's temporary, per-session history file from recent commands in the shell's primary HISTFILE.
    if [[ ! -f "${MCFLY_HISTORY}" ]]; then
      export MCFLY_HISTORY=$(command mktemp ${TMPDIR:-/tmp}/mcfly.XXXXXXXX)
      command tail -n100 "${MCFLY_HISTFILE}" >| ${MCFLY_HISTORY}
    fi

    # Write history to $MCFLY_HISTORY.
    fc -W "${MCFLY_HISTORY}"

    # Run mcfly with the saved code. It 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 $MCFLY_HISTORY_FORMAT add --exit ${exit_code}
    return ${exit_code} # Restore the original exit code by returning it.
  }
  precmd_functions+=(mcfly_prompt_command)

  # Cleanup $MCFLY_HISTORY tmp files on exit.
  exit_logger() {
    [ -n "$MCFLY_DEBUG" ] && echo "mcfly.zsh: Exiting and removing $MCFLY_HISTORY"
    command rm -f $MCFLY_HISTORY
  }
  zshexit_functions+=(exit_logger)

  # If this is an interactive shell, take ownership of ctrl-r.
  if [[ $- =~ .*i.* ]]; then
    mcfly-history-widget() {
      () {
        echoti rmkx
        exec </dev/tty
        local mcfly_output=$(mktemp ${TMPDIR:-/tmp}/mcfly.output.XXXXXXXX)
        $MCFLY_PATH --history_format $MCFLY_HISTORY_FORMAT search -o "${mcfly_output}" "${LBUFFER}"
        echoti smkx

        # Interpret commandline/run requests from McFly
        while read -r key val; do
          if [[ "$key" = "mode" ]]; then local mode="$val"; fi
          if [[ "$key" = "commandline" ]]; then local commandline="$val"; fi
        done < "${mcfly_output}"
        command rm -f $mcfly_output

        if [[ -n $commandline ]]; then
          RBUFFER=""
          LBUFFER="${commandline}"
        fi
        if [[ "${mode}" == "run" ]]; then
          zle accept-line
        fi
        zle redisplay
      }
    }
    zle -N mcfly-history-widget
    bindkey '^R' mcfly-history-widget
  fi

fi

It's work for me! Thank you very much!

sapiderman commented 1 year ago

Happening for me too on and bash:

Distributor ID: Pop
Description:    Pop!_OS 22.04 LTS
Release:    22.04
Codename:   jammy

McFly version: 0.7.0

Context

note: HISTFILE does exist worked previously with mcFly 0.6.1

Will pulling and bulding from #315 fix it for this case too?

sapiderman commented 1 year ago

Update: ooh, #315 fixed it for me! Thank you!

cantino commented 1 year ago

Thanks for confirming @digika-ding and @sapiderman!