ste

Једноставни уређивач табела
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување 6b5112b516b743c1840be30f710e98ebc8144625
родитељ 544fd1a167546308c1711a35359b1d5fe85d39e7
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Sun,  3 Sep 2023 18:13:22 +0200

Add rows command; handle large tables

Signed-off-by: Страхиња Радић <contact@strahinja.org>

Diffstat:
MTODO | 10++++++----
Mste.1.in | 17+++++++++++++++--
Mste.in | 100++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
измењених датотека: 3, додавања: 102(+), брисања: 25(-)

diff --git a/TODO b/TODO @@ -9,13 +9,15 @@ TODO [x] Add command to use the script to ste [x] Update manpage -[ ] Handle large tables - [ ] Add `lines` command similar to `cols` - < > Always output header? - Would require special handling of currently +[x] Handle large tables + [x] Add `lines` command similar to `cols` + </> Always output header? - Would require special handling of currently selected row. By going up from first displayed row [n] after header, how would one go to [n-1] while keeping the ability to go to header row? Hence, this is maybe not a good idea - header row either won't be output when scrolled past a screen, or it would be - unselectable/uneditable. I lean towards it not being output. + unselectable/uneditable. (Also, header columns are shown as comments + when editing a row anyway.) I lean towards it not being output. + - Ultimately chose not to output header. < > Command to replace a cell with [string]? diff --git a/ste.1.in b/ste.1.in @@ -39,11 +39,14 @@ Accepted commands are: Show current row. . .It Ic cols Op Ar amount -Set table width in screen columns to +Set screen width in columns to .Ar amount . +The value of 0 causes +.Nm +to autodetect screen width on each cycle. If .Ar amount -is omitted, show current table width. +is omitted, show current screen width. . .It Ic delete | Ic x Delete the current table row. @@ -115,6 +118,16 @@ is omitted, move the row pointer up. .It Ic print | Ic p Print (redraw) the table. . +.It Ic rows Op Ar amount +Set screen height in lines to +.Ar amount . +The value of 0 causes +.Nm +to autodetect screen height on each cycle. +If +.Ar amount +is omitted, show current screen height. +. .It Ic set Op Ar [no]option Turn option on/off. If argument is omitted, show all current option states. diff --git a/ste.in b/ste.in @@ -6,8 +6,10 @@ # Global options # Clear screen after update clear=1 -# Table output width in screen columns -cols=80 +# Table output width in screen columns (0 = detect) +cols=0 +# Table output height in screen rows (0 = detect) +rows=0 # Pass -m to table msdos=0 # Pass -n to table (recommended) @@ -32,12 +34,23 @@ error() printf "%s: %s\n" "$prog" "$(printf "$fmt" "$@")" } +detectcols() +{ + stty -a | tr \; '\n' | grep -E 'colu?m?n?s' | tr -d 'columns ' +} + +detectrows() +{ + stty -a | tr \; '\n' | grep rows | tr -d 'rows ' +} + help() { case "$1" in =) printf "= - Show current row\n";; - cols) printf "cols [amount] - Set table width in screen columns\n" - printf "cols - Show the current table width\n";; + cols) printf "cols [amount] - Set screen width in columns\n" + printf " (0 = autodetect)\n" + printf "cols - Show the current screen width\n";; delete|x) printf "delete | x - Delete the current table row\n";; edit|e) printf "edit | e - Edit the current table row\n";; @@ -63,10 +76,13 @@ help() printf "k - Move the current row pointer up\n";; print|p) printf "print | p - Print (redraw) the table\n";; + rows) printf "rows [amount] - Set screen height in lines\n" + printf " (0 = autodetect)\n" + printf "rows - Show the current screen height\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 j K k p print q set x\n" + printf "insert J j K k p print q rows set x\n" printf "help [command] - Show help about a command\n";; *) error "Unknown command '%s'" "$args";; esac @@ -84,7 +100,7 @@ printopt() setopts() { opts='' - opts="$opts -c $cols" + opts="$opts -c $_cols" if [ "$msdos" -eq 1 ]; then opts="$opts -m" fi @@ -136,26 +152,53 @@ fi continue=1 currow=0 +firstrow=0 redraw=1 sgi=$(printf "%b" "\033[") while [ "$continue" -eq 1 ]; do - rows=$(wc -l "$file" | cut -d' ' -f1) + maxrows=$(wc -l "$file" | cut -d' ' -f1) + + _cols="$cols" + if [ "$cols" -eq 0 ]; then + _cols=$(detectcols) + redraw=1 + fi + _rows="$rows" + if [ "$rows" -eq 0 ]; then + _rows=$(detectrows) + redraw=1 + fi + + if [ "$currow" -lt "$firstrow" ]; then + firstrow="$currow" + redraw=1 + elif [ $((currow - firstrow)) -gt $((_rows - 4)) ]; then + firstrow=$((currow - _rows + 4)) + if [ "$firstrow" -lt 0 ]; then + firstrow="$currow" + fi + redraw=1 + fi if [ "$redraw" -eq 1 ]; then if [ "$clear" -eq 1 ]; then clear fi # shellcheck disable=SC2046 - ${tableprog} $(setopts) "$file" | + tail -n+$((firstrow+1)) "$file" | + head -n$((_rows-3)) | + ${tableprog} $(setopts) | awk ' { - if (NR=='$currow'+2) print '\""$sgi"'7m'\"' $0 '\""$sgi"'0m'\"' + if (NR=='$((currow-firstrow))'+2) + print '\""$sgi"'7m'\"' $0 '\""$sgi"'0m'\"' else print }' redraw=0 fi - printf "ste> " + printf "ste[%dx%d+%d@%d/%d]> " \ + "$_cols" "$_rows" "$firstrow" "$currow" "$maxrows" read -r cmd IFS=' ' @@ -171,16 +214,21 @@ while [ "$continue" -eq 1 ]; do case "$cmd" in =) printf "Current row: %d\n" "$currow";; cols) case "$args" in - "") printf "Current table width: %d\n" "$cols";; + "") if [ "$cols" -eq 0 ]; then + printf "Current table width: %d (autodetected)"\ +"\n" "$_cols" + else + printf "Current table width: %d\n" "$cols" + fi;; *) cols="$args" redraw=1;; esac ;; delete|x) ${delprog} "$file" "$((currow + 1))" - if [ "$currow" -eq "$((rows - 1))" ]; then - if [ "$rows" -ge 2 ]; then - currow=$((rows - 2)) + if [ "$currow" -eq "$((maxrows - 1))" ]; then + if [ "$maxrows" -ge 2 ]; then + currow=$((maxrows - 2)) else currow=0 fi @@ -192,13 +240,13 @@ while [ "$continue" -eq 1 ]; do ;; exit|q) continue=0 ;; - G) currow=$((rows - 1)) + G) currow=$((maxrows - 1)) redraw=1 ;; g) case "$args" in "") currow=0 redraw=1;; - *) if [ "$args" -ge 0 ] && [ "$args" -lt "$rows" ]; then + *) if [ "$args" -ge 0 ] && [ "$args" -lt "$maxrows" ]; then currow="$args" redraw=1 else @@ -217,7 +265,7 @@ while [ "$continue" -eq 1 ]; do currow=$((currow + 1)) redraw=1;; "") ${insprog} "$file" - currow=$((rows)) + currow=$((maxrows)) redraw=1;; *) error "Invalid argument: '%s'" "$args";; esac @@ -226,7 +274,7 @@ while [ "$continue" -eq 1 ]; do if [ -n "$args" ]; then torow=$((currow + args)) fi - if [ "$torow" -lt "$rows" ]; then + if [ "$torow" -lt "$maxrows" ]; then ${moveprog} "$file" "$((currow+1))" "$((torow+1))" currow="$torow" redraw=1 @@ -238,7 +286,7 @@ while [ "$continue" -eq 1 ]; do if [ -n "$args" ]; then torow=$((currow + args)) fi - if [ "$torow" -lt "$rows" ]; then + if [ "$torow" -lt "$maxrows" ]; then currow="$torow" redraw=1 else @@ -271,6 +319,20 @@ while [ "$continue" -eq 1 ]; do print|p) redraw=1 ;; + rows) case "$args" in + "") if [ "$rows" -eq 0 ]; then + printf "Current screen height: %d"\ +" (autodetected)\n" "$_rows" + else + printf "Current screen height: %d\n" "$rows" + fi;; + *) rows="$args" + if [ "$rows" -ne 0 ] && [ "$rows" -lt 4 ]; then + rows=4 + fi + redraw=1;; + esac + ;; set) case "$args" in no*) value=0;;