-
Notifications
You must be signed in to change notification settings - Fork 0
feat(cli): support Homebrew binary paths for daemon and hooks #272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,71 @@ | ||||||
| #!/bin/bash | ||||||
|
|
||||||
| # Source bash-preexec.sh if it exists | ||||||
| if [ -f "bash-preexec.sh" ]; then | ||||||
| source "bash-preexec.sh" | ||||||
|
Comment on lines
+4
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The new Bash hook will source Useful? React with 👍 / 👎. |
||||||
| else | ||||||
| # Attempt to find bash-preexec.sh in the same directory as this script | ||||||
| _SHELLTIME_HOOK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||||||
| if [ -f "$_SHELLTIME_HOOK_DIR/bash-preexec.sh" ]; then | ||||||
| source "$_SHELLTIME_HOOK_DIR/bash-preexec.sh" | ||||||
| else | ||||||
| echo "Warning: bash-preexec.sh not found. Pre-execution hooks will not work." | ||||||
| return 1 | ||||||
| fi | ||||||
| fi | ||||||
|
|
||||||
| # Check if shelltime CLI exists | ||||||
| if ! command -v shelltime &> /dev/null | ||||||
| then | ||||||
| echo "Warning: shelltime CLI not found. Please install it to enable time tracking." | ||||||
| else | ||||||
| shelltime gc | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| fi | ||||||
|
|
||||||
| # Create a timestamp for the session when the shell starts | ||||||
| SESSION_ID=$(date +%Y%m%d%H%M%S) | ||||||
| LAST_COMMAND="" | ||||||
|
|
||||||
| # Function to be executed before each command | ||||||
| preexec_invoke_cmd() { | ||||||
| local CMD="$1" | ||||||
| LAST_COMMAND="$CMD" | ||||||
| # Check if command starts with exit, logout, or reboot | ||||||
| if [[ "$CMD" =~ ^(exit|logout|reboot) ]]; then | ||||||
| return | ||||||
| fi | ||||||
|
|
||||||
| # Avoid tracking shelltime commands themselves to prevent loops | ||||||
| if [[ "$CMD" =~ ^shelltime ]]; then | ||||||
| return | ||||||
| fi | ||||||
|
|
||||||
| shelltime track -s=bash -id=$SESSION_ID -cmd="$CMD" -p=pre --ppid=$PPID &> /dev/null | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Executing
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| # Function to be executed after each command (before prompt) | ||||||
| precmd_invoke_cmd() { | ||||||
| local LAST_RESULT=$? | ||||||
| # BASH_COMMAND in precmd is the *previous* command | ||||||
| local CMD="$LAST_COMMAND" | ||||||
| # Check if command starts with exit, logout, or reboot | ||||||
| if [[ "$CMD" =~ ^(exit|logout|reboot) ]]; then | ||||||
| return | ||||||
| fi | ||||||
|
|
||||||
| # Avoid tracking shelltime commands themselves to prevent loops | ||||||
| if [[ "$CMD" =~ ^shelltime ]]; then | ||||||
| return | ||||||
| fi | ||||||
|
|
||||||
| # Ensure CMD is not empty or the precmd_invoke_cmd itself | ||||||
| if [ -z "$CMD" ] || [ "$CMD" == "precmd_invoke_cmd" ]; then | ||||||
| return | ||||||
| fi | ||||||
|
|
||||||
| shelltime track -s=bash -id=$SESSION_ID -cmd="$CMD" -p=post -r=$LAST_RESULT --ppid=$PPID &> /dev/null | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The post-command tracking should also be backgrounded to ensure the prompt returns immediately after a command finishes.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| # Set the functions for bash-preexec | ||||||
| preexec_functions+=(preexec_invoke_cmd) | ||||||
| precmd_functions+=(precmd_invoke_cmd) | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #!/usr/bin/env fish | ||
|
|
||
| # Check if shelltime CLI exists | ||
| if not command -v shelltime > /dev/null | ||
| echo "Warning: shelltime CLI not found. Please install it to enable time tracking." | ||
| else | ||
| shelltime gc | ||
| end | ||
|
|
||
| # Create a timestamp for the session when the shell starts | ||
| set -g SESSION_ID (date +%Y%m%d%H%M%S) | ||
|
|
||
| # Capture parent process ID at shell startup (fish doesn't have native $PPID) | ||
| set -g FISH_PPID (ps -o ppid= -p %self | string trim) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| # Define the preexec function | ||
| function fish_preexec --on-event fish_preexec | ||
| if string match -q 'exit*' -- $argv; or string match -q 'logout*' -- $argv; or string match -q 'reboot*' -- $argv | ||
| return | ||
| end | ||
|
|
||
| shelltime track -s=fish -id=$SESSION_ID -cmd="$argv" -p=pre --ppid=$FISH_PPID > /dev/null | ||
| end | ||
|
|
||
| # Define the postexec function | ||
| function fish_postexec --on-event fish_postexec | ||
| set -g LAST_RESULT (echo $status) | ||
| if string match -q 'exit*' -- $argv; or string match -q 'logout*' -- $argv; or string match -q 'reboot*' -- $argv | ||
| return | ||
| end | ||
| # This event is triggered before each prompt, which is after each command | ||
| shelltime track -s=fish -id=$SESSION_ID -cmd="$argv" -p=post -r=$LAST_RESULT --ppid=$FISH_PPID > /dev/null | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Backgrounding the tracking call in Fish prevents latency between command completion and the next prompt. Also, redirecting stderr ensures that any errors from the tracking command don't clutter the terminal. |
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #!/usr/bin/env zsh | ||
|
|
||
| # Check if shelltime command exists | ||
| if ! command -v shelltime &> /dev/null | ||
| then | ||
| echo "Warning: shelltime command not found. Please install it to track shell usage." | ||
| else | ||
| shelltime gc | ||
| fi | ||
|
|
||
| # Create a timestamp for the session when the shell starts | ||
| SESSION_ID=$(date +%Y%m%d%H%M%S) | ||
|
|
||
| # Define the preexec function | ||
| preexec() { | ||
| local CMD=$1 | ||
| # Check if command starts with exit, logout, or reboot | ||
| if [[ $CMD =~ ^(exit|logout|reboot) ]]; then | ||
| return | ||
| fi | ||
|
|
||
| shelltime track -s=zsh -id=$SESSION_ID -cmd="$CMD" -p=pre --ppid=$PPID &> /dev/null | ||
| } | ||
|
|
||
| # Define the postexec function (in zsh, it's called precmd) | ||
| precmd() { | ||
| local LAST_RESULT=$? | ||
| local CMD=$(fc -ln -1) | ||
| # Check if command starts with exit, logout, or reboot | ||
| if [[ $CMD =~ ^(exit|logout|reboot) ]]; then | ||
| return | ||
| fi | ||
| shelltime track -s=zsh -id=$SESSION_ID -cmd="$CMD" -p=post -r=$LAST_RESULT --ppid=$PPID &> /dev/null | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The command returns
nilwhen the daemon binary is not found. It should return an error to ensure the CLI exits with a non-zero status code, which is essential for scripts and automation to detect failures.