-
Notifications
You must be signed in to change notification settings - Fork 0
/
len
executable file
·41 lines (40 loc) · 1.08 KB
/
len
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
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
source /home/fabian/d/programs/bash_scripts/sane
# split arguments and on \n, but not on spaces
IFS=$'\n'
# disable * expansion
set -f
if [[ "$1" =~ ^(\-\-?|\/)?(h(elp)?|\?)$ && "$2" == "" ]]; then
wrap "Counts characters in a string. Like \"wc -m\", but without counting escape sequences."
wrap "Only the longest line length will be returned, in case of line breaks or multiple arguments."
wrap "Example usage:"
echo "len \"a\\\\nbcd\\\\ne f\" \"ghij\\\\ng\\\\e[101mh\\\\e[0mi\""
echo "Output:"
len "a\nbcd\ne f" "ghij\ng\e[101mh\e[0mi"
wrap "Here the length of the line \"ghij\" was returned."
exit
fi
max=0
for line in $*; do
arr=($(echo -n "$line" | sed "s/(.)/\\1\\n/g"))
esc=0
for((pos=0; pos<${#arr[@]}; pos++)); do
# "\em"…
if [[ "${arr[pos]}" == $'\e' ]]; then
((esc+=2))
((pos++))
# "\e[m"…
if [[ "${arr[pos]}" == "[" ]]; then
((esc++))
((pos++))
# "\e[0m"…/"\e[93m"…/"\e[101m"…
while [[ "${arr[pos]}" =~ [0-9] ]]; do
((esc++))
((pos++))
done
fi
fi
done
((max=${#arr[@]}-esc>max?${#arr[@]}-esc:max))
done
echo $max