From 00f2a8c908e299d08e2efad038da25b2d5446976 Mon Sep 17 00:00:00 2001 From: Steven Hall Date: Mon, 28 Sep 2015 22:13:18 -0700 Subject: [PATCH 1/9] Added feature that let's you specify how often to fetch with --fetch_t you can specify how many seconds to wait before auto fetching. using the --fetch option gives you the default of 5 minutes still. "--fetch_t 45" for example would fetch every 45 seconds. --- fetch.sh | 6 +++++- git-radar | 21 +++++++++++++++++---- radar-base.sh | 12 +++++++++--- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/fetch.sh b/fetch.sh index 37601e3..a3fc571 100755 --- a/fetch.sh +++ b/fetch.sh @@ -10,4 +10,8 @@ dot="$(cd "$(dirname "$([ -L "$0" ] && $READLINK_CMD -f "$0" || echo "$0")")"; p source $dot/radar-base.sh -fetch; +if [[ -z "$1" ]]; then + fetch; +else + fetch $1; +fi diff --git a/git-radar b/git-radar index 67b8460..c3f7aa3 100755 --- a/git-radar +++ b/git-radar @@ -56,10 +56,11 @@ if [[ -z $@ ]]; then echo "usage:" echo " git-radar [--zsh|--bash|--fish] [--fetch]" echo "" - echo " --fetch # Fetches your repo asynchronously in the background every 5 mins" - echo " --zsh # Output prompt using Zsh style color characters" - echo " --bash # Output prompt using Bash style color characters" - echo " --fish # Output prompt using fish style color characters" + echo " --fetch # Fetches your repo asynchronously in the background every 5 mins" + echo " --fetch_t # Fetches your repo asynchronously in the background every seconds" + echo " --zsh # Output prompt using Zsh style color characters" + echo " --bash # Output prompt using Bash style color characters" + echo " --fish # Output prompt using fish style color characters" echo "" echo "Bash example:" echo " export PS1=\"\\W\\\$(git-radar --bash --fetch) \"" @@ -83,18 +84,30 @@ if [[ -z $@ ]]; then echo " end" echo "" echo " Same as the Bash but for fish." + echo "" + echo "Fetch_t example:" + echo " export PS1=\"\\W\\\$(git-radar --bash --fetch_t 45) \"" exit fi while [[ $# > 0 ]];do command="$1" shift + # Default fetch of 5 min if [[ "$command" == "--fetch" ]]; then nohup $dot/fetch.sh >/dev/null 2>&1 & fi + + # Custom fetch of whatever time interval the user wants + if [[ "$command" == "--fetch_t" ]]; then + # Now $1 is the value of fetch_t + nohup $dot/fetch.sh $1 >/dev/null 2>&1 & + fi + if [[ "$command" == "--zsh" ]]; then $dot/prompt.zsh $args fi + if [[ "$command" == "--bash" || "$command" == "--fish" ]]; then $dot/prompt.bash $args fi diff --git a/radar-base.sh b/radar-base.sh index 258e5b1..ee5fa00 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -154,8 +154,7 @@ time_now() { time_to_update() { if is_repo; then local timesincelastupdate="$(($(time_now) - $(timestamp)))" - local fiveminutes="$((5 * 60))" - if (( $timesincelastupdate > $fiveminutes )); then + if (( $timesincelastupdate > $1 )); then # time to update return 0 (which is true) return 0 else @@ -168,7 +167,14 @@ time_to_update() { } fetch() { - if time_to_update; then + if [ -z "$1" ]; then + # Default 5 minutes + local timeToUpdate="$((5 * 60))" + else + local timeToUpdate="$1" + fi + + if time_to_update $timeToUpdate; then record_timestamp git fetch --quiet > /dev/null 2>&1 fi From 9e46dc2520728237e48f9fea879b85601a3aebf3 Mon Sep 17 00:00:00 2001 From: Steven Hall Date: Mon, 28 Sep 2015 22:20:16 -0700 Subject: [PATCH 2/9] Updated readme with --fetch_t feature. --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 3ef156d..c54764f 100644 --- a/README.md +++ b/README.md @@ -182,6 +182,15 @@ export PROMPT="$PROMPT\$(git-radar --zsh --fetch) " ``` [(note: the `\` escaping the `$` is important)](#ensuring-prompt-execution) +You may also choose to use the `--fetch_t` option to specify your own fetch +times. `--fetch` defaults to fetch automatically every 5 minutes. With +`--fetch_t ` you can choose how often to fetch. + +eg. +```bash +export PS1="$PS1\$(git-radar --bash --fetch_t 30)" +``` + ## Customise your prompt Git Radar is highly customisable using a prompt format string. The 4 features @@ -226,6 +235,7 @@ The default prompt format uses this to add spaces only if the feature would render. In that way the prompt always looks well spaced out no matter how many features are rendering. + ## Support ### Ensuring prompt execution From b8797284031edbfd4c22f19f4878418cf4b2ec10 Mon Sep 17 00:00:00 2001 From: Steven Hall Date: Fri, 16 Oct 2015 11:31:21 -0700 Subject: [PATCH 3/9] Made changes recommeneded by @michaeldfallen in fetch.sh "if" now checks for $@ instead of just $1 in radar-base.sh there is now a parameter expansion on line 170 instead of the if statements. in git-radar, now there is a shift, and a regex check that the next value is a number. If the next value after --fetch_t is not a number, an error is echo'd and it resorts to the default behaviour of 5 minutes. --- fetch.sh | 2 +- git-radar | 15 +++++++++++++-- radar-base.sh | 7 +------ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/fetch.sh b/fetch.sh index a3fc571..877fa7b 100755 --- a/fetch.sh +++ b/fetch.sh @@ -10,7 +10,7 @@ dot="$(cd "$(dirname "$([ -L "$0" ] && $READLINK_CMD -f "$0" || echo "$0")")"; p source $dot/radar-base.sh -if [[ -z "$1" ]]; then +if [[ -z "$@" ]]; then fetch; else fetch $1; diff --git a/git-radar b/git-radar index c3f7aa3..a38f00f 100755 --- a/git-radar +++ b/git-radar @@ -100,8 +100,19 @@ while [[ $# > 0 ]];do # Custom fetch of whatever time interval the user wants if [[ "$command" == "--fetch_t" ]]; then - # Now $1 is the value of fetch_t - nohup $dot/fetch.sh $1 >/dev/null 2>&1 & + # Now $1 is the value for fetch_t + # If value is not a number produce an error + if ! [[ $1 =~ ^[0-9]+$ ]] ; then + echo "" + echo "Error: fetch_t did not receive a number" + echo "Defaulting to 5 min." + echo " " + nohup $dot/fetch.sh >/dev/null 2>&1 & + else + command="$1" + shift + nohup $dot/fetch.sh $command >/dev/null 2>&1 & + fi fi if [[ "$command" == "--zsh" ]]; then diff --git a/radar-base.sh b/radar-base.sh index ee5fa00..886a6e9 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -167,12 +167,7 @@ time_to_update() { } fetch() { - if [ -z "$1" ]; then - # Default 5 minutes - local timeToUpdate="$((5 * 60))" - else - local timeToUpdate="$1" - fi + local timeToUpdate = ${"$1":-"$((5 * 60))"} if time_to_update $timeToUpdate; then record_timestamp From 3bdb43d9dad5766c23a34e438ad3fbd05823b6bd Mon Sep 17 00:00:00 2001 From: Steven Hall Date: Fri, 16 Oct 2015 16:26:11 -0700 Subject: [PATCH 4/9] Fixed an error in my parameter subsitution. --- radar-base.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radar-base.sh b/radar-base.sh index 886a6e9..d639fa0 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -167,7 +167,7 @@ time_to_update() { } fetch() { - local timeToUpdate = ${"$1":-"$((5 * 60))"} + local timeToUpdate=${1:-"$((5 * 60))"} if time_to_update $timeToUpdate; then record_timestamp From 8bef8d80648accd2806ad44e8db6e3aacd92d2e7 Mon Sep 17 00:00:00 2001 From: Steven Hall Date: Sun, 18 Oct 2015 16:27:32 -0700 Subject: [PATCH 5/9] Custom Fetch time now uses a variable Set the variable GIT_RADAR_FETCH_TIME in a bashrc, zshrc or gitradarrc file to customize the fetch time. --- README.md | 20 +++++++++++++++----- fetch.sh | 6 +----- git-radar | 21 --------------------- radar-base.sh | 17 ++++++++++++++++- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index c54764f..4f65c9c 100644 --- a/README.md +++ b/README.md @@ -182,13 +182,23 @@ export PROMPT="$PROMPT\$(git-radar --zsh --fetch) " ``` [(note: the `\` escaping the `$` is important)](#ensuring-prompt-execution) -You may also choose to use the `--fetch_t` option to specify your own fetch -times. `--fetch` defaults to fetch automatically every 5 minutes. With -`--fetch_t ` you can choose how often to fetch. +You may also choose to fetch at a customized interval of time. To do so, add +this to your .bashrc, .zshrc: -eg. ```bash -export PS1="$PS1\$(git-radar --bash --fetch_t 30)" +export GIT_RADAR_FETCH_TIME= +``` + +For example, to fetch every 30 seconds (instead of the default 5 minutes): + +```bash +export GIT_RADAR_FETCH_TIME=30 +``` + +You can also do this in the gitradarrc file: + +```bash +GIT_RADAR_FETCH_TIME=30 ``` ## Customise your prompt diff --git a/fetch.sh b/fetch.sh index 877fa7b..37601e3 100755 --- a/fetch.sh +++ b/fetch.sh @@ -10,8 +10,4 @@ dot="$(cd "$(dirname "$([ -L "$0" ] && $READLINK_CMD -f "$0" || echo "$0")")"; p source $dot/radar-base.sh -if [[ -z "$@" ]]; then - fetch; -else - fetch $1; -fi +fetch; diff --git a/git-radar b/git-radar index a38f00f..07f0a0a 100755 --- a/git-radar +++ b/git-radar @@ -84,37 +84,16 @@ if [[ -z $@ ]]; then echo " end" echo "" echo " Same as the Bash but for fish." - echo "" - echo "Fetch_t example:" - echo " export PS1=\"\\W\\\$(git-radar --bash --fetch_t 45) \"" exit fi while [[ $# > 0 ]];do command="$1" shift - # Default fetch of 5 min if [[ "$command" == "--fetch" ]]; then nohup $dot/fetch.sh >/dev/null 2>&1 & fi - # Custom fetch of whatever time interval the user wants - if [[ "$command" == "--fetch_t" ]]; then - # Now $1 is the value for fetch_t - # If value is not a number produce an error - if ! [[ $1 =~ ^[0-9]+$ ]] ; then - echo "" - echo "Error: fetch_t did not receive a number" - echo "Defaulting to 5 min." - echo " " - nohup $dot/fetch.sh >/dev/null 2>&1 & - else - command="$1" - shift - nohup $dot/fetch.sh $command >/dev/null 2>&1 & - fi - fi - if [[ "$command" == "--zsh" ]]; then $dot/prompt.zsh $args fi diff --git a/radar-base.sh b/radar-base.sh index d639fa0..f9d834a 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -13,6 +13,20 @@ timethis() { echo "$1 - $dur" >> $HOME/duration.dat } +get_fetch_time() { + if [ -f "$rcfile_path/.gitradarrc.bash" ]; then + source "$rcfile_path/.gitradarrc.bash" + elif [ -f "$rcfile_path/.gitradarrc.zsh" ]; then + source "$rcfile_path/.gitradarrc.zsh" + elif [ -f "$rcfile_path/.gitradarrc" ]; then + source "$rcfile_path/.gitradarrc" + fi + + FETCH_TIME="${GIT_RADAR_FETCH_TIME:-"$((5 * 60))"}" + echo $FETCH_TIME + +} + prepare_bash_colors() { if [ -f "$rcfile_path/.gitradarrc.bash" ]; then source "$rcfile_path/.gitradarrc.bash" @@ -167,7 +181,8 @@ time_to_update() { } fetch() { - local timeToUpdate=${1:-"$((5 * 60))"} + get_fetch_time + local timeToUpdate=${1:-$FETCH_TIME} if time_to_update $timeToUpdate; then record_timestamp From f52ce6c1b35edf7f35f20b6c10811ff6d978a688 Mon Sep 17 00:00:00 2001 From: Steven Hall Date: Sun, 18 Oct 2015 16:30:17 -0700 Subject: [PATCH 6/9] Missed a fetch_t in the git_radar file to remove. --- git-radar | 1 - 1 file changed, 1 deletion(-) diff --git a/git-radar b/git-radar index 07f0a0a..ce11945 100755 --- a/git-radar +++ b/git-radar @@ -57,7 +57,6 @@ if [[ -z $@ ]]; then echo " git-radar [--zsh|--bash|--fish] [--fetch]" echo "" echo " --fetch # Fetches your repo asynchronously in the background every 5 mins" - echo " --fetch_t # Fetches your repo asynchronously in the background every seconds" echo " --zsh # Output prompt using Zsh style color characters" echo " --bash # Output prompt using Bash style color characters" echo " --fish # Output prompt using fish style color characters" From 51000c4213983fc2a72b039858112b3bb4c2b592 Mon Sep 17 00:00:00 2001 From: Steven Hall Date: Sun, 18 Oct 2015 16:31:52 -0700 Subject: [PATCH 7/9] Moved comments back to original position. --- git-radar | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/git-radar b/git-radar index ce11945..9c8565f 100755 --- a/git-radar +++ b/git-radar @@ -56,10 +56,10 @@ if [[ -z $@ ]]; then echo "usage:" echo " git-radar [--zsh|--bash|--fish] [--fetch]" echo "" - echo " --fetch # Fetches your repo asynchronously in the background every 5 mins" - echo " --zsh # Output prompt using Zsh style color characters" - echo " --bash # Output prompt using Bash style color characters" - echo " --fish # Output prompt using fish style color characters" + echo " --fetch # Fetches your repo asynchronously in the background every 5 mins" + echo " --zsh # Output prompt using Zsh style color characters" + echo " --bash # Output prompt using Bash style color characters" + echo " --fish # Output prompt using fish style color characters" echo "" echo "Bash example:" echo " export PS1=\"\\W\\\$(git-radar --bash --fetch) \"" From 40445543f10f4e10cc13ea8a9e4ba0a79cb200db Mon Sep 17 00:00:00 2001 From: Steven Hall Date: Sun, 18 Oct 2015 23:07:31 -0700 Subject: [PATCH 8/9] Since a number is no longer taken after --fetch_t we can remove the parameter substitution that has been removed in this commit. --- radar-base.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/radar-base.sh b/radar-base.sh index f9d834a..538d593 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -181,10 +181,10 @@ time_to_update() { } fetch() { + # Gives $FETCH_TIME a value get_fetch_time - local timeToUpdate=${1:-$FETCH_TIME} - if time_to_update $timeToUpdate; then + if time_to_update $FETCH_TIME; then record_timestamp git fetch --quiet > /dev/null 2>&1 fi From 984ff133d360222906ff20f7431be9a70a212539 Mon Sep 17 00:00:00 2001 From: Michael Allen Date: Wed, 21 Oct 2015 11:59:16 +0100 Subject: [PATCH 9/9] Fix tests broken by FETCH_TIME now being pushed in --- radar-base.sh | 3 ++- test-directories.sh | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/radar-base.sh b/radar-base.sh index 0f4a35a..099debd 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -183,9 +183,10 @@ time_now() { } time_to_update() { + last_time_updated="${1:-$FETCH_TIME}" if is_repo; then local timesincelastupdate="$(($(time_now) - $(timestamp)))" - if (( $timesincelastupdate > $1 )); then + if (( $timesincelastupdate > $last_time_updated )); then # time to update return 0 (which is true) return 0 else diff --git a/test-directories.sh b/test-directories.sh index 86a441e..5dde303 100755 --- a/test-directories.sh +++ b/test-directories.sh @@ -63,12 +63,14 @@ test_record_timestamp_in_repo() { test_time_to_update_when_timestamp_is_old() { cd $scriptDir + FETCH_TIME="$((5 * 60))" # Fetch every 5 mins touch -A "-010000" "$(dot_git)/lastupdatetime" assertTrue time_to_update } test_not_time_to_update_when_just_recorded() { cd $scriptDir + FETCH_TIME="$((5 * 60))" # Fetch every 5 mins record_timestamp assertFalse time_to_update } @@ -77,6 +79,7 @@ test_time_to_update_when_no_timestamp() { cd_to_tmp git init --quiet + FETCH_TIME="$((5 * 60))" # Fetch every 5 mins time_to_update assertTrue time_to_update