чување 868500b6dc1f634291a2a104f4f61dd6c70d83e9
родитељ 642f340a91e872c56d6d12a286782dde65bc8e78
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Tue, 27 Jun 2023 18:29:24 +0200
Add -h and -v
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| M | .gitignore | | | 1 | + |
| M | INSTALL | | | 4 | ++-- |
| M | all.do | | | 2 | +- |
| M | clean.do | | | 2 | +- |
| D | ste | | | 233 | ------------------------------------------------------------------------------- |
| M | ste.1.in | | | 12 | ++++++++++++ |
| A | ste.in | | | 261 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
измењених датотека: 7, додавања: 278(+), брисања: 237(-)
diff --git a/.gitignore b/.gitignore
@@ -16,6 +16,7 @@ nohup.out
*.gz
*.xz
config.redo
+ste
ste.1
ste.session
compile_commands.json
diff --git a/INSTALL b/INSTALL
@@ -2,8 +2,8 @@ Prerequisites
=============
* gzip
-* Standard utilities: cat, cp, diff, grep, head, mv, printf, rm, sed, stat,
- tail, touch, wc
+* Standard utilities: cat, cp, diff, getopts, grep, head, mv, printf, rm, sed,
+ stat, tail, touch, wc
* table[1]
* vipe from moreutils[2]
* Any text editor set via EDITOR environment variable
diff --git a/all.do b/all.do
@@ -1 +1 @@
-redo-ifchange ste.1.gz
+redo-ifchange ste ste.1.gz
diff --git a/clean.do b/clean.do
@@ -1,2 +1,2 @@
redo-always
-rm -f ste.1 ste.1.gz *~ version date
+rm -f ste ste.1 ste.1.gz *~ version date
diff --git a/ste b/ste
@@ -1,233 +0,0 @@
-#!/bin/sh
-# vim: set ft=bash:
-# ste - Simple Table Editor
-# See the file LICENSE for copyright and license details.
-
-# Global options
-# Clear screen after update
-clear=0
-# Table output width in screen columns
-cols=80
-# Pass -m to table
-msdos=0
-# Pass -n to table (recommended)
-noansi=1
-
-# Which flavors of TSV/CSV utilities to use
-delprog=tsvdel
-editprog=tsvedit
-insprog=tsvins
-tableprog=tsvtable
-
-error()
-{
- fmt=$1
- shift
- # shellcheck disable=SC2059
- printf "%s: %s\n" "$prog" "$(printf "$fmt" "$@")"
-}
-
-help()
-{
- case "$1" in
- =) printf "= - Show current row\n";;
- cols) printf "cols [amount] - Set table width in screen columns\n";;
- delete|x)
- printf "delete | x - Delete the current table row\n";;
- edit|e) printf "edit | e - Edit the current table row\n";;
- exit|q) printf "exit | q - Exit %s\n" "$prog";;
- G) printf "G - Set current row to last\n";;
- g) printf "g [num] - Set current table row to num (header = 0)\n"
- printf "g - Set current row to header row (0)\n";;
- help|h) printf "help [command] | h [command] - Show help "
- printf "(about a command)\n";;
- insert|i)
- printf "insert - | i - - Add a row to the table before the "
- printf "current line\n"
- printf "insert + | i + - Add a row to the table after the "
- printf "current line\n"
- printf "insert | i - Add a row to the end of table\n";;
- j) printf "j [num] - Move the current row pointer down num rows\n"
- printf "j - Move the current row pointer down\n";;
- k) printf "k [num] - Move the current row pointer up num rows\n"
- printf "k - Move the current row pointer up\n";;
- print|p)
- printf "print | p - Print (redraw) the table\n";;
- set) printf "set [[no]option] - Turn option on/off\n"
- printf "set - Show option states\n";;
- "") printf "Commands: = cols delete e edit exit G g h help i "
- printf "insert j k p print q set x\n"
- printf "help [command] - Show help about a command\n";;
- *) error "Unknown command '%s'" "$args";;
- esac
-}
-
-printopt()
-{
- opt=$1
- if [ "$(eval echo "\$$opt")" -eq 0 ]; then
- printf "no"
- fi
- printf "%s\n" "$opt"
-}
-
-setopts()
-{
- opts=''
- opts="$opts -c $cols"
- if [ "$msdos" -eq 1 ]; then
- opts="$opts -m"
- fi
- if [ "$noansi" -eq 1 ]; then
- opts="$opts -n"
- fi
- printf "%s" "$opts"
-}
-
-file=$1
-prog=${0##*/}
-
-if [ $# -lt 1 ]; then
- error "Usage: %s [file]" "$prog"
- exit 1
-fi
-
-if ! stat "$file" >/dev/null 2>&1; then
- ${insprog} "$file"
-fi
-if ! stat "$file" >/dev/null 2>&1; then
- error "File does not exist: '%s'" "$file"
- exit 1
-fi
-
-continue=1
-currow=0
-redraw=1
-sgi=$(printf "\e[")
-
-while [ "$continue" -eq 1 ]; do
- rows=$(wc -l "$file" | cut -d' ' -f1)
- if [ "$redraw" -eq 1 ]; then
- if [ "$clear" -eq 1 ]; then
- clear
- fi
- # shellcheck disable=SC2046
- ${tableprog} $(setopts) "$file" \
- | awk '
-{
- if (NR=='$currow'+2)
- {
- print '\""$sgi"'7m'\"' $0 '\""$sgi"'0m'\"'
- }
- else print
-}'
- redraw=0
- fi
-
- printf "ste> "
- read -r cmd
-
- IFS=' '
- set -- $cmd
- cmd=$1
- shift
- args="$@"
- case "$cmd" in
- =) printf "Current row: %d\n" "$currow";;
- cols) if [ -n "$args" ]; then
- cols="$args"
- redraw=1
- else
- error "Invalid argument: '%s'" "$args"
- fi
- ;;
- delete|x)
- ${delprog} "$file" "$((currow + 1))"
- if [ "$currow" -eq "$((rows - 1))" ]; then
- if [ "$rows" -ge 2 ]; then
- currow=$((rows - 2))
- else
- currow=0
- fi
- fi
- redraw=1
- ;;
- edit|e) ${editprog} "$file" "$((currow + 1))"
- redraw=1
- ;;
- exit|q) continue=0
- ;;
- G) currow=$((rows - 1))
- redraw=1
- ;;
- g) case "$args" in
- "") currow=0
- redraw=1;;
- *) if [ "$args" -ge 0 ] && [ "$args" -lt "$rows" ]; then
- currow="$args"
- redraw=1
- else
- error "Row out of range: '%s'" "$args"
- fi
- esac
- ;;
- help|h|"")
- help "$args"
- ;;
- insert|i)
- case "$args" in
- -) ${insprog} "$file" "$((currow))"
- redraw=1;;
- +) ${insprog} "$file" "$((currow + 1))"
- currow=$((currow + 1))
- redraw=1;;
- "") ${insprog} "$file"
- currow=$((rows))
- redraw=1;;
- *) error "Invalid argument: '%s'" "$args";;
- esac
- ;;
- j) torow=$((currow + 1))
- if [ -n "$args" ]; then
- torow=$((currow + args))
- fi
- if [ "$torow" -lt "$rows" ]; then
- currow="$torow"
- redraw=1
- else
- error "Row out of range: '%s'" "$torow"
- fi
- ;;
- k) torow=$((currow - 1))
- if [ -n "$args" ]; then
- torow=$((currow - args))
- fi
- if [ "$torow" -ge 0 ]; then
- currow="$torow"
- redraw=1
- else
- error "Row out of range: '%s'" "$torow"
- fi
- ;;
- print|p)
- redraw=1
- ;;
- set)
- case "$args" in
- no*) value=0;;
- *) value=1;;
- esac
-
- case "$args" in
- *clear) clear=$value;;
- *msdos) msdos=$value;;
- "") printopt "clear"
- printopt "msdos";;
- *) error "Unknown parameter '%s'" "$args";;
- esac
- ;;
- *)
- error "Unknown command '%s'" "$cmd"
- ;;
- esac
-done
diff --git a/ste.1.in b/ste.1.in
@@ -6,6 +6,7 @@
.Nd Simple table editor
.Sh SYNOPSIS
.Nm
+.Op Fl hv
.Op Ar tsvfile.tsv
.Sh DESCRIPTION
.Nm
@@ -16,6 +17,17 @@ to display and a number of POSIX shell scripts, which use
.Xr vipe 1
from moreutils to edit files in TSV (Tab Separated Values) format.
.
+.Ss Command line options
+.Bl -tag -width ".Nm Fl h" -offset indent
+.
+.It Nm Fl h
+Prints usage information.
+.
+.It Nm Fl v
+Prints version and commit date.
+.
+.El
+.
.Ss Interactive commands
.Nm
accepts interactive commands in its command interface.
diff --git a/ste.in b/ste.in
@@ -0,0 +1,261 @@
+#!/bin/sh
+# vim: set ft=bash:
+# ste - Simple Table Editor
+# See the file LICENSE for copyright and license details.
+
+# Global options
+# Clear screen after update
+clear=0
+# Table output width in screen columns
+cols=80
+# Pass -m to table
+msdos=0
+# Pass -n to table (recommended)
+noansi=1
+
+# Which flavors of TSV/CSV utilities to use
+delprog=tsvdel
+editprog=tsvedit
+insprog=tsvins
+tableprog=tsvtable
+
+# Options which normally should not be modified by user
+DATE="%DATE%"
+VERSION="%VERSION%"
+
+error()
+{
+ fmt=$1
+ shift
+ # shellcheck disable=SC2059
+ printf "%s: %s\n" "$prog" "$(printf "$fmt" "$@")"
+}
+
+help()
+{
+ case "$1" in
+ =) printf "= - Show current row\n";;
+ cols) printf "cols [amount] - Set table width in screen columns\n";;
+ delete|x)
+ printf "delete | x - Delete the current table row\n";;
+ edit|e) printf "edit | e - Edit the current table row\n";;
+ exit|q) printf "exit | q - Exit %s\n" "$prog";;
+ G) printf "G - Set current row to last\n";;
+ g) printf "g [num] - Set current table row to num (header = 0)\n"
+ printf "g - Set current row to header row (0)\n";;
+ help|h) printf "help [command] | h [command] - Show help "
+ printf "(about a command)\n";;
+ insert|i)
+ printf "insert - | i - - Add a row to the table before the "
+ printf "current line\n"
+ printf "insert + | i + - Add a row to the table after the "
+ printf "current line\n"
+ printf "insert | i - Add a row to the end of table\n";;
+ j) printf "j [num] - Move the current row pointer down num rows\n"
+ printf "j - Move the current row pointer down\n";;
+ k) printf "k [num] - Move the current row pointer up num rows\n"
+ printf "k - Move the current row pointer up\n";;
+ print|p)
+ printf "print | p - Print (redraw) the table\n";;
+ set) printf "set [[no]option] - Turn option on/off\n"
+ printf "set - Show option states\n";;
+ "") printf "Commands: = cols delete e edit exit G g h help i "
+ printf "insert j k p print q set x\n"
+ printf "help [command] - Show help about a command\n";;
+ *) error "Unknown command '%s'" "$args";;
+ esac
+}
+
+printopt()
+{
+ opt=$1
+ if [ "$(eval echo "\$$opt")" -eq 0 ]; then
+ printf "no"
+ fi
+ printf "%s\n" "$opt"
+}
+
+setopts()
+{
+ opts=''
+ opts="$opts -c $cols"
+ if [ "$msdos" -eq 1 ]; then
+ opts="$opts -m"
+ fi
+ if [ "$noansi" -eq 1 ]; then
+ opts="$opts -n"
+ fi
+ printf "%s" "$opts"
+}
+
+usage()
+{
+ printf "Usage: %s [-h] [-v] [tsvfile.tsv]\n" "${prog}"
+}
+
+file=$1
+prog=${0##*/}
+
+OPTIND=1
+while getopts hv OPT >/dev/null 2>&1
+do
+ case $OPT in
+ h) usage
+ exit
+ ;;
+ v) printf "%s %s, committed on %s\n" \
+ "${prog}" "${VERSION}" "${DATE}"
+ exit
+ ;;
+ ?) error "Unknown parameter '%s'" "$1"
+ usage >&2
+ exit 1
+ ;;
+ esac
+ OPTARG=
+done
+
+if [ $# -lt 1 ]; then
+ usage >&2
+ exit 1
+fi
+
+if ! stat "$file" >/dev/null 2>&1; then
+ ${insprog} "$file"
+fi
+if ! stat "$file" >/dev/null 2>&1; then
+ error "File does not exist: '%s'" "$file"
+ exit 1
+fi
+
+continue=1
+currow=0
+redraw=1
+sgi=$(printf "\e[")
+
+while [ "$continue" -eq 1 ]; do
+ rows=$(wc -l "$file" | cut -d' ' -f1)
+ if [ "$redraw" -eq 1 ]; then
+ if [ "$clear" -eq 1 ]; then
+ clear
+ fi
+ # shellcheck disable=SC2046
+ ${tableprog} $(setopts) "$file" \
+ | awk '
+{
+ if (NR=='$currow'+2)
+ {
+ print '\""$sgi"'7m'\"' $0 '\""$sgi"'0m'\"'
+ }
+ else print
+}'
+ redraw=0
+ fi
+
+ printf "ste> "
+ read -r cmd
+
+ IFS=' '
+ set -- $cmd
+ cmd=$1
+ shift
+ args="$@"
+ case "$cmd" in
+ =) printf "Current row: %d\n" "$currow";;
+ cols) if [ -n "$args" ]; then
+ cols="$args"
+ redraw=1
+ else
+ error "Invalid argument: '%s'" "$args"
+ fi
+ ;;
+ delete|x)
+ ${delprog} "$file" "$((currow + 1))"
+ if [ "$currow" -eq "$((rows - 1))" ]; then
+ if [ "$rows" -ge 2 ]; then
+ currow=$((rows - 2))
+ else
+ currow=0
+ fi
+ fi
+ redraw=1
+ ;;
+ edit|e) ${editprog} "$file" "$((currow + 1))"
+ redraw=1
+ ;;
+ exit|q) continue=0
+ ;;
+ G) currow=$((rows - 1))
+ redraw=1
+ ;;
+ g) case "$args" in
+ "") currow=0
+ redraw=1;;
+ *) if [ "$args" -ge 0 ] && [ "$args" -lt "$rows" ]; then
+ currow="$args"
+ redraw=1
+ else
+ error "Row out of range: '%s'" "$args"
+ fi
+ esac
+ ;;
+ help|h|"")
+ help "$args"
+ ;;
+ insert|i)
+ case "$args" in
+ -) ${insprog} "$file" "$((currow))"
+ redraw=1;;
+ +) ${insprog} "$file" "$((currow + 1))"
+ currow=$((currow + 1))
+ redraw=1;;
+ "") ${insprog} "$file"
+ currow=$((rows))
+ redraw=1;;
+ *) error "Invalid argument: '%s'" "$args";;
+ esac
+ ;;
+ j) torow=$((currow + 1))
+ if [ -n "$args" ]; then
+ torow=$((currow + args))
+ fi
+ if [ "$torow" -lt "$rows" ]; then
+ currow="$torow"
+ redraw=1
+ else
+ error "Row out of range: '%s'" "$torow"
+ fi
+ ;;
+ k) torow=$((currow - 1))
+ if [ -n "$args" ]; then
+ torow=$((currow - args))
+ fi
+ if [ "$torow" -ge 0 ]; then
+ currow="$torow"
+ redraw=1
+ else
+ error "Row out of range: '%s'" "$torow"
+ fi
+ ;;
+ print|p)
+ redraw=1
+ ;;
+ set)
+ case "$args" in
+ no*) value=0;;
+ *) value=1;;
+ esac
+
+ case "$args" in
+ *clear) clear=$value;;
+ *msdos) msdos=$value;;
+ "") printopt "clear"
+ printopt "msdos";;
+ *) error "Unknown parameter '%s'" "$args";;
+ esac
+ ;;
+ *)
+ error "Unknown command '%s'" "$cmd"
+ ;;
+ esac
+done