2018-02-16 06:05:07 +01:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-11-30 23:07:03 +01:00
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
"github.com/gdamore/tcell/v2/views"
|
2018-06-12 02:04:21 +02:00
|
|
|
"github.com/mattn/go-runewidth"
|
2018-02-16 06:05:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// A context allows you to draw in a sub-region of the terminal
|
|
|
|
type Context struct {
|
2019-12-20 19:21:32 +01:00
|
|
|
screen tcell.Screen
|
|
|
|
viewport *views.ViewPort
|
|
|
|
x, y int
|
|
|
|
onPopover func(*Popover)
|
2018-02-16 06:05:07 +01:00
|
|
|
}
|
|
|
|
|
2018-02-27 04:41:54 +01:00
|
|
|
func (ctx *Context) X() int {
|
2018-06-01 09:58:00 +02:00
|
|
|
x, _, _, _ := ctx.viewport.GetPhysical()
|
|
|
|
return x
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) Y() int {
|
2018-06-01 09:58:00 +02:00
|
|
|
_, y, _, _ := ctx.viewport.GetPhysical()
|
|
|
|
return y
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
|
2018-02-16 06:05:07 +01:00
|
|
|
func (ctx *Context) Width() int {
|
2018-06-01 09:58:00 +02:00
|
|
|
width, _ := ctx.viewport.Size()
|
|
|
|
return width
|
2018-02-16 06:05:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) Height() int {
|
2018-06-01 09:58:00 +02:00
|
|
|
_, height := ctx.viewport.Size()
|
|
|
|
return height
|
2018-02-16 06:05:07 +01:00
|
|
|
}
|
|
|
|
|
2019-12-20 19:21:32 +01:00
|
|
|
func NewContext(width, height int, screen tcell.Screen, p func(*Popover)) *Context {
|
2018-06-01 09:58:00 +02:00
|
|
|
vp := views.NewViewPort(screen, 0, 0, width, height)
|
2019-12-20 19:21:32 +01:00
|
|
|
return &Context{screen, vp, 0, 0, p}
|
2018-02-16 06:05:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) Subcontext(x, y, width, height int) *Context {
|
2018-06-01 09:58:00 +02:00
|
|
|
vp_width, vp_height := ctx.viewport.Size()
|
2018-06-12 02:04:21 +02:00
|
|
|
if x < 0 || y < 0 {
|
2018-06-01 09:58:00 +02:00
|
|
|
panic(fmt.Errorf("Attempted to create context with negative offset"))
|
2018-02-16 06:05:07 +01:00
|
|
|
}
|
2018-06-12 02:04:21 +02:00
|
|
|
if x+width > vp_width || y+height > vp_height {
|
2018-06-01 09:58:00 +02:00
|
|
|
panic(fmt.Errorf("Attempted to create context larger than parent"))
|
2018-02-16 06:05:07 +01:00
|
|
|
}
|
2018-06-01 09:58:00 +02:00
|
|
|
vp := views.NewViewPort(ctx.viewport, x, y, width, height)
|
2019-12-20 19:21:32 +01:00
|
|
|
return &Context{ctx.screen, vp, ctx.x + x, ctx.y + y, ctx.onPopover}
|
2018-02-16 06:05:07 +01:00
|
|
|
}
|
|
|
|
|
2018-06-01 09:58:00 +02:00
|
|
|
func (ctx *Context) SetCell(x, y int, ch rune, style tcell.Style) {
|
|
|
|
width, height := ctx.viewport.Size()
|
|
|
|
if x >= width || y >= height {
|
2022-09-26 17:11:49 +02:00
|
|
|
// no-op when dims are inadequate
|
|
|
|
return
|
2018-02-16 06:05:07 +01:00
|
|
|
}
|
2018-06-01 09:58:00 +02:00
|
|
|
crunes := []rune{}
|
|
|
|
ctx.viewport.SetContent(x, y, ch, crunes, style)
|
2018-02-16 06:05:07 +01:00
|
|
|
}
|
|
|
|
|
2018-06-01 09:58:00 +02:00
|
|
|
func (ctx *Context) Printf(x, y int, style tcell.Style,
|
2022-07-31 22:16:40 +02:00
|
|
|
format string, a ...interface{},
|
|
|
|
) int {
|
2018-06-01 09:58:00 +02:00
|
|
|
width, height := ctx.viewport.Size()
|
2018-02-16 06:05:07 +01:00
|
|
|
|
2018-06-01 09:58:00 +02:00
|
|
|
if x >= width || y >= height {
|
2022-09-26 17:11:49 +02:00
|
|
|
// no-op when dims are inadequate
|
|
|
|
return 0
|
2018-02-16 06:05:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
str := fmt.Sprintf(format, a...)
|
|
|
|
|
|
|
|
old_x := x
|
|
|
|
|
|
|
|
newline := func() bool {
|
|
|
|
x = old_x
|
|
|
|
y++
|
2018-06-01 09:58:00 +02:00
|
|
|
return y < height
|
2018-02-16 06:05:07 +01:00
|
|
|
}
|
|
|
|
for _, ch := range str {
|
|
|
|
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:
|
2018-06-01 09:58:00 +02:00
|
|
|
crunes := []rune{}
|
|
|
|
ctx.viewport.SetContent(x, y, ch, crunes, style)
|
2018-02-18 01:42:29 +01:00
|
|
|
x += runewidth.RuneWidth(ch)
|
2018-06-12 02:04:21 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-06-01 09:58:00 +02:00
|
|
|
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) {
|
2019-01-14 14:07:24 +01:00
|
|
|
ctx.screen.ShowCursor(ctx.x+x, ctx.y+y)
|
|
|
|
}
|
|
|
|
|
2022-09-14 21:09:40 +02:00
|
|
|
func (ctx *Context) SetCursorStyle(cs tcell.CursorStyle) {
|
|
|
|
ctx.screen.SetCursorStyle(cs)
|
|
|
|
}
|
|
|
|
|
2019-01-14 14:07:24 +01:00
|
|
|
func (ctx *Context) HideCursor() {
|
|
|
|
ctx.screen.HideCursor()
|
2018-02-16 06:05:07 +01:00
|
|
|
}
|
2019-12-20 19:21:32 +01:00
|
|
|
|
|
|
|
func (ctx *Context) Popover(x, y, width, height int, d Drawable) {
|
|
|
|
ctx.onPopover(&Popover{
|
|
|
|
x: ctx.x + x,
|
|
|
|
y: ctx.y + y,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
content: d,
|
|
|
|
})
|
|
|
|
}
|
2022-09-14 21:09:40 +02:00
|
|
|
|
|
|
|
func (ctx *Context) View() *views.ViewPort {
|
|
|
|
return ctx.viewport
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Context) Show() {
|
|
|
|
ctx.screen.Show()
|
|
|
|
}
|