add line numbers

This commit is contained in:
Keiran 2025-08-06 10:11:05 +01:00
parent bd179ccc99
commit 1bb6c16786

View File

@ -150,21 +150,25 @@ func (m Model) View() string {
b.WriteString("Text Editor (Ctrl+C or Esc to quit)\n\n")
contentHeight := m.height - 4
lineNumWidth := len(fmt.Sprintf("%d", len(m.lines))) + 1
for i, line := range m.lines {
if i >= contentHeight {
break
}
lineNum := fmt.Sprintf("%*d ", lineNumWidth, i+1)
if i == m.row {
if m.col == 0 {
b.WriteString("█" + line + "\n")
b.WriteString(lineNum + "█" + line + "\n")
} else if m.col >= len(line) {
b.WriteString(line + "█\n")
b.WriteString(lineNum + line + "█\n")
} else {
b.WriteString(line[:m.col] + "█" + line[m.col:] + "\n")
b.WriteString(lineNum + line[:m.col] + "█" + line[m.col:] + "\n")
}
} else {
b.WriteString(line + "\n")
b.WriteString(lineNum + line + "\n")
}
}
@ -178,6 +182,7 @@ func (m Model) View() string {
return b.String()
}
func main() {
p := tea.NewProgram(NewModel(), tea.WithAltScreen())
if _, err := p.Run(); err != nil {