Skip to content

Instantly share code, notes, and snippets.

@KeyAmam
Last active October 20, 2023 07:34
Show Gist options
  • Select an option

  • Save KeyAmam/a6afcabc3a724fc4a541aca7629c3ff6 to your computer and use it in GitHub Desktop.

Select an option

Save KeyAmam/a6afcabc3a724fc4a541aca7629c3ff6 to your computer and use it in GitHub Desktop.

Revisions

  1. KeyAmam revised this gist Nov 30, 2019. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions .bash_hooks
    Original file line number Diff line number Diff line change
    @@ -16,9 +16,6 @@
    # define a function
    # add it into __hooks__after_each_command

    # ask for the admin password
    sudo -v

    filename="$(basename "$BASH_SOURCE")"

    # deactivate hooks to initialize (activate at the end)
  2. KeyAmam revised this gist Nov 28, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion .bash_hooks
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,8 @@
    # define a function
    # add it into __hooks__after_each_command


    # ask for the admin password
    sudo -v

    filename="$(basename "$BASH_SOURCE")"

  3. KeyAmam revised this gist Nov 28, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions .bash_hooks
    Original file line number Diff line number Diff line change
    @@ -106,6 +106,7 @@ unset __hooks__register_function
    # initialize PREVPWD
    echo 'initializing $PREVPWD..'
    export PREVPWD="$PWD"
    unset filename
    # activate hooks
    echo 'activating hooks..'
    export __HOOKS__ACTIVATED=true
  4. KeyAmam revised this gist Nov 27, 2019. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions .bash_hooks
    Original file line number Diff line number Diff line change
    @@ -42,11 +42,6 @@ function __hooks__before_each_command() {
    # before each command
    __hooks__set_git_clone_flag "${last_command[@]}";

    if [ "$PREVPWD" != "$PWD" ]; then
    # on changing directories
    export PREVPWD="$PWD"
    fi

    if [ -z "$__HOOKS__EXECUTING_LINE" ]; then
    return
    fi
    @@ -75,6 +70,12 @@ function __hooks__after_each_command() {
    # after each command
    __hooks__prevent_duplicate_actions
    __hooks__unset_git_clone_flag

    if [ "$PREVPWD" != "$PWD" ]; then
    # after changing directories
    # do something here
    export PREVPWD="$PWD"
    fi
    }
    ########## END OF SUBSEQUENT HOOK ##########

  5. KeyAmam revised this gist Nov 27, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions .bash_hooks
    Original file line number Diff line number Diff line change
    @@ -33,11 +33,11 @@ function __hooks__set_git_clone_flag() {
    }

    function __hooks__before_each_command() {
    local last_command=( $BASH_COMMAND )

    if [ -z "$__HOOKS__ACTIVATED" ]; then
    return
    fi

    local last_command=( $BASH_COMMAND )

    # before each command
    __hooks__set_git_clone_flag "${last_command[@]}";
  6. KeyAmam revised this gist Nov 27, 2019. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions .bash_hooks
    Original file line number Diff line number Diff line change
    @@ -96,12 +96,16 @@ function __hooks__register_function() {
    }

    # register the preceding hook
    echo 'registering the preceding hook..'
    trap '__hooks__before_each_command' DEBUG
    # register the subsequent hook
    echo 'registering the subsequent hook..'
    __hooks__register_function '__hooks__after_each_command'
    unset __hooks__register_function
    # initialize PREVPWD
    echo 'initializing $PREVPWD..'
    export PREVPWD="$PWD"
    # activate hooks
    echo 'activating hooks..'
    export __HOOKS__ACTIVATED=true
    ########## FINISH REGISTERING HOOKS ##########
  7. KeyAmam created this gist Nov 27, 2019.
    107 changes: 107 additions & 0 deletions .bash_hooks
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,107 @@
    #!/bin/bash

    # NOTES
    # To activate hooks, run:
    # . ./.bash_hooks

    # To clear hooks, run:
    # trap '' DEBUG
    # PROMPT_COMMAND=

    # To add a new action to the preceding hook
    # define a function
    # add it into __hooks__before_each_command

    # To add a new action to the subsequent hook
    # define a function
    # add it into __hooks__after_each_command



    filename="$(basename "$BASH_SOURCE")"

    # deactivate hooks to initialize (activate at the end)
    unset __HOOKS__ACTIVATED

    ########## START OF PRECEDING HOOK ##########
    # define functions and add them into __hooks__before_each_command
    function __hooks__set_git_clone_flag() {
    if [ 'git' = "$1" ] && [ 'clone' = "$2" ]; then
    echo "Setting __HOOKS__ON_GIT_CLONE=true in $filename"
    export __HOOKS__ON_GIT_CLONE=true
    fi
    }

    function __hooks__before_each_command() {
    if [ -z "$__HOOKS__ACTIVATED" ]; then
    return
    fi

    local last_command=( $BASH_COMMAND )

    # before each command
    __hooks__set_git_clone_flag "${last_command[@]}";

    if [ "$PREVPWD" != "$PWD" ]; then
    # on changing directories
    export PREVPWD="$PWD"
    fi

    if [ -z "$__HOOKS__EXECUTING_LINE" ]; then
    return
    fi

    unset __HOOKS__EXECUTING_LINE
    # before each first command of a comamnd line
    }
    ########## END OF PRECEDING HOOK ##########



    ########## START OF SUBSEQUENT HOOK ##########
    # define functions and add them into __hooks__after_each_command
    function __hooks__prevent_duplicate_actions() {
    export __HOOKS__EXECUTING_LINE=true
    }

    function __hooks__unset_git_clone_flag() {
    unset __HOOKS__ON_GIT_CLONE
    }

    function __hooks__after_each_command() {
    if [ -z "$__HOOKS__ACTIVATED" ]; then
    return
    fi
    # after each command
    __hooks__prevent_duplicate_actions
    __hooks__unset_git_clone_flag
    }
    ########## END OF SUBSEQUENT HOOK ##########



    ########## START REGISTERING HOOKS ##
    function __hooks__register_function() {
    local f="$1"; shift
    local regex="^ *$f *$"
    # filter the given function name out of PROMPT_COMMAND to avoid invalid format and duplicate execution
    # awk splits PROMPT_COMMAND by ";", extract elements that UNMATCH to the given regex and join with ";"
    # avoided using ORS to skip appending ";" to the last record
    PROMPT_COMMAND="$(
    echo "$PROMPT_COMMAND" |
    awk -v regex="$regex" 'BEGIN { RS=";" } length($1) > 0 && $1 !~ regex { printf "%s%s", delim, $1; delim=";" }'
    )"
    # append the function name
    export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND;}$f"
    }

    # register the preceding hook
    trap '__hooks__before_each_command' DEBUG
    # register the subsequent hook
    __hooks__register_function '__hooks__after_each_command'
    unset __hooks__register_function
    # initialize PREVPWD
    export PREVPWD="$PWD"
    # activate hooks
    export __HOOKS__ACTIVATED=true
    ########## FINISH REGISTERING HOOKS ##########