From 70e4465fc96c90140558a9fbbb189fb54c292071 Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Mon, 13 Jan 2020 19:29:50 +0100 Subject: [PATCH 01/10] The art of ommision Fixed ok.sh:80: and :240: bad substitution in zshell bad substitution in zshell export "$x" works, but shellcheck complaints , so now it's export "${x?}" --- ok.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ok.sh b/ok.sh index 54eae35..65af7d6 100755 --- a/ok.sh +++ b/ok.sh @@ -91,7 +91,7 @@ environment variables (for internal use): # export variables because python is a sub-process, and variables might have changed since initialization for x in $(set | grep "^_OK_" | awk -F '=' '{print $1}'); do - export "$x"="${!x}" + export "${x?}" done local -r version="0.8.0" @@ -237,7 +237,7 @@ else unset re_list_once # export variables so `ok` can be used from scripts as well for x in $(set | grep "^_OK_" | awk -F '=' '{print $1}'); do - export "$x"="${!x}" + export "${x?}" done #make ok available for scripts as well export -f ok From 7ca1b8348f2992cdc3010e9a076f880927cb097f Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Mon, 13 Jan 2020 20:03:45 +0100 Subject: [PATCH 02/10] Fixed some shellcheck warnings --- ok.sh | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/ok.sh b/ok.sh index 65af7d6..904b1bd 100755 --- a/ok.sh +++ b/ok.sh @@ -3,9 +3,9 @@ called=$_ #basically, get the absolute path of this script (handy for loads of things) -pushd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null; +pushd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null || exit 1 _OK__PATH_TO_ME=$(pwd) -popd > /dev/null; +popd > /dev/null || exit 1 # Don't let ok-show.py's shebang control the python version; prefer python3 above python regular (2) _OK__PATH_TO_PYTHON=$(command -v python3 || command -v python) @@ -45,11 +45,11 @@ script-arguments: if [ -z ${_OK_VERBOSE+x} ]; then local v="unset"; else local v="$_OK_VERBOSE"; fi if [ -z ${_OK_PROMPT_DEFAULT+x} ]; then local l="unset"; else local l="$_OK_PROMPT_DEFAULT"; fi echo -e "environment variables (used for colored output; current colors are shown): - _OK_C_HEADING ${_OK_C_HEADING}Color-code${c_nc} for lines starting with a comment (heading). Defaults to red. - _OK_C_NUMBER ${_OK_C_NUMBER}Color-code${c_nc} for numbering. Defaults to cyan. - _OK_C_COMMENT ${_OK_C_COMMENT}Color-code${c_nc} for comments after commands. Defaults to blue. - _OK_C_COMMAND ${_OK_C_COMMAND}Color-code${c_nc} for commands. Defaults to color-reset. - _OK_C_PROMPT ${_OK_C_PROMPT}Color-code${c_nc} for prompt (both input as command confirmation). Defaults to color for numbering. + _OK_C_HEADING ${_OK_C_HEADING:-}Color-code${c_nc} for lines starting with a comment (heading). Defaults to red. + _OK_C_NUMBER ${_OK_C_NUMBER:-}Color-code${c_nc} for numbering. Defaults to cyan. + _OK_C_COMMENT ${_OK_C_COMMENT:-}Color-code${c_nc} for comments after commands. Defaults to blue. + _OK_C_COMMAND ${_OK_C_COMMAND:-}Color-code${c_nc} for commands. Defaults to color-reset. + _OK_C_PROMPT ${_OK_C_PROMPT:-}Color-code${c_nc} for prompt (both input as command confirmation). Defaults to color for numbering. environment variables (other configuration): _OK_COMMENT_ALIGN Level ($e) of comment alignment. 0=no alignment, 1=align consecutive lines (Default), 2=including whitespace, 3 align all. _OK_PROMPT String ($p) used as prompt (both input as command confirmation). Defaults to '$ '. @@ -190,7 +190,8 @@ environment variables (for internal use): echo "Nothing to do: this folder doesn't have a readable '$ok_file' file" fi fi - export _OK__LAST_PWD=$(pwd) + _OK__LAST_PWD=$(pwd) + export _OK__LAST_PWD } if [[ "$called" == "$0" ]]; then From 5c4b4e5888d9afab8ece1dd780861b4ff64b13c1 Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Mon, 13 Jan 2020 20:12:38 +0100 Subject: [PATCH 03/10] Update ok.sh Never use exit in sourced scripts --- ok.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ok.sh b/ok.sh index 904b1bd..f72a034 100755 --- a/ok.sh +++ b/ok.sh @@ -3,9 +3,9 @@ called=$_ #basically, get the absolute path of this script (handy for loads of things) -pushd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null || exit 1 +pushd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null || (>&2 echo "ok-bash: pushd failed") _OK__PATH_TO_ME=$(pwd) -popd > /dev/null || exit 1 +popd > /dev/null || (>&2 echo "ok-bash: popd failed") # Don't let ok-show.py's shebang control the python version; prefer python3 above python regular (2) _OK__PATH_TO_PYTHON=$(command -v python3 || command -v python) From 5a5cfbaddde21e10536ffd34fff4e9998dcfd934 Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Mon, 13 Jan 2020 20:38:50 +0100 Subject: [PATCH 04/10] Update ok.sh Removed export of OK-variables when running ok-function, as it is the responsibility of the user to alter and export these variables. --- ok.sh | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/ok.sh b/ok.sh index f72a034..02348b6 100755 --- a/ok.sh +++ b/ok.sh @@ -89,11 +89,6 @@ environment variables (for internal use): cat "$ok_file" | "$_OK__PATH_TO_PYTHON" "${_OK__PATH_TO_ME}/ok-show.py" -v "$verbose" -c "$comment_align" || return $? } - # export variables because python is a sub-process, and variables might have changed since initialization - for x in $(set | grep "^_OK_" | awk -F '=' '{print $1}'); do - export "${x?}" - done - local -r version="0.8.0" # used for colored output (see: https://stackoverflow.com/a/20983251/56) # notice: this is partly a duplication from code in ok-show.py @@ -236,7 +231,7 @@ else shift done unset re_list_once - # export variables so `ok` can be used from scripts as well + # export variables so `ok` can be used from scripts as well. Hereafter, exporting is the responsibility of the user. for x in $(set | grep "^_OK_" | awk -F '=' '{print $1}'); do export "${x?}" done From 9566e603bdb61cb08442dc940d7b23161ef51750 Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Mon, 13 Jan 2020 20:43:50 +0100 Subject: [PATCH 05/10] Update ok.sh Made escape-codes work with exporting functions (when running `declare -f ok`, the output became colored halfway) --- ok.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ok.sh b/ok.sh index 02348b6..50dcbe1 100755 --- a/ok.sh +++ b/ok.sh @@ -92,8 +92,8 @@ environment variables (for internal use): local -r version="0.8.0" # used for colored output (see: https://stackoverflow.com/a/20983251/56) # notice: this is partly a duplication from code in ok-show.py - local -r c_nc=$'\033[0m' - if [ -z ${_OK_C_NUMBER+x} ]; then local c_number=$'\033[0;36m'; else local c_number=$_OK_C_NUMBER; fi #NUMBER defaults to CYAN + local -r c_nc=$'\033''[0m' + if [ -z ${_OK_C_NUMBER+x} ]; then local c_number=$'\033''[0;36m'; else local c_number=$_OK_C_NUMBER; fi #NUMBER defaults to CYAN 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 # other customizations (some environment variables can be overridden by arguments) if [ -z ${_OK_PROMPT+x} ]; then local prompt="$ "; else local prompt=$_OK_PROMPT; fi From 1c2c5bdfc39718e6fc616d5fbe757fec7bfa8e80 Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Thu, 16 Jan 2020 22:49:10 +0100 Subject: [PATCH 06/10] Fixed an python2 error shutil.get_terminal_size doesn't exist in python2 --- ok-show.py | 11 +++++++++-- ok.sh | 9 ++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/ok-show.py b/ok-show.py index 68b04a8..b11f189 100755 --- a/ok-show.py +++ b/ok-show.py @@ -130,15 +130,22 @@ def print_line(l, clr, nr_positions_line_nr, format_line): def main(): # customizations clr = ok_color() - terminal_size = shutil.get_terminal_size() + # handle arguments parser = argparse.ArgumentParser(description='Show the ok-file colorized (or just one line).') parser.add_argument('--verbose', '-v', metavar='V', type=int, default=1, help='0=quiet, 1=normal, 2=verbose. Defaults to 1. ') parser.add_argument('--comment_align', '-c', metavar='CA', type=int, default=2, choices= [0,1,2,3], help='Level ($e) of comment alignment. 0=no alignment, 1=align consecutive lines (Default), 2=including whitespace, 3 align all.') - parser.add_argument('--terminal_width', '-t', metavar='TW', type=int, default=terminal_size.columns, help='number of columns of the terminal (tput cols)') + parser.add_argument('--terminal_width', '-t', metavar='TW', type=int, default=None, help='number of columns of the terminal (tput cols)') parser.add_argument('only_line_nr', metavar='N', type=int, nargs='?', help='the line number to show') args = parser.parse_args() + if args.terminal_width is None: + if sys.version_info[0] >= 3: + args.terminal_width = shutil.get_terminal_size().columns + else: + # Python 2 doesn't have `get_terminal_size` + args.terminal_width = 80 + if args.verbose > 1: print('comment_align:', args.comment_align) print('terminal_width:', args.terminal_width) diff --git a/ok.sh b/ok.sh index 50dcbe1..aa52b29 100755 --- a/ok.sh +++ b/ok.sh @@ -71,9 +71,11 @@ environment variables (for internal use): # save and remove argument. Remaining arguments are passwed to eval automatically local line_nr=$1 #LINE_NR is guaranteed to be 1 or more shift + local twidth + twidth="$(stty size|awk '{print $2}')" # get the line to be executed local line_text - line_text="$(cat "$ok_file" | "$_OK__PATH_TO_PYTHON" "${_OK__PATH_TO_ME}/ok-show.py" -v "$verbose" -c "$comment_align" "$line_nr")" + line_text="$(cat "$ok_file" | "$_OK__PATH_TO_PYTHON" "${_OK__PATH_TO_ME}/ok-show.py" -v "$verbose" -c "$comment_align" -t "$twidth" "$line_nr")" local res=$? if [[ $res -ne 0 ]]; then #because stdout/stderr are swapped by ok-show.py in this case, handle this too @@ -85,8 +87,9 @@ environment variables (for internal use): function _ok_cmd_list { unset -f _ok_cmd_list - - cat "$ok_file" | "$_OK__PATH_TO_PYTHON" "${_OK__PATH_TO_ME}/ok-show.py" -v "$verbose" -c "$comment_align" || return $? + local twidth + twidth="$(stty size|awk '{print $2}')" + cat "$ok_file" | "$_OK__PATH_TO_PYTHON" "${_OK__PATH_TO_ME}/ok-show.py" -v "$verbose" -c "$comment_align" -t "$twidth" || return $? } local -r version="0.8.0" From 6844fca9b6ba421fe6dddb4af0a37c69d44cb8a9 Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Mon, 20 Jan 2020 21:16:47 +0100 Subject: [PATCH 07/10] Fixed issue #19 --- ok.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ok.sh b/ok.sh index aa52b29..dad264e 100755 --- a/ok.sh +++ b/ok.sh @@ -182,14 +182,14 @@ environment variables (for internal use): if [[ $verbose -ge 2 && $once_check == 1 && $_OK__LAST_PWD == $(pwd) ]]; then echo "The listing for this folder has already been shown" fi + _OK__LAST_PWD=$(pwd) + export _OK__LAST_PWD fi else if [[ $verbose -ge 2 ]]; then echo "Nothing to do: this folder doesn't have a readable '$ok_file' file" fi fi - _OK__LAST_PWD=$(pwd) - export _OK__LAST_PWD } if [[ "$called" == "$0" ]]; then From faa8f717364b85a6544a21eedfcefd07e4e13aa6 Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Mon, 20 Jan 2020 21:39:41 +0100 Subject: [PATCH 08/10] Python call now on one line --- ok-show.py | 5 +++-- ok.sh | 20 +++++++++----------- test/.ok | 1 + 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/ok-show.py b/ok-show.py index b11f189..509a9c4 100755 --- a/ok-show.py +++ b/ok-show.py @@ -147,8 +147,9 @@ def main(): args.terminal_width = 80 if args.verbose > 1: - print('comment_align:', args.comment_align) - print('terminal_width:', args.terminal_width) + print(' comment_align: %d' % args.comment_align) + print('terminal_width: %d' % args.terminal_width) + print('python version: '+ sys.version.replace('\n', '\t')) # prepare (read stdin parse, transform, and calculate stuff) # Unicode: best to ignore other encodings? SO doesn't seem to give good advice diff --git a/ok.sh b/ok.sh index dad264e..97d1ec0 100755 --- a/ok.sh +++ b/ok.sh @@ -66,16 +66,21 @@ environment variables (for internal use): fi } + function _ok_show { + unset -f _ok_show + local twidth + twidth="$(stty size|awk '{print $2}')" + cat "$ok_file" | "${_OK__PATH_TO_PYTHON:-$(command -v python3 || command -v python)}" "${_OK__PATH_TO_ME}/ok-show.py" -v "$verbose" -c "$comment_align" -t "$twidth" "$@" + } + function _ok_cmd_run { unset -f _ok_cmd_run # save and remove argument. Remaining arguments are passwed to eval automatically local line_nr=$1 #LINE_NR is guaranteed to be 1 or more shift - local twidth - twidth="$(stty size|awk '{print $2}')" # get the line to be executed local line_text - line_text="$(cat "$ok_file" | "$_OK__PATH_TO_PYTHON" "${_OK__PATH_TO_ME}/ok-show.py" -v "$verbose" -c "$comment_align" -t "$twidth" "$line_nr")" + line_text="$(_ok_show "$line_nr")" local res=$? if [[ $res -ne 0 ]]; then #because stdout/stderr are swapped by ok-show.py in this case, handle this too @@ -85,13 +90,6 @@ environment variables (for internal use): eval "$line_text" } - function _ok_cmd_list { - unset -f _ok_cmd_list - local twidth - twidth="$(stty size|awk '{print $2}')" - cat "$ok_file" | "$_OK__PATH_TO_PYTHON" "${_OK__PATH_TO_ME}/ok-show.py" -v "$verbose" -c "$comment_align" -t "$twidth" || return $? - } - local -r version="0.8.0" # used for colored output (see: https://stackoverflow.com/a/20983251/56) # notice: this is partly a duplication from code in ok-show.py @@ -154,7 +152,7 @@ environment variables (for internal use): _ok_cmd_run "$line_nr" "$@" || return $? elif [[ $cmd == list ]]; then if [[ $once_check == 0 || ($once_check == 1 && $_OK__LAST_PWD != $(pwd)) ]]; then - _ok_cmd_list + _ok_show || return $? local list_result=$? if [[ $list_result -gt 1 ]]; then return $list_result diff --git a/test/.ok b/test/.ok index b5cf597..870776d 100644 --- a/test/.ok +++ b/test/.ok @@ -1,6 +1,7 @@ # Control what python to use _OK__PATH_TO_PYTHON=$(command -v python) # set python2 _OK__PATH_TO_PYTHON=$(command -v python3) # set python3 +unset _OK__PATH_TO_PYTHON #test if this works too echo "Path to python: '$_OK__PATH_TO_PYTHON'" # check path $_OK__PATH_TO_PYTHON --version # check python version # Control encoding or not From b32cd7efe0ed26a639142335d984bd7a178e3d93 Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Mon, 20 Jan 2020 21:41:49 +0100 Subject: [PATCH 09/10] No more shellcheck warnings --- ok.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ok.sh b/ok.sh index 97d1ec0..217e8f6 100755 --- a/ok.sh +++ b/ok.sh @@ -69,8 +69,13 @@ environment variables (for internal use): function _ok_show { unset -f _ok_show local twidth + local input="$ok_file" + if [[ $input = - ]]; then + input="/dev/stdin" + fi twidth="$(stty size|awk '{print $2}')" - cat "$ok_file" | "${_OK__PATH_TO_PYTHON:-$(command -v python3 || command -v python)}" "${_OK__PATH_TO_ME}/ok-show.py" -v "$verbose" -c "$comment_align" -t "$twidth" "$@" + + "${_OK__PATH_TO_PYTHON:-$(command -v python3 || command -v python)}" "${_OK__PATH_TO_ME}/ok-show.py" -v "$verbose" -c "$comment_align" -t "$twidth" "$@" < "${input}" } function _ok_cmd_run { From 48ad43d10bdccb597c7ccb86aca97877c7379dd7 Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Mon, 20 Jan 2020 21:56:44 +0100 Subject: [PATCH 10/10] Made ok_show a public callable function --- ok.sh | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ok.sh b/ok.sh index 217e8f6..4048e36 100755 --- a/ok.sh +++ b/ok.sh @@ -66,16 +66,16 @@ environment variables (for internal use): fi } - function _ok_show { - unset -f _ok_show + function ok_show { local twidth - local input="$ok_file" - if [[ $input = - ]]; then + local input="${1:--}" + shift + if [[ $input = - ]]; then # Prevent shellcheck's "useless cat"-warning input="/dev/stdin" fi twidth="$(stty size|awk '{print $2}')" - - "${_OK__PATH_TO_PYTHON:-$(command -v python3 || command -v python)}" "${_OK__PATH_TO_ME}/ok-show.py" -v "$verbose" -c "$comment_align" -t "$twidth" "$@" < "${input}" + + "${_OK__PATH_TO_PYTHON:-$(command -v python3 || command -v python)}" "${_OK__PATH_TO_ME}/ok-show.py" -v "${verbose:-1}" -c "${comment_align:-1}" -t "${twidth:-80}" "$@" < "${input}" } function _ok_cmd_run { @@ -85,7 +85,7 @@ environment variables (for internal use): shift # get the line to be executed local line_text - line_text="$(_ok_show "$line_nr")" + line_text="$(ok_show "$ok_file" "$line_nr")" local res=$? if [[ $res -ne 0 ]]; then #because stdout/stderr are swapped by ok-show.py in this case, handle this too @@ -157,7 +157,7 @@ environment variables (for internal use): _ok_cmd_run "$line_nr" "$@" || return $? elif [[ $cmd == list ]]; then if [[ $once_check == 0 || ($once_check == 1 && $_OK__LAST_PWD != $(pwd)) ]]; then - _ok_show || return $? + ok_show "$ok_file" || return $? local list_result=$? if [[ $list_result -gt 1 ]]; then return $list_result