Skip to content
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

Header comments added & colors are made configurable #2

Merged
merged 3 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Manipulate customizations (colors)
unset _OK_C_HEADING;unset _OK_C_NUMBER;unset _OK_C_COMMENT;unset _OK_C_COMMAND;unset _OK_C_PROMPT #Reset colors to defaults
_OK_C_HEADING="[h]";_OK_C_NUMBER="[n]";_OK_C_COMMENT="[--]";_OK_C_COMMAND="[C]";_OK_C_PROMPT="[p]" #Change colors to text markers for debugging
_OK_C_HEADING=$'\033[1;30;45m';_OK_C_NUMBER=$'\033[1;33;44m';_OK_C_COMMENT=$'\033[1;34;46m';_OK_C_COMMAND=$'\033[1;37;44m' #Custom color scheme
49 changes: 31 additions & 18 deletions ok
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,40 @@
# tip: "." (i.e. source) this file from your profile (.bashrc), e.g. ". ~/ok"

ok() {
# used for colored output (see: https://stackoverflow.com/a/20983251/56)
local C_NC=$(tput sgr 0)
if [ -z ${_OK_C_HEADING+x} ]; then local C_HEADING=$(tput setaf 1); else local C_HEADING=$_OK_C_HEADING; fi #HEADING defaults to RED
if [ -z ${_OK_C_NUMBER+x} ]; then local C_NUMBER=$(tput setaf 6); else local C_NUMBER=$_OK_C_NUMBER; fi #NUMBER defaults to CYAN
if [ -z ${_OK_C_COMMENT+x} ]; then local C_COMMENT=$(tput setaf 4); else local C_COMMENT=$_OK_C_COMMENT; fi #COMMENT defaults to BLUE
if [ -z ${_OK_C_COMMAND+x} ]; then local C_COMMAND=$C_NC; else local C_COMMAND=$_OK_C_COMMAND; fi #COMMAND defaults to NO COLOR
if [ -z ${_OK_C_PROMPT+x} ]; then local C_PROMPT=$C_NUMBER; else local C_PROMPT=$_OK_C_PROMPT; fi #PROMPT defaults to same color as NUMBER

# used for colored output
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
# if there is a file called .ok...
if [ -f .ok ]; then
re='^[0-9]+$'
# 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 -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
eval $( cat .ok | sed ${LINE_NR}'!d' )
# save and remove argument. Remaining arguments are passwed to eval automatically
LINE_NR=$1
shift
LINE_TEXT=$( cat .ok | grep -vE "^#" | sed ${LINE_NR}'!d' )
# output the command first
echo -e "${C_PROMPT}\$ ${C_COMMAND}${LINE_TEXT}${C_NC}" | sed -E "s/(#.*)\$/${C_COMMENT}\1/1"
# remove commented lines, and execute that line from the file
eval $LINE_TEXT
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"
# list the content of the file, with a number (1-based) before each line,
# except lines starting with a "#", those are printed red without a number) as headers
cat .ok | awk -v h=$C_HEADING -v n=$C_NUMBER -v c=$C_COMMENT -v m=$C_COMMAND -v x=$C_NC $'
$0 ~ /^#/ {
#print the (sub-)headings
print x h $0 x;
}
$0 ~ /^[^#]/ {
#print the commands
sub(/#/,c "#");
print x n "" ++i "." m " " $0 x;
}'
fi
fi
}
fi
}