/* The MIT License (MIT) Copyright (c) 2026 pacman64 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. */ /* To compile a smaller-sized command-line app, you can use the `go` command as follows: go build -ldflags "-s -w" -trimpath ncol.go */ package main import ( "bufio" "errors" "io" "os" "strconv" "strings" "unicode/utf8" ) const info = ` ncol [options...] [filenames...] Nice COLumns realigns and styles data tables using ANSI color sequences. In particular, all auto-detected numbers are styled so they're easier to read at a glance. Input tables can be either lines of space-separated values or tab-separated values, and are auto-detected using the first non-empty line. When not given filepaths to read data from, this tool reads from standard input by default. The options are, available both in single and double-dash versions -h show this help message -help show this help message -no-sums avoid showing a final row with column sums -unsummed avoid showing a final row with column sums -no-tiles avoid showing color-coded tiles at the start of lines -untiled avoid showing color-coded tiles at the start of lines ` const columnGap = 2 // altDigitStyle is used to make 4+ digit-runs easier to read const altDigitStyle = "\x1b[38;2;168;168;168m" func main() { sums := true tiles := true args := os.Args[1:] out: for len(args) > 0 { switch args[0] { case `-h`, `--h`, `-help`, `--help`: os.Stdout.WriteString(info[1:]) return case `-no-sums`, `--no-sums`, `-no-totals`, `--no-totals`, `-unsummed`, `--unsummed`, `-untotaled`, `--untotaled`, `-untotalled`, `--untotalled`: sums = false args = args[1:] continue case `-no-tiles`, `--no-tiles`, `-untiled`, `--untiled`: tiles = false args = args[1:] continue default: break out } } if len(args) > 0 && args[0] == `--` { args = args[1:] } var res table res.ShowTiles = tiles res.ShowSums = sums if err := run(args, &res); err != nil { os.Stderr.WriteString(err.Error()) os.Stderr.WriteString("\n") os.Exit(1) } } // table has all summary info gathered from the data, along with the row // themselves, stored as lines/strings type table struct { Columns int Rows []string MaxWidth []int MaxDotDecimals []int Numeric []int Sums []float64 LoopItems func(line string, items int, t *table, f itemFunc) int sb strings.Builder ShowTiles bool ShowSums bool } type itemFunc func(i int, s string, t *table) func run(paths []string, res *table) error { for _, p := range paths { if err := handleFile(res, p); err != nil { return err } } if len(paths) == 0 { if err := handleReader(res, os.Stdin); err != nil { return err } } bw := bufio.NewWriterSize(os.Stdout, 32*1024) defer bw.Flush() realign(bw, res) return nil } func handleFile(res *table, path string) error { f, err := os.Open(path) if err != nil { // on windows, file-not-found error messages may mention `CreateFile`, // even when trying to open files in read-only mode return errors.New(`can't open file named ` + path) } defer f.Close() return handleReader(res, f) } func handleReader(t *table, r io.Reader) error { const gb = 1024 * 1024 * 1024 sc := bufio.NewScanner(r) sc.Buffer(nil, 8*gb) for i := 0; sc.Scan(); i++ { s := sc.Text() if i == 0 && len(s) > 2 && s[0] == 0xef && s[1] == 0xbb && s[2] == 0xbf { s = s[3:] } if len(s) == 0 { continue } t.Rows = append(t.Rows, s) if t.Columns == 0 { if t.LoopItems == nil { if strings.IndexByte(s, '\t') >= 0 { t.LoopItems = loopItemsTSV } else { t.LoopItems = loopItemsSSV } } const maxInt = int(^uint(0) >> 1) t.Columns = t.LoopItems(s, maxInt, t, doNothing) } t.LoopItems(s, t.Columns, t, updateItem) } return sc.Err() } // doNothing is given to LoopItems to count items, while doing nothing else func doNothing(i int, s string, t *table) { } func updateItem(i int, s string, t *table) { // ensure column-info-slices have enough room if i >= len(t.MaxWidth) { t.MaxWidth = append(t.MaxWidth, 0) t.MaxDotDecimals = append(t.MaxDotDecimals, 0) t.Numeric = append(t.Numeric, 0) t.Sums = append(t.Sums, 0) } // keep track of widest rune-counts for each column w := countWidth(s) if t.MaxWidth[i] < w { t.MaxWidth[i] = w } // update stats for numeric items if isNumeric(s, &(t.sb)) { dd := countDotDecimals(s) if t.MaxDotDecimals[i] < dd { t.MaxDotDecimals[i] = dd } t.Numeric[i]++ f, _ := strconv.ParseFloat(t.sb.String(), 64) t.Sums[i] += f } } // loopItemsSSV loops over a line's items, allocation-free style; when given // empty strings, the callback func is never called func loopItemsSSV(s string, max int, t *table, f itemFunc) int { i := 0 s = trimTrailingSpaces(s) for { s = trimLeadingSpaces(s) if len(s) == 0 { return i } if i+1 == max { f(i, s, t) return i + 1 } j := strings.IndexByte(s, ' ') if j < 0 { f(i, s, t) return i + 1 } f(i, s[:j], t) s = s[j+1:] i++ } } func trimLeadingSpaces(s string) string { for len(s) > 0 && s[0] == ' ' { s = s[1:] } return s } func trimTrailingSpaces(s string) string { for len(s) > 0 && s[len(s)-1] == ' ' { s = s[:len(s)-1] } return s } // loopItemsTSV loops over a line's tab-separated items, allocation-free style; // when given empty strings, the callback func is never called func loopItemsTSV(s string, max int, t *table, f itemFunc) int { if len(s) == 0 { return 0 } i := 0 for { if i+1 == max { f(i, s, t) return i + 1 } j := strings.IndexByte(s, '\t') if j < 0 { f(i, s, t) return i + 1 } f(i, s[:j], t) s = s[j+1:] i++ } } func skipLeadingEscapeSequences(s string) string { for len(s) >= 2 { if s[0] != '\x1b' { return s } switch s[1] { case '[': s = skipSingleLeadingANSI(s[2:]) case ']': if len(s) < 3 || s[2] != '8' { return s } s = skipSingleLeadingOSC(s[3:]) default: return s } } return s } func skipSingleLeadingANSI(s string) string { for len(s) > 0 { upper := s[0] &^ 32 s = s[1:] if 'A' <= upper && upper <= 'Z' { break } } return s } func skipSingleLeadingOSC(s string) string { var prev byte for len(s) > 0 { b := s[0] s = s[1:] if prev == '\x1b' && b == '\\' { break } prev = b } return s } // isNumeric checks if a string is valid/useable as a number func isNumeric(s string, sb *strings.Builder) bool { if len(s) == 0 { return false } sb.Reset() s = skipLeadingEscapeSequences(s) if len(s) > 0 && (s[0] == '+' || s[0] == '-') { sb.WriteByte(s[0]) s = s[1:] } s = skipLeadingEscapeSequences(s) if len(s) == 0 { return false } if b := s[0]; b == '.' { sb.WriteByte(b) return isDigits(s[1:], sb) } digits := 0 for { s = skipLeadingEscapeSequences(s) if len(s) == 0 { break } b := s[0] sb.WriteByte(b) if b == '.' { return isDigits(s[1:], sb) } if !('0' <= b && b <= '9') { return false } digits++ s = s[1:] } s = skipLeadingEscapeSequences(s) return len(s) == 0 && digits > 0 } func isDigits(s string, sb *strings.Builder) bool { if len(s) == 0 { return false } digits := 0 for { s = skipLeadingEscapeSequences(s) if len(s) == 0 { break } if b := s[0]; '0' <= b && b <= '9' { sb.WriteByte(b) s = s[1:] digits++ } else { return false } } s = skipLeadingEscapeSequences(s) return len(s) == 0 && digits > 0 } // countDecimals counts decimal digits from the string given, assuming it // represents a valid/useable float64, when parsed func countDecimals(s string) int { dot := strings.IndexByte(s, '.') if dot < 0 { return 0 } decs := 0 s = s[dot+1:] for len(s) > 0 { s = skipLeadingEscapeSequences(s) if len(s) == 0 { break } if '0' <= s[0] && s[0] <= '9' { decs++ } s = s[1:] } return decs } // countDotDecimals is like func countDecimals, but this one also includes // the dot, when any decimals are present, else the count stays at 0 func countDotDecimals(s string) int { decs := countDecimals(s) if decs > 0 { return decs + 1 } return decs } func countWidth(s string) int { width := 0 for len(s) > 0 { i := indexStartANSI(s) if i < 0 { width += utf8.RuneCountInString(s) return width } width += utf8.RuneCountInString(s[:i]) for len(s) > 0 { upper := s[0] &^ 32 s = s[1:] if 'A' <= upper && upper <= 'Z' { break } } } return width } func indexStartANSI(s string) int { var prev byte for i := range s { b := s[i] if prev == '\x1b' && b == '[' { return i - 1 } prev = b } return -1 } func realign(w *bufio.Writer, t *table) { // make sums row first, as final alignments are usually affected by these var sums []string if t.ShowSums { sums = make([]string, 0, t.Columns) } for i := 0; i < t.Columns && t.ShowSums; i++ { s := `-` width := 1 if t.Numeric[i] > 0 { decs := t.MaxDotDecimals[i] if decs > 0 { decs-- } var buf [64]byte s = string(strconv.AppendFloat(buf[:0], t.Sums[i], 'f', decs, 64)) width = len(s) } if t.MaxWidth[i] < width { t.MaxWidth[i] = width } sums = append(sums, s) } due := 0 showItem := func(i int, s string, t *table) { if i > 0 { due += columnGap } if isNumeric(s, &(t.sb)) { dd := countDotDecimals(s) rpad := t.MaxDotDecimals[i] - dd width := countWidth(s) lpad := t.MaxWidth[i] - (width + rpad) + due writeSpaces(w, lpad) f, _ := strconv.ParseFloat(t.sb.String(), 64) writeNumericItem(w, s, numericStyle(f)) due = rpad return } writeSpaces(w, due) w.WriteString(s) due = t.MaxWidth[i] - countWidth(s) } writeTile := func(i int, s string, t *table) { if len(s) == 0 { w.WriteString("\x1b[0m○") return } if isNumeric(s, &(t.sb)) { f, _ := strconv.ParseFloat(t.sb.String(), 64) w.WriteString(numericStyle(f)) w.WriteString("■") return } if s[0] == ' ' || s[len(s)-1] == ' ' { w.WriteString("\x1b[38;2;196;160;0m■") return } w.WriteString("\x1b[38;2;128;128;128m■") } // show realigned rows for _, line := range t.Rows { due = 0 if t.ShowTiles { end := t.LoopItems(line, t.Columns, t, writeTile) if end < len(t.MaxWidth)-1 { w.WriteString("\x1b[0m") } for i := end; i < len(t.MaxWidth); i++ { w.WriteString("×") } w.WriteString("\x1b[0m") due += columnGap } t.LoopItems(line, t.Columns, t, showItem) if w.WriteByte('\n') != nil { return } } if t.Columns > 0 && t.ShowSums { realignSums(w, t, sums) } } func realignSums(w *bufio.Writer, t *table, sums []string) { due := 0 if t.ShowTiles { due += t.Columns + columnGap } for i, s := range sums { if i > 0 { due += columnGap } if t.Numeric[i] == 0 { writeSpaces(w, due) w.WriteString(s) due = t.MaxWidth[i] - countWidth(s) continue } lpad := t.MaxWidth[i] - len(s) + due writeSpaces(w, lpad) writeNumericItem(w, s, numericStyle(t.Sums[i])) due = 0 } w.WriteByte('\n') } // writeSpaces does what it says, minimizing calls to write-like funcs func writeSpaces(w *bufio.Writer, n int) { const spaces = ` ` if n < 1 { return } for n >= len(spaces) { w.WriteString(spaces) n -= len(spaces) } w.WriteString(spaces[:n]) } func writeRowTiles(w *bufio.Writer, s string, t *table, writeTile itemFunc) { end := t.LoopItems(s, t.Columns, t, writeTile) if end < len(t.MaxWidth)-1 { w.WriteString("\x1b[0m") } for i := end + 1; i < len(t.MaxWidth); i++ { w.WriteString("×") } w.WriteString("\x1b[0m") } func numericStyle(f float64) string { if f > 0 { if float64(int64(f)) == f { return "\x1b[38;2;0;135;0m" } return "\x1b[38;2;0;155;95m" } if f < 0 { if float64(int64(f)) == f { return "\x1b[38;2;204;0;0m" } return "\x1b[38;2;215;95;95m" } if f == 0 { return "\x1b[38;2;0;95;215m" } return "\x1b[38;2;128;128;128m" } func writeNumericItem(w *bufio.Writer, s string, startStyle string) { w.WriteString(startStyle) if len(s) > 0 && (s[0] == '-' || s[0] == '+') { w.WriteByte(s[0]) s = s[1:] } dot := strings.IndexByte(s, '.') if dot < 0 { restyleDigits(w, s, altDigitStyle) w.WriteString("\x1b[0m") return } if len(s[:dot]) > 3 { restyleDigits(w, s[:dot], altDigitStyle) w.WriteString("\x1b[0m") w.WriteString(startStyle) w.WriteByte('.') } else { w.WriteString(s[:dot]) w.WriteByte('.') } rest := s[dot+1:] restyleDigits(w, rest, altDigitStyle) if len(rest) < 4 { w.WriteString("\x1b[0m") } } // restyleDigits renders a run of digits as alternating styled/unstyled runs // of 3 digits, which greatly improves readability, and is the only purpose // of this app; string is assumed to be all decimal digits func restyleDigits(w *bufio.Writer, digits string, altStyle string) { if len(digits) < 4 { // digit sequence is short, so emit it as is w.WriteString(digits) return } // separate leading 0..2 digits which don't align with the 3-digit groups i := len(digits) % 3 // emit leading digits unstyled, if there are any w.WriteString(digits[:i]) // the rest is guaranteed to have a length which is a multiple of 3 digits = digits[i:] // start by styling, unless there were no leading digits style := i != 0 for len(digits) > 0 { if style { w.WriteString(altStyle) w.WriteString(digits[:3]) w.WriteString("\x1b[0m") } else { w.WriteString(digits[:3]) } // advance to the next triple: the start of this func is supposed // to guarantee this step always works digits = digits[3:] // alternate between styled and unstyled 3-digit groups style = !style } }