diff --git a/README.md b/README.md index c97851d..8fd8b3d 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ will create the symbolic link for each script in the \ directory. - [fsz](scripts/fsz) adjusts console font size. - [hr](scripts/hr) displays horizontal rule with current time. - [renamepdfs](scripts/renamepdfs) renames PDF files using their metadata. +- [ts](scripts/ts) adds timestamps to command’s output. - [vcat](scripts/vcat) displays file content with [syntax highlighting](https://www.gnu.org/software/src-highlite/) (see also [bat](https://github.com/sharkdp/bat)). - [viper](scripts/viper) is a simple alternative to viper from [moreutils](https://joeyh.name/code/moreutils/) package which can be used with custom vim parameters. diff --git a/scripts/ts b/scripts/ts new file mode 100755 index 0000000..e2faf40 --- /dev/null +++ b/scripts/ts @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +# Add timestamp to each line of the text output as moreutils/ts does. + +rstc="\033[00m" +colour="\033[1;36m" # blue + +usage() { + echo "USAGE: ${0##*/} [-n] [DATE FORMAT]" + echo " e.g. \`ping 127.0.0.1 | ts [%T]'" +} + +for arg in "$@"; do + case "$arg" in + -h) usage; exit 0;; + -n) colour=""; rstc="";; + *) date_fmt="${arg}";; + esac +done + +DATE_FORMAT="${date_fmt:-[%Y-%m-%d %T]}" + +awk -v fmt="$DATE_FORMAT" -v colour="$colour" -v rstc="$rstc" '{ + printf("%s%s%s %s\n", colour, strftime(fmt), rstc, $0) +}' +