summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2018-10-07 22:01:55 -0400
committerJakob Kaivo <jkk@ung.org>2018-10-07 22:01:55 -0400
commit548bdfbfdf5a25f04f2988b4a769570a60b54f18 (patch)
tree23753a77a6baa5b6a6cc979e898c5f30bdf838a5
parent8c6950f8b82a70c3ff5c32f768bb338b7356ef49 (diff)
add the script
-rwxr-xr-xcolor-ls153
1 files changed, 153 insertions, 0 deletions
diff --git a/color-ls b/color-ls
new file mode 100755
index 0000000..c84cc90
--- /dev/null
+++ b/color-ls
@@ -0,0 +1,153 @@
+#!/bin/sh
+# MIT License
+#
+# Copyright (c) 2018 Jakob Kaivo
+#
+# 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.
+
+output=-x # TODO: change this back to -C when that is fixed
+dash_F=-F
+dash_1=''
+
+_columns_down () {
+ awk -vwidest=0 '
+ {
+ if (length($0) > widest) {
+ widest = length($0);
+ }
+ all[NR] = $0;
+ }
+
+ END {
+ widest++;
+ column = 0;
+ columns = ENVIRON["COLUMNS"];
+ if (columns == 0) {
+ columns = 80;
+ }
+ ncolumns = columns / widest;
+
+ i = 0;
+ while (i < NR) {
+ # FIXME: this is all jacked up
+ printf("%-*s", widest, all[(row * ncolumns) + (i % ncolumns)]);
+ column += widest;
+ if (column > columns) {
+ printf("\n");
+ column = 0;
+ row++;
+ }
+ i++;
+ }
+
+ if (column != 0) {
+ printf("\n");
+ }
+ }'
+}
+
+_columns_across () {
+ awk -vwidest=0 '
+ {
+ if (length($0) > widest) {
+ widest = length($0);
+ }
+ all[NR] = $0;
+ }
+
+ END {
+ widest++;
+ column = 0;
+ columns = ENVIRON["COLUMNS"];
+ if (columns == 0) {
+ columns = 80;
+ }
+
+ for (i in all) {
+ printf("%-*s", widest, all[i]);
+ column += widest;
+ if (column > columns) {
+ printf("\n");
+ column = 0;
+ }
+ }
+
+ if (column != 0) {
+ printf("\n");
+ }
+ }'
+}
+
+_commas () {
+ awk -vcomma=0 '
+ {
+ printf("%s%s", comma == 1 ? ", " : "", $0 );
+ comma = 1;
+ }
+
+ END {
+ printf("\n");
+ }'
+}
+
+_output () {
+ case $output in
+ -C) cat | _columns_down;;
+ -x) cat | _columns_across;;
+ -m) cat | _commas;;
+ -1|-g|-l|-n|-o) cat;;
+ esac
+}
+
+_colorize () {
+ awk -vcolor=0 '
+ /\/$/ { color=34; }
+ /\*$/ { color=32; }
+ /\|$/ { color=33; }
+ /@$/ { color=35; }
+ /[^/@*|]$/ { color=39; }
+ { printf("\033\[%sm%s\033\[0m\n", color, $0); }'
+}
+
+_remove_symbols () {
+ if [ ! -z "$dash_F" ]; then
+ sed -e 's/[/*@|]\(.\[0m\)$/\1/g'
+ else
+ cat
+ fi
+}
+
+while getopts ACFHLRSacdfgiklmopqrstux1 option
+do
+ case $option in
+ g) output=-g; dash_1='';; # long
+ l) output=-l; dash_1='';; # long
+ n) output=-n; dash_1='';; # long
+ o) output=-o; dash_1='';; # long
+ 1) output=-1; dash_1='';; # 1 per line
+ C) output=-C; dash_1=-1;; # columns, sorted down
+ m) output=-m; dash_1=-1;; # comma separated stream
+ x) output=-x; dash_1=-1;; # columns, sorted across
+ F) dash_F='';;
+ ?) ;;
+ esac
+done
+
+# FIXME: the script returns 0 even if ls does not
+ls $dash_F ${1+$@} $dash_1 | _colorize | _remove_symbols $dash_F | _output $output