tabstops.ts
ink/tabstops.ts
47
Lines
1113
Bytes
1
Exports
2
Imports
10
Keywords
What this is
This page documents one file from the repository and includes its full source so you can read it without leaving the docs site.
Beginner explanation
This file is one piece of the larger system. Its name, directory, imports, and exports show where it fits. Start by reading the exports and related files first.
How it is used
Start from the exports list and related files. Those are the easiest clues for where this file fits into the system.
Expert explanation
Architecturally, this file intersects with ui-flow. It contains 47 lines, 2 detected imports, and 1 detected exports.
Important relationships
Detected exports
expandTabs
Keywords
columnresultparttexttokenstringwidthintervaltokenizertokenselse
Detected imports
./stringWidth.js./termio/tokenize.js
Source notes
This page embeds the full file contents. Small or leaf files are still indexed honestly instead of being over-explained.
Full source
// Tab expansion, inspired by Ghostty's Tabstops.zig
// Uses 8-column intervals (POSIX default, hardcoded in terminals like Ghostty)
import { stringWidth } from './stringWidth.js'
import { createTokenizer } from './termio/tokenize.js'
const DEFAULT_TAB_INTERVAL = 8
export function expandTabs(
text: string,
interval = DEFAULT_TAB_INTERVAL,
): string {
if (!text.includes('\t')) {
return text
}
const tokenizer = createTokenizer()
const tokens = tokenizer.feed(text)
tokens.push(...tokenizer.flush())
let result = ''
let column = 0
for (const token of tokens) {
if (token.type === 'sequence') {
result += token.value
} else {
const parts = token.value.split(/(\t|\n)/)
for (const part of parts) {
if (part === '\t') {
const spaces = interval - (column % interval)
result += ' '.repeat(spaces)
column += spaces
} else if (part === '\n') {
result += part
column = 0
} else {
result += part
column += stringWidth(part)
}
}
}
}
return result
}