ste (4686B)
1 #!/bin/sh 2 # vim: set ft=bash: 3 # ste - Simple Table Editor 4 # See the file LICENSE for copyright and license details. 5 6 # Global options 7 # Clear screen after update 8 clear=0 9 # Table output width in screen columns 10 cols=80 11 # Pass -m to table 12 msdos=0 13 # Pass -n to table (recommended) 14 noansi=1 15 16 # Which flavors of TSV/CSV utilities to use 17 delprog=tsvdel 18 editprog=tsvedit 19 insprog=tsvins 20 tableprog=tsvtable 21 22 error() 23 { 24 fmt=$1 25 shift 26 # shellcheck disable=SC2059 27 printf "%s: %s\n" "$prog" "$(printf "$fmt" "$@")" 28 } 29 30 help() 31 { 32 case "$1" in 33 =) printf "= - Show current row\n";; 34 cols) printf "cols [amount] - Set table width in screen columns\n";; 35 delete|x) 36 printf "delete | x - Delete the current table row\n";; 37 edit|e) printf "edit | e - Edit the current table row\n";; 38 exit|q) printf "exit | q - Exit %s\n" "$prog";; 39 G) printf "G - Set current row to last\n";; 40 g) printf "g [num] - Set current table row to num (header = 0)\n" 41 printf "g - Set current row to header row (0)\n";; 42 help|h) printf "help [command] | h [command] - Show help " 43 printf "(about a command)\n";; 44 insert|i) 45 printf "insert - | i - - Add a row to the table before the " 46 printf "current line\n" 47 printf "insert + | i + - Add a row to the table after the " 48 printf "current line\n" 49 printf "insert | i - Add a row to the end of table\n";; 50 j) printf "j [num] - Move the current row pointer down num rows\n" 51 printf "j - Move the current row pointer down\n";; 52 k) printf "k [num] - Move the current row pointer up num rows\n" 53 printf "k - Move the current row pointer up\n";; 54 print|p) 55 printf "print | p - Print (redraw) the table\n";; 56 set) printf "set [[no]option] - Turn option on/off\n" 57 printf "set - Show option states\n";; 58 "") printf "Commands: = cols delete e edit exit G g h help i " 59 printf "insert j k p print q set x\n" 60 printf "help [command] - Show help about a command\n";; 61 *) error "Unknown command '%s'" "$args";; 62 esac 63 } 64 65 printopt() 66 { 67 opt=$1 68 if [ "$(eval echo "\$$opt")" -eq 0 ]; then 69 printf "no" 70 fi 71 printf "%s\n" "$opt" 72 } 73 74 setopts() 75 { 76 opts='' 77 opts="$opts -c $cols" 78 if [ "$msdos" -eq 1 ]; then 79 opts="$opts -m" 80 fi 81 if [ "$noansi" -eq 1 ]; then 82 opts="$opts -n" 83 fi 84 printf "%s" "$opts" 85 } 86 87 file=$1 88 prog=${0##*/} 89 90 if [ $# -lt 1 ]; then 91 error "Usage: %s [file]" "$prog" 92 exit 1 93 fi 94 95 if ! stat "$file" >/dev/null 2>&1; then 96 ${insprog} "$file" 97 fi 98 if ! stat "$file" >/dev/null 2>&1; then 99 error "File does not exist: '%s'" "$file" 100 exit 1 101 fi 102 103 continue=1 104 currow=0 105 redraw=1 106 sgi=$(printf "\e[") 107 108 while [ "$continue" -eq 1 ]; do 109 rows=$(wc -l "$file" | cut -d' ' -f1) 110 if [ "$redraw" -eq 1 ]; then 111 if [ "$clear" -eq 1 ]; then 112 clear 113 fi 114 # shellcheck disable=SC2046 115 ${tableprog} $(setopts) "$file" \ 116 | awk ' 117 { 118 if (NR=='$currow'+2) 119 { 120 print '\""$sgi"'7m'\"' $0 '\""$sgi"'0m'\"' 121 } 122 else print 123 }' 124 redraw=0 125 fi 126 127 printf "ste> " 128 read -r cmd 129 130 IFS=' ' 131 set -- $cmd 132 cmd=$1 133 shift 134 args="$@" 135 case "$cmd" in 136 =) printf "Current row: %d\n" "$currow";; 137 cols) if [ -n "$args" ]; then 138 cols="$args" 139 redraw=1 140 else 141 error "Invalid argument: '%s'" "$args" 142 fi 143 ;; 144 delete|x) 145 ${delprog} "$file" "$((currow + 1))" 146 if [ "$currow" -eq "$((rows - 1))" ]; then 147 if [ "$rows" -ge 2 ]; then 148 currow=$((rows - 2)) 149 else 150 currow=0 151 fi 152 fi 153 redraw=1 154 ;; 155 edit|e) ${editprog} "$file" "$((currow + 1))" 156 redraw=1 157 ;; 158 exit|q) continue=0 159 ;; 160 G) currow=$((rows - 1)) 161 redraw=1 162 ;; 163 g) case "$args" in 164 "") currow=0 165 redraw=1;; 166 *) if [ "$args" -ge 0 ] && [ "$args" -lt "$rows" ]; then 167 currow="$args" 168 redraw=1 169 else 170 error "Row out of range: '%s'" "$args" 171 fi 172 esac 173 ;; 174 help|h|"") 175 help "$args" 176 ;; 177 insert|i) 178 case "$args" in 179 -) ${insprog} "$file" "$((currow))" 180 redraw=1;; 181 +) ${insprog} "$file" "$((currow + 1))" 182 currow=$((currow + 1)) 183 redraw=1;; 184 "") ${insprog} "$file" 185 currow=$((rows)) 186 redraw=1;; 187 *) error "Invalid argument: '%s'" "$args";; 188 esac 189 ;; 190 j) torow=$((currow + 1)) 191 if [ -n "$args" ]; then 192 torow=$((currow + args)) 193 fi 194 if [ "$torow" -lt "$rows" ]; then 195 currow="$torow" 196 redraw=1 197 else 198 error "Row out of range: '%s'" "$torow" 199 fi 200 ;; 201 k) torow=$((currow - 1)) 202 if [ -n "$args" ]; then 203 torow=$((currow - args)) 204 fi 205 if [ "$torow" -ge 0 ]; then 206 currow="$torow" 207 redraw=1 208 else 209 error "Row out of range: '%s'" "$torow" 210 fi 211 ;; 212 print|p) 213 redraw=1 214 ;; 215 set) 216 case "$args" in 217 no*) value=0;; 218 *) value=1;; 219 esac 220 221 case "$args" in 222 *clear) clear=$value;; 223 *msdos) msdos=$value;; 224 "") printopt "clear" 225 printopt "msdos";; 226 *) error "Unknown parameter '%s'" "$args";; 227 esac 228 ;; 229 *) 230 error "Unknown command '%s'" "$cmd" 231 ;; 232 esac 233 done