summary refs log tree commit diff stats
path: root/ui/context.go
diff options
context:
space:
mode:
Diffstat (limited to 'ui/context.go')
-rw-r--r--ui/context.go31
1 files changed, 19 insertions, 12 deletions
diff --git a/ui/context.go b/ui/context.go
index e7d9ebe..ae9e561 100644
--- a/ui/context.go
+++ b/ui/context.go
@@ -3,7 +3,8 @@ package ui
 import (
 	"fmt"
 
-	"github.com/nsf/termbox-go"
+	"github.com/mattn/go-runewidth"
+	tb "github.com/nsf/termbox-go"
 )
 
 // A context allows you to draw in a sub-region of the terminal
@@ -38,15 +39,15 @@ func (ctx *Context) Subcontext(x, y, width, height int) *Context {
 	}
 }
 
-func (ctx *Context) SetCell(x, y int, ch rune, fg, bg termbox.Attribute) {
+func (ctx *Context) SetCell(x, y int, ch rune, fg, bg tb.Attribute) {
 	if x >= ctx.width || y >= ctx.height {
 		panic(fmt.Errorf("Attempted to draw outside of context"))
 	}
-	termbox.SetCell(ctx.x+x, ctx.y+y, ch, fg, bg)
+	tb.SetCell(ctx.x+x, ctx.y+y, ch, fg, bg)
 }
 
-func (ctx *Context) Printf(x, y int, ref termbox.Cell,
-	format string, a ...interface{}) {
+func (ctx *Context) Printf(x, y int, ref tb.Cell,
+	format string, a ...interface{}) int {
 
 	if x >= ctx.width || y >= ctx.height {
 		panic(fmt.Errorf("Attempted to draw outside of context"))
@@ -64,29 +65,35 @@ func (ctx *Context) Printf(x, y int, ref termbox.Cell,
 		return y < ctx.height
 	}
 	for _, ch := range str {
+		if str == " こんにちは " {
+			fmt.Printf("%c\n", ch)
+		}
 		switch ch {
 		case '\n':
 			if !newline() {
-				return
+				return runewidth.StringWidth(str)
 			}
 		case '\r':
 			x = old_x
 		default:
-			termbox.SetCell(x, y, ch, ref.Fg, ref.Bg)
-			x++
+			tb.SetCell(x, y, ch, ref.Fg, ref.Bg)
+			x += runewidth.RuneWidth(ch)
 			if x == old_x+ctx.width {
 				if !newline() {
-					return
+					return runewidth.StringWidth(str)
 				}
 			}
 		}
 	}
+
+	return runewidth.StringWidth(str)
 }
 
-func (ctx *Context) Fill(x, y, width, height int, ref termbox.Cell) {
+func (ctx *Context) Fill(x, y, width, height int, ref tb.Cell) {
 	_x := x
-	for ; y < height && y < ctx.height; y++ {
-		for ; x < width && x < ctx.width; x++ {
+	_y := y
+	for ; y < _y+height && y < ctx.height; y++ {
+		for ; x < _x+width && x < ctx.width; x++ {
 			ctx.SetCell(x, y, ref.Ch, ref.Fg, ref.Bg)
 		}
 		x = _x
<vc@akkartik.com> 2018-10-21 20:36:43 -0700 committer Kartik Agaram <vc@akkartik.com> 2018-10-21 20:36:43 -0700 4712' href='/akkartik/mu/commit/subx/opcodes?h=hlt&id=417a05ee7dbecbf846c54cd1e186e000415fb0a3'>417a05ee ^
f15fcfe8 ^
417a05ee ^

a78deb23 ^
417a05ee ^
0c7f08ce ^

f15fcfe8 ^

469a1a9a ^
0c7f08ce ^




417a05ee ^
924ed08a ^
417a05ee ^


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