about summary refs log tree commit diff stats
path: root/ui/context.go
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2018-02-16 00:05:07 -0500
committerDrew DeVault <sir@cmpwn.com>2018-02-16 00:09:25 -0500
commit8c8c21f3ffef1a7083405c45221576858d1af5d5 (patch)
treeedc159a57ecd6eb68fe9bdc0e724810bc76f96bb /ui/context.go
parent18493180bdbbdadfe415d68c3a04a13b946fff43 (diff)
downloadaerc-8c8c21f3ffef1a7083405c45221576858d1af5d5.tar.gz
Initial pass on new frontend design
Diffstat (limited to 'ui/context.go')
-rw-r--r--ui/context.go95
1 files changed, 95 insertions, 0 deletions
diff --git a/ui/context.go b/ui/context.go
new file mode 100644
index 0000000..9f2e2fe
--- /dev/null
+++ b/ui/context.go
@@ -0,0 +1,95 @@
+package ui
+
+import (
+	"fmt"
+
+	"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) Width() int {
+	return ctx.width
+}
+
+func (ctx *Context) Height() int {
+	return ctx.height
+}
+
+func NewContext() *Context {
+	width, height := termbox.Size()
+	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 termbox.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)
+}
+
+func (ctx *Context) Printf(x, y int, ref termbox.Cell,
+	format string, a ...interface{}) {
+
+	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 {
+		switch ch {
+		case '\n':
+			if !newline() {
+				return
+			}
+		case '\r':
+			x = old_x
+		default:
+			termbox.SetCell(x, y, ch, ref.Fg, ref.Bg)
+			x++
+			if x == old_x+ctx.width {
+				if !newline() {
+					return
+				}
+			}
+		}
+	}
+}
+
+func (ctx *Context) Fill(x, y, width, height int, ref termbox.Cell) {
+	_x := x
+	for ; y < height && y < ctx.height; y++ {
+		for ; x < width && x < ctx.width; x++ {
+			ctx.SetCell(x, y, ref.Ch, ref.Fg, ref.Bg)
+		}
+		x = _x
+	}
+}