cantino / mcfly

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

zsh's special history encoding is not handled #191

Closed onriv closed 2 years ago

onriv commented 2 years ago

zsh seems containing a special encoding in its history format: http://www.zsh.org/mla/users/2011/msg00154.html, and currently it seems making that mcfly cannot handle some characters correctly. For example:

❯ echo 'emoji😄'
emoji😄
❯ mcfly search emoji
echo 'emoji�������'

And the problem seems solved via decoding the zsh history first:

echo "$(cat  << 'EOF'
#define Meta ((char) 0x83)

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>

/* from zsh utils.c */
char *unmetafy(char *s, int *len)
{
  char *p, *t;

  for (p = s; *p && *p != Meta; p++);
  for (t = p; (*t = *p++);)
    if (*t++ == Meta)
      t[-1] = *p++ ^ 32;
  if (len)
    *len = t - s;
  return s;
}

int main(int argc, char *argv[]) {
  char *line = NULL;
  size_t size;

  while (getline(&line, &size, stdin) != -1) {
    unmetafy(line, NULL);
    printf("%s", line);
  }

  if (line) free(line);
  return EXIT_SUCCESS;
}
EOF
)"| gcc -x c - -o ~/.mcfly/zsh_history_format

mcfly_prompt_command () {
    local exit_code=$?
    if [[ ! -f "${MCFLY_HISTORY}" ]]
    then
        export MCFLY_HISTORY=$(mktemp -t mcfly.XXXXXXXX)
        tail -n100 "${HISTFILE}" |~/.mcfly/zsh_history_format >| ${MCFLY_HISTORY}
    fi
    # https://stackoverflow.com/questions/7756609/pass-stdout-as-file-name-for-command-line-util
    [ -z "${MCFLY_HISTORY_BUF}" ] && export MCFLY_HISTORY_BUF=mcfly_history_buf && mkfifo $MCFLY_HISTORY_BUF
    # fc -W "${MCFLY_HISTORY}"
    fc -W $MCFLY_HISTORY_BUF
    ~/.mcfly/zsh_history_format <$MCFLY_HISTORY_BUF > $MCFLY_HISTORY
    [ -n "$MCFLY_DEBUG" ] && echo "mcfly.zsh: Run mcfly add --exit ${exit_code}"
    $MCFLY_PATH --history_format zsh add --exit ${exit_code}
    return ${exit_code}
}
johannfr commented 2 years ago

I believe I'm having a similar problem, except that I'm stumbing into unicode problems on "normal" characters, not emoji. Screenshot 2021-09-07 at 10 41 28

cantino commented 2 years ago

You're right, I reproduced this issue. It's definitely a bug.

cantino commented 2 years ago

I think you have the right idea @onriv, but a better solution is to translate unmetafy into Rust and handle it in the mcfly binary. I can see if I can figure it out, although my C pointer math is pretty stale.

onriv commented 2 years ago

Found someone translated it: https://github.com/adder46/hstr-rs/blob/master/src/hstr.rs#L55 . I have never been programming with rust before. May it be helpful

cantino commented 2 years ago

Thanks!

cantino commented 2 years ago

Does #193 work for you?

onriv commented 2 years ago

@cantino hi, it works! Thanks a lot!

cantino commented 2 years ago

Thanks!

cantino commented 2 years ago

Released in 0.5.10.