/
draw.go
90 lines (64 loc) · 1.93 KB
/
draw.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"strings"
"github.com/inancgumus/screen"
)
func draw(lines []string) {
screen.MoveTopLeft()
title := fmt.Sprintf("Pico - %s%s ", FileName, DirtyIndicator())
footer := fmt.Sprintf("Ln %d, Col %d ", ActiveCursor.AbsoluteY()+1, ActiveCursor.AbsoluteX()+1)
// Draw the header
headerSuffix := fmt.Sprintf(" %s ─┐", Version)
fmt.Println(renderRow(-1, "┌ ", title, "─", headerSuffix))
// Draw the body
for y := 0; y < ActiveCursor.BodyHeight; y++ {
absoluteY := y + ScrollY
line := ""
if absoluteY < len(lines) {
line = lines[absoluteY]
}
fmt.Println(renderRow(y, "│ ", line, " ", " │"))
}
// Draw the footer
fmt.Print(renderRow(-1, "└ ", footer, "─", " ^O Save ─ ^C Exit ─ ^X Save & Exit ┘"))
screen.MoveTopLeft()
}
func renderRow(y int, prefix, text, padding, suffix string) string {
var sb strings.Builder
sb.WriteString(prefix)
// Text length (with support for unicode)
textLen := len([]rune(text))
// Append padding
prefixLen := len([]rune(prefix)) - 2
suffixLen := len([]rune(suffix)) - 2
paddingLen := ActiveCursor.BodyWidth - textLen - prefixLen - suffixLen
if paddingLen > 0 {
text += strings.Repeat(padding, paddingLen)
}
// -1 is footer and header (non-content) rows
if y != -1 {
// Clamp the text length and scroll horizontally
if textLen > 0 {
from := ScrollX
to := ScrollX + ActiveCursor.BodyWidth
for textLen+1 < to {
text += " "
textLen++
}
text = string([]rune(text)[from:to])
}
// Insert cursor
if !ActiveCursor.Disabled && ActiveCursor.Y == y {
char := ActiveCursor.CurrentChar()
// If the cursor is at the end of the line, we need to remove the first character to make room for the cursor
if ActiveCursor.X == ActiveCursor.BodyWidth {
text = text[1:]
}
text = ReplaceCharacterAt(text, InvertColors(char), ActiveCursor.X)
}
}
sb.WriteString(text)
sb.WriteString(suffix)
return sb.String()
}