From 3a372ab21ac4cb5240704e447c9beb4963c1b399 Mon Sep 17 00:00:00 2001 From: davidjbng <29679909+davidjbng@users.noreply.github.com> Date: Fri, 26 May 2023 21:35:09 +0200 Subject: [PATCH] fix saving commands to history --- README.md | 7 ++++++- conf.d/github-copilot-cli.fish | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c6b6426..e2205f0 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,17 @@ # github-copilot-cli.fish + Make GitHub Copilot CLI's alias work for Fish shell. The current Copilot CLI in beta only supports bash-like syntax. +Improvements over [z11i/github-copilot-cli.fish](https://github.com/z11i/github-copilot-cli.fish): + +- Save commands to history + ## How to install ### Via fisher ``` -fisher install z11i/github-copilot-cli.fish +fisher install davidjbng/github-copilot-cli.fish ``` ### Manually diff --git a/conf.d/github-copilot-cli.fish b/conf.d/github-copilot-cli.fish index 391ec5a..4095162 100644 --- a/conf.d/github-copilot-cli.fish +++ b/conf.d/github-copilot-cli.fish @@ -10,12 +10,36 @@ function __fish_add_history and history merge end +function __get_last_history_item + echo (history --max=1 | string trim) +end + +function __remove_last_history_item + set LAST_CMD (__get_last_history_item) + set LAST_CMD_ESCAPED (string escape --style=regex -- $LAST_CMD) + awk -v cmd="^- cmd: $LAST_CMD_ESCAPED" -v when="^ when: [0-9]+\$" ' + { + if ($0 ~ cmd) { + getline + if ($0 ~ when) { + getline + } + } else { + print + } + } + ' $__fish_user_data_dir/fish_history >$__fish_user_data_dir/fish_history.tmp + mv $__fish_user_data_dir/fish_history.tmp $__fish_user_data_dir/fish_history + history merge +end + function __copilot_what-the-shell set TMPFILE (mktemp) trap 'rm -f $TMPFILE' EXIT if github-copilot-cli what-the-shell $argv --shellout $TMPFILE if test -e $TMPFILE set FIXED_CMD (cat $TMPFILE) + __remove_last_history_item __fish_add_history $FIXED_CMD eval $FIXED_CMD else @@ -33,6 +57,7 @@ function __copilot_git-assist if github-copilot-cli git-assist $argv --shellout $TMPFILE if test -e $TMPFILE set FIXED_CMD (cat $TMPFILE) + __remove_last_history_item __fish_add_history $FIXED_CMD eval $FIXED_CMD else @@ -50,6 +75,7 @@ function __copilot_gh-assist if github-copilot-cli gh-assist $argv --shellout $TMPFILE if test -e $TMPFILE set FIXED_CMD (cat $TMPFILE) + __remove_last_history_item __fish_add_history $FIXED_CMD eval $FIXED_CMD else