чување 13fe173c322f8e83fcd9c55ee21acf273fb9d958 родитељ 41f76cdae72d475ae9f3d044d3291a57b0f83679 Аутор: Страхиња Радић <contact@strahinja.org> Датум: Sun, 18 Jun 2023 16:56:59 +0200 Remove ste from .gitignore, add ste itself Signed-off-by: Страхиња Радић <contact@strahinja.org> Diffstat:
| M | .gitignore | | | 1 | - |
| A | ste | | | 233 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
измењених датотека: 2, додавања: 233(+), брисања: 1(-)
diff --git a/.gitignore b/.gitignore @@ -16,7 +16,6 @@ nohup.out *.gz *.xz config.redo -ste ste.1 ste.session compile_commands.json diff --git a/ste b/ste @@ -0,0 +1,233 @@ +#!/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