From 40fc23b276aa7c261ddea1cb2d17257f6e192ea9 Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Thu, 14 Jun 2018 16:42:18 +0200 Subject: [PATCH 1/5] output trailing comments in a different color --- ok | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) mode change 100644 => 100755 ok diff --git a/ok b/ok old mode 100644 new mode 100755 index 004f753..45c0b82 --- a/ok +++ b/ok @@ -1,9 +1,12 @@ +#!/usr/bin/env bash + # tip: "." (i.e. source) this file from your profile (.bashrc), e.g. ". ~/ok" ok() { # used for colored output CYAN='\033[0;36m' +BLUE=$'\033[0;34m' NC='\033[0m' # No Color # if there is a file called .ok... @@ -11,15 +14,14 @@ NC='\033[0m' # No Color re='^[0-9]+$' # if the user provided a parameter, $1, which contains a number... if [[ $1 =~ $re ]] ; then - # output the command first (TODO: output trailing comments in a different color) - cat .ok | echo -e "${CYAN}$ ${NC}$(sed $1'!d')" + # output the command first + cat .ok | echo -e "${CYAN}$ ${NC}$(sed $1'!d')" | sed "s/#/${BLUE}#/1" # execute that line from the file # TODO: pass through any remaining parameters, i.e ${@:2} source <( cat .ok | sed $1'!d' ) else # list the content of the file, with a number (1-based) before each line - # TODO: output trailing comments in a different color - cat .ok | awk -v z=$CYAN -v n=$NC '{print z "" ++i "." n " " $0 }' + cat .ok | awk -v z=$CYAN -v n=$NC '{print z "" ++i "." n " " $0 }' | sed "s/#/${BLUE}#/1" fi fi } \ No newline at end of file From 3a22ae3ce82873236fb210202d69cb899f7ba1c3 Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Thu, 14 Jun 2018 16:51:25 +0200 Subject: [PATCH 2/5] Fix for macOS bash's process substitution bug --- ok | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ok b/ok index 45c0b82..9b9a2e3 100755 --- a/ok +++ b/ok @@ -18,7 +18,7 @@ NC='\033[0m' # No Color cat .ok | echo -e "${CYAN}$ ${NC}$(sed $1'!d')" | sed "s/#/${BLUE}#/1" # execute that line from the file # TODO: pass through any remaining parameters, i.e ${@:2} - source <( cat .ok | sed $1'!d' ) + eval $( cat .ok | sed $1'!d' ) else # list the content of the file, with a number (1-based) before each line cat .ok | awk -v z=$CYAN -v n=$NC '{print z "" ++i "." n " " $0 }' | sed "s/#/${BLUE}#/1" From b16f0e1443c41f3bfa493cbf04632c13b0dcfa40 Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Thu, 14 Jun 2018 16:53:45 +0200 Subject: [PATCH 3/5] fix for output trailing comments in a different color --- ok | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ok b/ok index 9b9a2e3..07f3465 100755 --- a/ok +++ b/ok @@ -5,9 +5,9 @@ ok() { # used for colored output -CYAN='\033[0;36m' +CYAN=$'\033[0;36m' BLUE=$'\033[0;34m' -NC='\033[0m' # No Color +NC=$'\033[0m' # No Color # if there is a file called .ok... if [ -f .ok ]; then @@ -15,13 +15,13 @@ NC='\033[0m' # No Color # if the user provided a parameter, $1, which contains a number... if [[ $1 =~ $re ]] ; then # output the command first - cat .ok | echo -e "${CYAN}$ ${NC}$(sed $1'!d')" | sed "s/#/${BLUE}#/1" + cat .ok | echo -e "${CYAN}$ ${NC}$(sed $1'!d')" | sed -E "s/(#.*)\$/${BLUE}\1${NC}/1" # execute that line from the file # TODO: pass through any remaining parameters, i.e ${@:2} eval $( cat .ok | sed $1'!d' ) else # list the content of the file, with a number (1-based) before each line - cat .ok | awk -v z=$CYAN -v n=$NC '{print z "" ++i "." n " " $0 }' | sed "s/#/${BLUE}#/1" + cat .ok | awk -v z=$CYAN -v n=$NC '{print z "" ++i "." n " " $0 }' | sed -E "s/(#.*)\$/${BLUE}\1${NC}/1" fi fi } \ No newline at end of file From f65c1a3131c4a0c81a1474a027508269cb459c4e Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Thu, 14 Jun 2018 17:08:59 +0200 Subject: [PATCH 4/5] pass through any remaining parameters --- ok | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ok b/ok index 07f3465..9d632c8 100755 --- a/ok +++ b/ok @@ -16,9 +16,11 @@ NC=$'\033[0m' # No Color if [[ $1 =~ $re ]] ; then # output the command first cat .ok | echo -e "${CYAN}$ ${NC}$(sed $1'!d')" | sed -E "s/(#.*)\$/${BLUE}\1${NC}/1" + # save and remove argument. Remaining arguments are passwed to eval automatically + LINE_NR=$1 + shift # execute that line from the file - # TODO: pass through any remaining parameters, i.e ${@:2} - eval $( cat .ok | sed $1'!d' ) + eval $( cat .ok | sed ${LINE_NR}'!d' ) else # list the content of the file, with a number (1-based) before each line cat .ok | awk -v z=$CYAN -v n=$NC '{print z "" ++i "." n " " $0 }' | sed -E "s/(#.*)\$/${BLUE}\1${NC}/1" From 3cc292931d4b27c3e4b9410e28e4ac7fef1f979b Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Sat, 16 Jun 2018 15:40:12 +0200 Subject: [PATCH 5/5] Fixed leaking of variables --- ok | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ok b/ok index 9d632c8..abef63c 100755 --- a/ok +++ b/ok @@ -5,9 +5,9 @@ ok() { # used for colored output -CYAN=$'\033[0;36m' -BLUE=$'\033[0;34m' -NC=$'\033[0m' # No Color +local CYAN=$'\033[0;36m' +local BLUE=$'\033[0;34m' +local NC=$'\033[0m' # No Color # if there is a file called .ok... if [ -f .ok ]; then