/
draw.go
153 lines (134 loc) · 2.63 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright (c) 2011 David Forsythe.
* See LICENSE file for license details.
*/
package main
import (
"curses"
"math"
"os"
)
func initscreen(s *Dvi) {
curses.Initscr()
curses.Cbreak()
curses.Noecho()
curses.Nonl()
curses.Start_color()
curses.Stdwin.Keypad(true)
s.w = curses.Stdwin
curses.Init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)
curses.Init_pair(2, curses.COLOR_RED, curses.COLOR_WHITE)
curses.Init_pair(3, curses.COLOR_BLACK, curses.COLOR_YELLOW)
}
func endscreen() {
curses.Endwin()
}
func charlen(c byte) int {
if c == '\t' {
return 8
}
return 1
}
func screenlines(l *Line) int {
if s := int(math.Ceil(float64(l.length()) / float64(*curses.Cols))); s > 0 {
return s
}
return 1
}
func draw(d *Dvi) os.Error {
f := d.b
if f == nil {
return nil
}
// this is some slow ass scroll action, ill figure out a way to avoid this later
for l := f.first; l != f.disp && l != nil; l = l.next {
if l == f.pos.line {
for l != f.disp {
f.disp = f.disp.prev
}
break
}
}
for i, l := 0, f.disp; l != nil; i, l = i+screenlines(l), l.next {
if l == f.pos.line {
for i+screenlines(l) > *curses.Rows-1 {
f.disp = f.disp.next
i -= screenlines(l)
}
break
}
}
cursory := 0
cursorx := 0
y := 0
for l := f.disp; y < *curses.Rows-1 && l != nil; y, l = y+1, l.next {
x := 0
d.w.Move(y, 0)
d.w.Clrtoeol()
if l == f.pos.line {
cursory = y
for i, c := range l.text {
if i >= f.pos.off {
break
}
cursorx += charlen(c)
if cursorx > *curses.Cols-1 {
cursory++
cursorx = 0
}
}
}
x = 0
colors := int32(0)
if f.pos.line == l {
colors = curses.Color_pair(1)
}
for _, c := range l.text {
if x > *curses.Cols-1 {
y++
x = 0
d.w.Move(y, 0)
d.w.Clrtoeol()
}
for i := x; i < x+charlen(c); i++ {
d.w.Mvaddch(y, i, int32(c), colors)
}
x += charlen(c)
}
if colors != 0 {
for ; x < *curses.Cols; x++ {
d.w.Mvaddch(y, x, int32(' '), colors)
}
}
}
for ; y < *curses.Rows-1; y++ {
d.w.Move(y, 0)
d.w.Clrtoeol()
d.w.Mvaddch(y, 0, int32('~'), 0)
}
d.currx = cursorx
d.curry = cursory
msg := ""
mcolor := 3
beep := false
select {
case fromq := <-d.statusq:
msg = fromq.Display()
mcolor = fromq.Color()
beep = fromq.Beep()
default:
msg, mcolor, beep = d.statusDisplay()
}
d.w.Move(*curses.Rows-1, 0)
d.w.Clrtoeol()
for i := 0; i < *curses.Cols && i < len(msg); i++ {
d.w.Mvaddch(*curses.Rows-1, i, int32(msg[i]), curses.Color_pair(mcolor))
}
// s.w.Mvwaddnstr(*curses.Rows-1, 0, msg, *curses.Cols)
d.w.Move(cursory, cursorx)
if beep {
curses.Beep()
}
d.w.Refresh()
return nil
}