Refine print

This commit is contained in:
2026-03-25 21:38:10 +09:00
parent 3eb2a9795d
commit 0119523867
4 changed files with 62 additions and 40 deletions

View File

@@ -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...)
}

View File

@@ -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

View File

@@ -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()
}

View File

@@ -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
}