diff options
author | Drew DeVault <sir@cmpwn.com> | 2018-02-26 22:54:39 -0500 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2018-02-26 22:54:39 -0500 |
commit | 1418e1b9dc41d8f69bccb8de0fe0f1fb6835ce11 (patch) | |
tree | 4ae8b3373fdadb6dd3e7b8c8789cf938522b8f8a /lib/ui/context.go | |
parent | 661e3ec2a4dd97d4a8a8eab4f281b088770a6af2 (diff) | |
download | aerc-1418e1b9dc41d8f69bccb8de0fe0f1fb6835ce11.tar.gz |
Split UI library and widgets
Diffstat (limited to 'lib/ui/context.go')
-rw-r--r-- | lib/ui/context.go | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/lib/ui/context.go b/lib/ui/context.go new file mode 100644 index 00000000..ca3f4522 --- /dev/null +++ b/lib/ui/context.go @@ -0,0 +1,109 @@ +package ui + +import ( + "fmt" + + "github.com/mattn/go-runewidth" + tb "github.com/nsf/termbox-go" +) + +// A context allows you to draw in a sub-region of the terminal +type Context struct { + x int + y int + width int + height int +} + +func (ctx *Context) X() int { + return ctx.x +} + +func (ctx *Context) Y() int { + return ctx.y +} + +func (ctx *Context) Width() int { + return ctx.width +} + +func (ctx *Context) Height() int { + return ctx.height +} + +func NewContext(width, height int) *Context { + return &Context{0, 0, width, height} +} + +func (ctx *Context) Subcontext(x, y, width, height int) *Context { + if x+width > ctx.width || y+height > ctx.height { + panic(fmt.Errorf("Attempted to create context larger than parent")) + } + return &Context{ + x: ctx.x + x, + y: ctx.y + y, + width: width, + height: height, + } +} + +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")) + } + tb.SetCell(ctx.x+x, ctx.y+y, ch, fg, bg) +} + +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")) + } + + str := fmt.Sprintf(format, a...) + + x += ctx.x + y += ctx.y + old_x := x + + newline := func() bool { + x = old_x + y++ + return y < ctx.height + } + for _, ch := range str { + if str == " こんにちは " { + fmt.Printf("%c\n", ch) + } + switch ch { + case '\n': + if !newline() { + return runewidth.StringWidth(str) + } + case '\r': + x = old_x + default: + tb.SetCell(x, y, ch, ref.Fg, ref.Bg) + x += runewidth.RuneWidth(ch) + if x == old_x+ctx.width { + if !newline() { + return runewidth.StringWidth(str) + } + } + } + } + + return runewidth.StringWidth(str) +} + +func (ctx *Context) Fill(x, y, width, height int, ref tb.Cell) { + _x := 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 + } +} |