libcodecols provides column-counting and histogram rendering for source code analysis. Designed after Rasmus Andersson's linelen_hist.sh. It computes per-line column widths, frequency distributions, summary statistics (including percentiles), and renders Unicode histogram charts.
codecols := import('codecols')
codecols := import('codecols')
// All-in-one analysis
result := codecols.analyze(readFile('main.c'))
codecols.renderHistogram(result.freqs, {})
println('mean:', result.stats.mean)
println('p90:', result.stats.p90)
// Or use report() for a formatted summary
codecols.report(readFile('main.c'), { histoWidth: 40 })
Takes a list of line strings and returns a list of column widths. Empty lines are excluded. Each width is rounded up to the nearest even number.
Parameters:
lines— list of strings (one per line)
Returns: list of integers
codecols.columnCounts(['hello', '', 'hi'])
// => [6, 2]
Takes a list of column counts and returns a frequency map { columnWidth: count }.
Parameters:
cols— list of integers fromcolumnCounts()
Returns: object mapping column width strings to occurrence counts
codecols.frequencies([4, 4, 6, 8])
// => { '4': 2, '6': 1, '8': 1 }
Computes summary statistics for a list of column counts.
Parameters:
cols— list of integers fromcolumnCounts()
Returns:
{
min: 4 // smallest column count
max: 120 // largest column count
mean: 42.5 // average (rounded to 2 decimal places)
median: 38 // median value
total: 8500 // sum of all column counts
lineCount: 200 // number of non-empty lines
p75: 60 // 75th percentile
p90: 80 // 90th percentile
p95: 100 // 95th percentile
p99: 118 // 99th percentile
}
Computes the p-th percentile of a pre-sorted list of numbers.
Parameters:
sortedVals— sorted list of numbersp— percentile in range[0, 100]
Returns: number
codecols.percentile([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 90)
// => 9
Renders a Unicode histogram bar of a given length using 1/8th block character precision.
Parameters:
n— bar length in 1/8th-character units (e.g. 24 = 3 full blocks)
Returns: string of Unicode block characters
codecols.histo(24) // => '███'
codecols.histo(5) // => '▋'
Prints a formatted histogram table to stdout.
Parameters:
freqs— frequency map fromfrequencies()opts— optional config object:maxCols— maximum column number to display (default: auto from data)histoWidth— maximum bar width in characters (default: 60)step— column grouping step size (default: 2)
codecols.renderHistogram(freqs, { histoWidth: 40, step: 4 })
Returns the histogram as a list of strings instead of printing to stdout. Takes the same parameters as renderHistogram().
Returns: list of strings (header + one per row)
lines := codecols.formatHistogram(freqs, {})
Performs a full column analysis on raw source text. Combines columnCounts(), frequencies(), and stats().
Parameters:
text— raw source code string
Returns:
{
cols: [...] // list of per-line column counts
freqs: { ... } // frequency map
stats: { ... } // summary statistics
}
All-in-one: analyzes source text, prints the histogram, and prints a summary line. Returns the analysis result.
Parameters:
text— raw source code stringopts— histogram options (same asrenderHistogram())
Returns: same as analyze()
Performs full column analysis on multiple source texts in parallel. Returns a list of { cols, freqs, stats } results.
codecolsResults := panalyze([source1, source2, source3])