Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

print column numbers in some semi-useful way, statically.

  • Loading branch information...
commit 97ed4dbefbc86ba34f68519e4f9d56ce86963342 1 parent 1ea541a
Jonathan Zacsh authored

Showing 1 changed file with 71 additions and 0 deletions. Show diff stats Hide diff stats

  1. 71  share/printcols
71  share/printcols
... ...
@@ -0,0 +1,71 @@
  1
+#!/usr/bin/env bash
  2
+
  3
+# Column printer for the top of files (used *one* time in a small reporter).
  4
+
  5
+#Sanity checks
  6
+if [[ "$BASH_VERSINFO" -lt 4 ]];then
  7
+  printf 'Bash V4 features heavily used, Bash 4 required.\n' >&2
  8
+  exit 1
  9
+fi
  10
+(( $# < 3 )) || {
  11
+  _n="$(basename "$0")"; _n="${n,,}"
  12
+  echo 'usage: %s NUM_COLS [-d]
  13
+  NUM_COLS number of columns to label
  14
+  -d\tEnable debugging
  15
+
  16
+  Example: `%s 80` will render:
  17
+  1 3 5 7 9 13| 17|
  18
+  Where the pipe character, |, marks the exact location of the column number.
  19
+  ' "$_n" "$_n" >&2
  20
+  exit 2
  21
+}
  22
+
  23
+NUM_COLS=$(( 10#${1:-80} ))
  24
+
  25
+# enable debugging
  26
+if [[ $2 = -d ]];then
  27
+  PRINT_COLS_DEBUG=1
  28
+fi
  29
+
  30
+printer_spacing() {
  31
+  col=$(( 10#${1} ))
  32
+  if (( $col > 9 )); then
  33
+    if [[ $2 = 'prepare' ]];then
  34
+      printf '%d|' $col
  35
+    else
  36
+      printf '4'
  37
+    fi
  38
+  else
  39
+    if [[ $2 = 'prepare' ]];then
  40
+      printf '%d' $col
  41
+    else
  42
+      printf '2'
  43
+    fi
  44
+  fi
  45
+}
  46
+
  47
+if (( PRINT_COLS_DEBUG ));then
  48
+  seq $NUM_COLS | while read col; do
  49
+    printf '%s' "$col" >&2
  50
+  done
  51
+  printf '\n'
  52
+
  53
+fi
  54
+
  55
+seq $NUM_COLS | while read col; do
  56
+  column=$(( 10#${col} ))
  57
+
  58
+  if (( column == 1 ));then
  59
+    printf "%s" "$column"
  60
+  else
  61
+    width=$(( 10#$(printer_spacing "$column") ))
  62
+    cleared=$(( $(( column -1 )) % width ))
  63
+
  64
+    # DEBUG:   printf '%d cleared: %d\n' "$column" $cleared >&2 # DONOTSUBMIT
  65
+    if (( cleared == 0 )); then
  66
+      modded="$(printer_spacing "$column" 'prepare')"
  67
+      printf "%${width}s" "$modded"
  68
+    fi
  69
+  fi
  70
+done
  71
+printf '\n'

0 notes on commit 97ed4db

Please sign in to comment.
Something went wrong with that request. Please try again.