Skip to content

Commit

Permalink
Merge pull request #9 from mkcn/zsh-support
Browse files Browse the repository at this point in the history
Zsh support
  • Loading branch information
mkcn authored Aug 8, 2019
2 parents f61ff6c + b32a395 commit 87b7349
Showing 1 changed file with 62 additions and 17 deletions.
79 changes: 62 additions & 17 deletions bash/f.sh
Original file line number Diff line number Diff line change
@@ -1,40 +1,87 @@
#!/bin/bash

# absolute path of the project folder
_fast_history_project_directory="${BASH_SOURCE[0]%/*}/../"
############################################################
# tool: fastHistory
# note: this script must be called with "source <file>"
# author: Mirko Conti
# date: 2019-07-24
############################################################

# if true the return code of the executed command is check before to store it
# by default this feature is disable
_fast_history_check_return_code=false
_fast_history_bash_debug=false

_fast_history_hooked_cmd=""
_fast_history_short_cmd=false
# [sperimental feature, off by default] if true the return code of the executed command is check before to store it
_fast_history_check_return_code=false

# define internal log function
_fast_history_log() {
if [ "$1" = "error" ]; then
echo "[fastHistory][ERROR] $2";
elif [ "$1" = "info" ]; then
echo "[fastHistory][INFO ] $2";
elif [ "$1" = "debug" ] && $_fast_history_bash_debug ; then
echo "[fastHistory][DEBUG] $2";
fi
}

# start message
_fast_history_log "debug" "loading fastHistory start..";

# check if bash or zsh
if [ -n "$BASH_VERSION" ]; then
_fast_history_log "debug" "bash detected";
_fast_history_project_directory="${BASH_SOURCE[0]%/*}/../";
elif [ -n "$ZSH_VERSION" ]; then
_fast_history_log "debug" "zsh detected";
_fast_history_project_directory="$0:a:h/../";
else
_fast_history_log "error" "your shell is not supported";
return 1;
fi

# check environment
if [ -s "$_fast_history_project_directory"fastHistory/version.txt ]; then
_fast_history_log "debug" "installation folder: $_fast_history_project_directory";
else
_fast_history_log "error" "cannot find installation folder";
return 1;
fi

# load bash hook functions (more info: https://github.com/rcaloras/bash-preexec/)
source "$_fast_history_project_directory"bash/bash-preexec.sh;
if [ -z "$__bp_imported" ]; then
_fast_history_log "error" "preexec cannot be loaded";
return 1;
else
_fast_history_log "debug" "preexec loaded correctly";
fi

# define custom function to call the fastHistory in SEARCH mode
f-search() {
# trick to capture all input (otherwise the comments are removed)
arguments=${_fast_history_hooked_cmd:${#FUNCNAME[0]} + 1}
arguments=${_fast_history_hooked_cmd:8 + 1}
python3 "$_fast_history_project_directory"fastHistory/fastHistory.py "search" "$arguments";
unset _fast_history_hooked_cmd;
}

# define an alternative (shorter and faster to type) function to call the fastHistory in SEARCH mode
f() {
# trick to capture all input (otherwise the comments are removed)
arguments=${_fast_history_hooked_cmd:${#FUNCNAME[0]} + 1}
arguments=${_fast_history_hooked_cmd:1 + 1}
python3 "$_fast_history_project_directory"fastHistory/fastHistory.py "search" "$arguments";
unset _fast_history_hooked_cmd;
}

# define function to add a command to fastHistory without execute it
f-add() {
# trick to capture all input (otherwise the comments are removed)
arguments=${_fast_history_hooked_cmd:${#FUNCNAME[0]} + 1}
arguments=${_fast_history_hooked_cmd:5 + 1}
python3 "$_fast_history_project_directory"fastHistory/fastHistory.py "add-explicit" "$arguments";
unset _fast_history_hooked_cmd;
}

# define function to import db
f-import(){
DIR=$1
if [ "${DIR:0:1}" = "/" ]; then
Expand All @@ -44,6 +91,7 @@ f-import(){
fi
}

# define function to export db
f-export(){
if [ $# -eq 0 ]; then
python3 "$_fast_history_project_directory"fastHistory/fastHistory.py "export" "fastHistory_$(date +'%Y-%m-%d').db";
Expand All @@ -57,10 +105,6 @@ f-export(){
fi
}

# load bash hook functions
# more info: https://github.com/rcaloras/bash-preexec/
source "$_fast_history_project_directory"bash/bash-preexec.sh

# "preexec" is executed just after a command has been read and is about to be executed
# we store the hooked command in a bash variable
preexec() { _fast_history_hooked_cmd="$1"; }
Expand All @@ -79,14 +123,15 @@ precmd() {
python3 "$_fast_history_project_directory"fastHistory/fastHistory.py "add" "$_fast_history_hooked_cmd"
# clean the cmd, this is needed because precmd can be trigged without preexec (example ctrl+c)
unset _fast_history_hooked_cmd;
else
if $_fast_history_bash_debug ; then echo "[fastHistory][DEBUG] '#' not found: command ignored"; fi;
else
_fast_history_log "debug" "shell command ignored";
fi;
else
if $_fast_history_bash_debug ; then echo "[fastHistory][DEBUG] error code detected: command ignored"; fi;
_fast_history_log "debug" "error code detected: command ignored";
fi;
else
if $_fast_history_bash_debug ; then echo "[fastHistory][DEBUG] _fast_history_hooked_cmd: empty"; fi;
fi;
}


_fast_history_log "debug" "loading fastHistory completed. Use 'f' to start";

0 comments on commit 87b7349

Please sign in to comment.