1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-05 07:29:04 -08:00
rc/sh/trunc

19 lines
653 B
Bash
Executable file

#!/usr/bin/env sh
# compat: +ash +bash +dash +hush +ksh +mksh +oksh +osh +posh +yash +zsh YES_BB_AWK
trunc() { ### @-
### truncate text to fit within your terminal using the unicode character `…`.
###
### ```
### $ echo $COLUMNS
### 84
### $ seq 1 100 | tr '\n' ' ' | trunc
### 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31…
### ```
[ $# -le 1 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; }
COLUMNS="${COLUMNS:-"$(tput cols)"}"
awk -vL="${1:-$COLUMNS}" '{e=length()>L?"…":"";print substr($0,0,L-(e?1:0)) e}'
}
[ -n "${preload+-}" ] || trunc "$@"