From 011952386749770db068cd63bf0be95799198bbe Mon Sep 17 00:00:00 2001 From: Aki Kareha Date: Wed, 25 Mar 2026 21:38:10 +0900 Subject: [PATCH] Refine print --- internal/console/console.go | 8 ------- internal/editor/editor.go | 47 ++++++++++++++++++++----------------- internal/editor/screen.go | 12 +++------- internal/util/util.go | 35 +++++++++++++++++++++++++-- 4 files changed, 62 insertions(+), 40 deletions(-) diff --git a/internal/console/console.go b/internal/console/console.go index 511865e..22fee89 100644 --- a/internal/console/console.go +++ b/internal/console/console.go @@ -97,11 +97,3 @@ func ReadRune() rune { } return r } - -func Print(s string) { - fmt.Print(s) -} - -func Printf(format string, a ...any) (n int, err error) { - return fmt.Printf(format, a...) -} diff --git a/internal/editor/editor.go b/internal/editor/editor.go index 9f0243a..353744a 100644 --- a/internal/editor/editor.go +++ b/internal/editor/editor.go @@ -110,7 +110,7 @@ func (ed *Editor) drawBuffer() { } console.MoveCursor(0, y) - console.Print(line) + util.Print(line) y += ed.lineHeight(line) if y >= h-1 { @@ -120,7 +120,7 @@ func (ed *Editor) drawBuffer() { for ; y < h-1; y++ { console.MoveCursor(0, y) - console.Print("~") + util.Print("~") } } @@ -130,9 +130,9 @@ func (ed *Editor) drawStatus() { console.MoveCursor(0, h-1) switch ed.mode { case modeCommand: - console.Print("c") + util.Print("c") case modeInsert: - console.Print("i") + util.Print("i") } } @@ -159,22 +159,23 @@ func (ed *Editor) updateCursor() { if ed.row < ed.vrow { ed.vrow = ed.row - } else if ed.vrow+h-2 <= ed.row { - ed.vrow = ed.row - (h - 2) } y := 0 for i := ed.vrow; i < ed.row; i++ { - var line string - if ed.mode == modeInsert && i == ed.row { - line = ed.head + ed.insert.String() + ed.tail - } else { - line = ed.lines[i] - } - - y += ed.lineHeight(line) + y += ed.lineHeight(ed.lines[i]) } ed.y = y + dy + + for ed.y >= h-1 { + ed.vrow++ + + y := 0 + for i := ed.vrow; i < ed.row; i++ { + y += ed.lineHeight(ed.lines[i]) + } + ed.y = y + dy + } } func (ed *Editor) repaint() { @@ -202,16 +203,20 @@ func (ed *Editor) exitInsert() { } func (ed *Editor) insertNewline() { - lines := make([]string, 0, len(ed.lines)+1) - lines = append(lines, ed.lines[:ed.row+1]...) - lines = append(lines, "") + before := make([]string, 0, len(ed.lines)+1) + before = append(before, ed.lines[:ed.row]...) + var after []string if ed.row+1 < len(ed.lines) { - lines = append(lines, ed.lines[ed.row+1:]...) + after = ed.lines[ed.row+1:] + } else { + after = []string{} } + newLines := []string{ + ed.head + ed.insert.String(), + ed.tail, + } + ed.lines = append(append(before, newLines...), after...) - lines[ed.row] = ed.head + ed.insert.String() - lines[ed.row+1] = ed.tail - ed.lines = lines ed.row++ ed.col = 0 diff --git a/internal/editor/screen.go b/internal/editor/screen.go index 38372d4..c308e4c 100644 --- a/internal/editor/screen.go +++ b/internal/editor/screen.go @@ -4,18 +4,12 @@ import ( "tea.kareha.org/lab/levi/internal/console" ) -type screen struct { - w, h int -} +type screen struct{} func newScreen() screen { - w, h := console.Size() - return screen{ - w: w, - h: h, - } + return screen{} } func (scr *screen) size() (int, int) { - return scr.w, scr.h + return console.Size() } diff --git a/internal/util/util.go b/internal/util/util.go index 569dd7b..1493d22 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -1,6 +1,7 @@ package util import ( + "fmt" "unicode" ) @@ -20,7 +21,14 @@ func isEmoji(r rune) bool { return r >= 0x1f300 && r <= 0x1faff } -func runeWidth(r rune) int { +const tabWidth = 4 + +func runeWidth(r rune, x int) int { + // tab + if r == '\t' { + return tabWidth - (x % tabWidth) + } + // control code if r == 0 { return 0 @@ -54,8 +62,31 @@ func StringWidth(s string, col int) int { if i >= col { break } - sum += runeWidth(r) + w := runeWidth(r, sum) + sum += w i++ } return sum } + +func Print(s string) { + x := 0 + for _, r := range s { + if r == '\t' { + spaces := tabWidth - (x % tabWidth) + for i := 0; i < spaces; i++ { + fmt.Print(" ") + } + x += spaces + } else { + fmt.Printf("%c", r) + x += runeWidth(r, x) + } + } +} + +func Printf(format string, a ...any) (n int, err error) { + s := fmt.Sprintf(format, a...) + Print(s) + return len(s), nil +}