├── README ├── LICENSE └── barchart /README: -------------------------------------------------------------------------------- 1 | BARCHART 2 | ======== 3 | 4 | This program makes bar charts. For example, take this file 'data': 5 | 6 | 33 This is a label 7 | 0 This is another 8 | 100 9 | 80 The line above had no label 10 | 11 | Running './barchart -w 80 data' would produce: 12 | 13 | 33 This is a label ################ 14 | 0 This is another 15 | 100 ################################################ 16 | 80 The line above had no label ####################################### 17 | 18 | The script should work with any POSIX shell. However, it can only automatically 19 | detect terminal width if the 'size' operand to 'stty' is present and the 20 | controlling terminal is available as '/dev/tty'. 21 | 22 | This script integrates well into pipelines. For example, this makes a bar chart 23 | of line counts for all source files in 'src': 24 | 25 | find -L src -type f -exec wc -l {} \; | sort -rn | barchart | less 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Jude Melton-Houghton 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /barchart: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | char=# 4 | [ -r /dev/tty ] && cols=$(stty /dev/null | awk '{print $2}') 5 | [ -n "$cols" ] || cols=80 6 | while [ 0 -lt "$#" ]; do 7 | opt="$1" 8 | case "$opt" in 9 | -c) shift; char="$1" ;; 10 | -c*) char="${1##-c}" ;; 11 | -w) shift; cols="$1" ;; 12 | -w*) cols="${1##-w}" ;; 13 | -v) echo 'barchart version 1.0.5'; exit 0 ;; 14 | -h) cat <&2 "$0: Invalid option: $opt"; exit 1 ;; 27 | *) break ;; 28 | esac 29 | [ 0 -lt "$#" ] || { 30 | echo >&2 "$0: Option $opt requires an argument" 31 | exit 1 32 | } 33 | shift 34 | done 35 | 36 | exec awk -v char="$char" -v cols="$cols" ' 37 | { count = +$1; 38 | $1 = ""; 39 | label = substr($0, 2); 40 | sum += count; 41 | if (count > max) max = count; 42 | l = length(""count); 43 | if (l > countlen) countlen = l; 44 | l = length(label); 45 | if (l > labellen) labellen = l; 46 | counts[+n] = count; 47 | labels[+n] = label; 48 | ++n } 49 | END { width = cols - countlen - 1 - labellen - 1; 50 | for (i = 0; i < +n; ++i) { 51 | printf "%"countlen"d %-"labellen"s ", counts[i], labels[i]; 52 | if (max > width) counts[i] = counts[i] / max * width; 53 | while (counts[i]-- > 0) printf "%s", char; 54 | printf "\n"; 55 | } }' "$@" 56 | --------------------------------------------------------------------------------