#!/bin/sh # The MIT License (MIT) # # Copyright (c) 2026 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # nts [options...] [files...] # # # Nice TimeStamp styles the leading timestamp on lines using ANSI codes, # using a tab to separate it from the line content. File-input is available, # even if typical uses-cases are for timestamping live stdin lines. # # The options are, available both in single and double-dash versions # # -h, -help show this help message # # -plain don't style timestamps # # -b, -blue use a blue style # -gray use a gray style # -g, -green use a green style # -o, -orange use an orange style # -p, -purple use a purple style # -r, -red use a red style # # -bb, -blueback use a blue-background style # -grayback use a gray-background style # -gb, -greenback use a green-background style # -ob, -orangeback use an orange-background style # -pb, -purpleback use a purple-background style # -rb, -redback use a red-background style style='\x1b[48;2;218;218;218m\x1b[38;2;0;95;153m' case "$1" in -h|--h|-help|--help) awk '/^# +nts /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; -b|--b|-blue|--blue) style='\x1b[38;2;0;95;215m' shift ;; -bb|--bb|-blueback|--blueback) style='\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m' shift ;; -bold|--bold|-bolded|--bolded) style='\x1b[1m' shift ;; -dim|--dim|-faint|--faint|-gray|--gray) style='\x1b[38;2;168;168;168m' shift ;; -grayback|--grayback) style='\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m' shift ;; -g|--g|-green|--green) style='\x1b[38;2;0;135;95m' shift ;; -gb|--gb|-greenback|--greenback) style='\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m' shift ;; -i|--i|-inv|--inv|-inverse|--inverse|-invert|--invert|-inverted|--inverted) style='\x1b[7m' shift ;; -o|--o|-orange|--orange) style='\x1b[38;2;215;95;0m' shift ;; -ob|--ob|-orangeback|--orangeback) style='\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m' shift ;; -p|--p|-purple|--purple) style='\x1b[38;2;135;95;255m' shift ;; -plain|--plain) style='' shift ;; -pb|--pb|-purpleback|--purpleback) style='\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m' shift ;; -r|--r|-red|--red) style='\x1b[38;2;204;0;0m' shift ;; -rb|--rb|-redback|--redback) style='\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m' shift ;; -u|--u|-underline|--underline|-underlined|--underlined) style='\x1b[4m' shift ;; esac [ "$1" = '--' ] && shift flush=0 if [ -p /dev/stdout ] || [ -t 1 ]; then flush=1 fi # show all non-existing files given failed=0 for arg in "$@"; do if [ "${arg}" = "-" ]; then continue fi if [ ! -e "${arg}" ]; then printf "no file named \"%s\"\n" "${arg}" > /dev/stderr failed=1 fi done if [ "${failed}" -gt 0 ]; then exit 2 fi # busybox's `ts` doesn't update/flush lines live, so avoid `ts` just in case awk -v style="${style}" -v flush="${flush}" ' BEGIN { fmt = (style == "") ? "%s%s\t" : "%s%s\x1b[0m\t" } { printf fmt, style, strftime("%Y-%m-%d %H:%M:%S") print if (flush) fflush() } ' "$@"