File: clean.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright (c) 2026 pacman64 6 # 7 # Permission is hereby granted, free of charge, to any person obtaining a copy 8 # of this software and associated documentation files (the "Software"), to deal 9 # in the Software without restriction, including without limitation the rights 10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 # copies of the Software, and to permit persons to whom the Software is 12 # furnished to do so, subject to the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be included in 15 # all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 # SOFTWARE. 24 25 26 # clean [options...] [command] [args...] 27 # 28 # 29 # CLEAN the screen, after running the command given. When its standard output 30 # is piped, no cleaning happens, and this script becomes a passthru. 31 # 32 # This tool doesn't clear the screen, but rather uses the alternative screen 33 # buffer, so the original content of the terminal is restored when done. 34 # 35 # The options are, available both in single and double-dash versions 36 # 37 # -h, -help show this help message 38 # -p [msg] wait/prompt for the enter key before quitting 39 # -pause wait/prompt for the enter key before quitting 40 # -v, -verbose show styled banner of command run 41 # -w, -when also show when command was run, if in verbose mode 42 43 44 when=0 45 pause=0 46 prompt='' 47 verbose=0 48 49 while [ $# -gt 0 ]; do 50 case "$1" in 51 -h|--h|-help|--help) 52 awk '/^# +clean /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 53 exit 0 54 ;; 55 56 -p|--p) 57 if [ $# -lt 2 ]; then 58 printf "%s: expected prompt message after %s option\n" \ 59 "$0" "$1" >&2 60 exit 1 61 fi 62 pause=1 63 if [ "$2" = '--' ]; then 64 shift 2; break 65 fi 66 prompt="$2"; shift 2; continue 67 ;; 68 69 -pause|--pause) pause=1; shift; continue ;; 70 71 -v|--v|-verbose|--verbose) verbose=1; shift; continue ;; 72 73 -w|--w|-when|--when) when=1; shift; continue ;; 74 75 --) shift; break ;; 76 77 -*) 78 printf "%s: unsupported option %s\n" "$0" "$1" >&2 79 exit 1 80 ;; 81 esac 82 83 break 84 done 85 86 if [ $# -eq 0 ]; then 87 awk '/^# +clean /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2 88 exit 1 89 fi 90 91 title='' 92 if [ "${verbose}" -eq 1 ]; then 93 if [ "${when}" -eq 1 ]; then 94 # d="$(date)" 95 # d="$(date +'%Y-%m-%d %H:%M:%S %a %b %d')" 96 d="$(date +'%Y-%m-%d %H:%M:%S')" 97 title="$(printf "%s %s" "${d}" "$*")" 98 else 99 title="$(printf "%s" "$*")" 100 fi 101 fi 102 103 prompt="${prompt:-Press the Enter key to continue...}" 104 105 # if stdout is being piped, avoid alternate-screening 106 if [ -p /dev/stdout ]; then 107 [ "${verbose}" -eq 1 ] && printf "\033[7m%-80s\033[0m\n" "${title}" 108 109 if [ "${pause}" -eq 0 ]; then 110 exec "$@" 111 fi 112 113 "$@" 114 res=$? 115 printf "\n%s" "${prompt}" 116 read -r ignored 117 exit "${res}" 118 fi 119 120 # restore previous screen contents when sent a SIGINT signal, which usually 121 # happens by pressing Ctrl+C 122 trap ' 123 tput -T xterm rmcup 2> /dev/null || printf "\033[?1049l\033[23;0;0t" 124 exit 130 125 ' INT 126 127 tput -T xterm smcup 2> /dev/null || printf "\033[?1049h\033[22;0;0t" 128 printf "\033[H" # move cursor to the top-left corner 129 130 [ "${verbose}" -eq 1 ] && printf "\033[7m%-80s\033[0m\n" "${title}" 131 132 "$@" 133 res=$? 134 135 if [ "${pause}" -eq 1 ]; then 136 printf "\n%s" "${prompt}" 137 read -r ignored 138 fi 139 140 tput -T xterm rmcup 2> /dev/null || printf "\033[?1049l\033[23;0;0t" 141 142 exit "${res}"