File: nts.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 # nts [options...] [files...] 27 # 28 # 29 # Nice TimeStamp styles the leading timestamp on lines using ANSI codes, 30 # using a tab to separate it from the line content. File-input is available, 31 # even if typical uses-cases are for timestamping live stdin lines. 32 # 33 # The options are, available both in single and double-dash versions 34 # 35 # -h, -help show this help message 36 # 37 # -plain don't style timestamps 38 # 39 # -b, -blue use a blue style 40 # -gray use a gray style 41 # -g, -green use a green style 42 # -o, -orange use an orange style 43 # -p, -purple use a purple style 44 # -r, -red use a red style 45 # 46 # -bb, -blueback use a blue-background style 47 # -grayback use a gray-background style 48 # -gb, -greenback use a green-background style 49 # -ob, -orangeback use an orange-background style 50 # -pb, -purpleback use a purple-background style 51 # -rb, -redback use a red-background style 52 53 54 style='\x1b[48;2;218;218;218m\x1b[38;2;0;95;153m' 55 56 case "$1" in 57 -h|--h|-help|--help) 58 awk '/^# +nts /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 59 exit 0 60 ;; 61 62 -b|--b|-blue|--blue) 63 style='\x1b[38;2;0;95;215m' 64 shift 65 ;; 66 67 -bb|--bb|-blueback|--blueback) 68 style='\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m' 69 shift 70 ;; 71 72 -bold|--bold|-bolded|--bolded) 73 style='\x1b[1m' 74 shift 75 ;; 76 77 -dim|--dim|-faint|--faint|-gray|--gray) 78 style='\x1b[38;2;168;168;168m' 79 shift 80 ;; 81 82 -grayback|--grayback) 83 style='\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m' 84 shift 85 ;; 86 87 -g|--g|-green|--green) 88 style='\x1b[38;2;0;135;95m' 89 shift 90 ;; 91 92 -gb|--gb|-greenback|--greenback) 93 style='\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m' 94 shift 95 ;; 96 97 -i|--i|-inv|--inv|-inverse|--inverse|-invert|--invert|-inverted|--inverted) 98 style='\x1b[7m' 99 shift 100 ;; 101 102 -o|--o|-orange|--orange) 103 style='\x1b[38;2;215;95;0m' 104 shift 105 ;; 106 107 -ob|--ob|-orangeback|--orangeback) 108 style='\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m' 109 shift 110 ;; 111 112 -p|--p|-purple|--purple) 113 style='\x1b[38;2;135;95;255m' 114 shift 115 ;; 116 117 -plain|--plain) 118 style='' 119 shift 120 ;; 121 122 -pb|--pb|-purpleback|--purpleback) 123 style='\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m' 124 shift 125 ;; 126 127 -r|--r|-red|--red) 128 style='\x1b[38;2;204;0;0m' 129 shift 130 ;; 131 132 -rb|--rb|-redback|--redback) 133 style='\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m' 134 shift 135 ;; 136 137 -u|--u|-underline|--underline|-underlined|--underlined) 138 style='\x1b[4m' 139 shift 140 ;; 141 esac 142 143 [ "$1" = '--' ] && shift 144 145 flush=0 146 if [ -p /dev/stdout ] || [ -t 1 ]; then 147 flush=1 148 fi 149 150 # show all non-existing files given 151 failed=0 152 for arg in "$@"; do 153 if [ "${arg}" = "-" ]; then 154 continue 155 fi 156 if [ ! -e "${arg}" ]; then 157 printf "no file named \"%s\"\n" "${arg}" > /dev/stderr 158 failed=1 159 fi 160 done 161 162 if [ "${failed}" -gt 0 ]; then 163 exit 2 164 fi 165 166 # busybox's `ts` doesn't update/flush lines live, so avoid `ts` just in case 167 168 awk -v style="${style}" -v flush="${flush}" ' 169 BEGIN { fmt = (style == "") ? "%s%s\t" : "%s%s\x1b[0m\t" } 170 171 { 172 printf fmt, style, strftime("%Y-%m-%d %H:%M:%S") 173 print 174 if (flush) fflush() 175 } 176 ' "$@"