aerc/lib/ui/context.go

122 lines
2.6 KiB
Go
Raw Normal View History

2018-02-16 06:05:07 +01:00
package ui
import (
"fmt"
2018-02-18 01:42:29 +01:00
"github.com/mattn/go-runewidth"
"github.com/gdamore/tcell"
"github.com/gdamore/tcell/views"
2018-02-16 06:05:07 +01:00
)
// A context allows you to draw in a sub-region of the terminal
type Context struct {
viewport *views.ViewPort
2018-02-16 06:05:07 +01:00
}
func (ctx *Context) X() int {
x, _, _, _ := ctx.viewport.GetPhysical()
return x
}
func (ctx *Context) Y() int {
_, y, _, _ := ctx.viewport.GetPhysical()
return y
}
2018-02-16 06:05:07 +01:00
func (ctx *Context) Width() int {
width, _ := ctx.viewport.Size()
return width
2018-02-16 06:05:07 +01:00
}
func (ctx *Context) Height() int {
_, height := ctx.viewport.Size()
return height
2018-02-16 06:05:07 +01:00
}
func NewContext(width, height int, screen tcell.Screen) *Context {
vp := views.NewViewPort(screen, 0, 0, width, height)
return &Context{vp}
2018-02-16 06:05:07 +01:00
}
func (ctx *Context) Subcontext(x, y, width, height int) *Context {
vp_width, vp_height := ctx.viewport.Size()
if (x < 0 || y < 0) {
panic(fmt.Errorf("Attempted to create context with negative offset"))
2018-02-16 06:05:07 +01:00
}
if (x + width > vp_width || y + height > vp_height) {
panic(fmt.Errorf("Attempted to create context larger than parent"))
2018-02-16 06:05:07 +01:00
}
vp := views.NewViewPort(ctx.viewport, x, y, width, height)
return &Context{vp}
2018-02-16 06:05:07 +01:00
}
func (ctx *Context) SetCell(x, y int, ch rune, style tcell.Style) {
width, height := ctx.viewport.Size()
if x >= width || y >= height {
2018-02-16 06:05:07 +01:00
panic(fmt.Errorf("Attempted to draw outside of context"))
}
crunes := []rune{}
ctx.viewport.SetContent(x, y, ch, crunes, style)
2018-02-16 06:05:07 +01:00
}
func (ctx *Context) Printf(x, y int, style tcell.Style,
2018-02-18 01:42:29 +01:00
format string, a ...interface{}) int {
width, height := ctx.viewport.Size()
2018-02-16 06:05:07 +01:00
if x >= width || y >= height {
2018-02-16 06:05:07 +01:00
panic(fmt.Errorf("Attempted to draw outside of context"))
}
str := fmt.Sprintf(format, a...)
old_x := x
newline := func() bool {
x = old_x
y++
return y < height
2018-02-16 06:05:07 +01:00
}
for _, ch := range str {
2018-02-18 01:42:29 +01:00
if str == " こんにちは " {
fmt.Printf("%c\n", ch)
}
2018-02-16 06:05:07 +01:00
switch ch {
case '\n':
if !newline() {
2018-02-18 01:42:29 +01:00
return runewidth.StringWidth(str)
2018-02-16 06:05:07 +01:00
}
case '\r':
x = old_x
default:
crunes := []rune{}
ctx.viewport.SetContent(x, y, ch, crunes, style)
2018-02-18 01:42:29 +01:00
x += runewidth.RuneWidth(ch)
if x == old_x + width {
2018-02-16 06:05:07 +01:00
if !newline() {
2018-02-18 01:42:29 +01:00
return runewidth.StringWidth(str)
2018-02-16 06:05:07 +01:00
}
}
}
}
2018-02-18 01:42:29 +01:00
return runewidth.StringWidth(str)
2018-02-16 06:05:07 +01:00
}
//func (ctx *Context) Screen() tcell.Screen {
// return ctx.screen
//}
func (ctx *Context) Fill(x, y, width, height int, rune rune, style tcell.Style) {
vp := views.NewViewPort(ctx.viewport, x, y, width, height)
vp.Fill(rune, style)
}
func (ctx *Context) SetCursor(x, y int) {
// FIXME: Cursor needs to be set on tcell.Screen, or layout has to
// provide a CellModel
// cv := views.NewCellView()
// cv.Init()
// cv.SetView(ctx.viewport)
// cv.SetCursor(x, y)
2018-02-16 06:05:07 +01:00
}