package tcelllivepage
import (
"context"
"html"
"net/http"
"sync"
"time"
"github.com/gdamore/tcell/v2"
am "github.com/pancsta/asyncmachine-go/pkg/machine"
lp "github.com/pancsta/livepage"
)
const (
DefaultCols = 80
DefaultRows = 25
DefaultFontSize = 14
maxDim = 4096
screenID = "tcell-screen"
)
type Theme struct {
Fg tcell .Color
Bg tcell .Color
Cursor tcell .Color
}
func DefaultTheme () Theme {
return Theme {
Fg : tcell .NewHexColor (0xD4D4D4 ),
Bg : tcell .NewHexColor (0x101014 ),
Cursor : tcell .NewHexColor (0x56B6C2 ),
}
}
type Opts struct {
Cols, Rows int
FontSize int
FontFamily string
Theme Theme
Title string
RefreshInterval time .Duration
Live lp .Opts
}
type Screen struct {
tcell .SimulationScreen
ctx context .Context
cancel context .CancelFunc
page *lp .Page [*compo ]
compo *compo
mach *am .Machine
theme Theme
fontSize int
fontFamily string
refreshInterval time .Duration
primed bool
lastRefresh time .Time
refreshTimer *time .Timer
mu sync .Mutex
dirty bool
html string
styles map [tcell .Style ]string
gen uint64
sizesMx sync .Mutex
sizes map [string ][2 ]int
finiOnce sync .Once
}
func NewScreen (ctx context .Context , opts Opts ) (*Screen , error ) {
if opts .Cols <= 0 {
opts .Cols = DefaultCols
}
if opts .Rows <= 0 {
opts .Rows = DefaultRows
}
if opts .FontSize <= 0 {
opts .FontSize = DefaultFontSize
}
if opts .FontFamily == "" {
opts .FontFamily = `ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace`
}
def := DefaultTheme ()
if !opts .Theme .Fg .Valid () {
opts .Theme .Fg = def .Fg
}
if !opts .Theme .Bg .Valid () {
opts .Theme .Bg = def .Bg
}
if !opts .Theme .Cursor .Valid () {
opts .Theme .Cursor = def .Cursor
}
if opts .Live .Debounce <= 0 {
opts .Live .Debounce = 20 * time .Millisecond
}
sim := tcell .NewSimulationScreen ("UTF-8" )
if err := sim .Init (); err != nil {
return nil , err
}
sim .SetSize (opts .Cols , opts .Rows )
sim .Clear ()
sim .Show ()
sim .EnableMouse ()
ctx , cancel := context .WithCancel (ctx )
s := &Screen {
SimulationScreen : sim ,
ctx : ctx ,
cancel : cancel ,
theme : opts .Theme ,
fontSize : opts .FontSize ,
fontFamily : opts .FontFamily ,
refreshInterval : opts .RefreshInterval ,
dirty : true ,
styles : map [tcell .Style ]string {},
sizes : map [string ][2 ]int {},
}
fullPage := opts .Live .HtmlPage == nil
basePage := opts .Live .HtmlPage
if basePage == nil {
title := opts .Title
if title == "" {
title = "livepage"
}
basePage = func (body string ) string {
return "<!DOCTYPE html><html><head><meta charset=\"utf-8\">" +
"<title>" + html .EscapeString (title ) + "</title></head><body>" +
body + "</body></html>"
}
}
opts .Live .HtmlPage = func (body string ) string {
return injectHead (basePage (body ), s .headTags (fullPage ))
}
userOnEvent := opts .Live .OnEvent
opts .Live .OnEvent = func (ev *lp .DomEvent ) {
s .handleEvent (ev )
if userOnEvent != nil {
userOnEvent (ev )
}
}
userOnClient := opts .Live .OnClient
opts .Live .OnClient = func (id string , connected bool ) {
s .handleClient (id , connected )
if userOnClient != nil {
userOnClient (id , connected )
}
}
s .compo = &compo {s : s }
mach := am .New (ctx , am .Schema {am .StateReady : {}}, nil )
s .mach = mach
page , err := lp .New (ctx , s .compo , opts .Live )
if err != nil {
cancel ()
sim .Fini ()
mach .Dispose ()
return nil , err
}
s .page = page
return s , nil
}
func (s *Screen ) Init () error {
return nil
}
func (s *Screen ) Fini () {
s .finiOnce .Do (func () {
s .mu .Lock ()
if s .refreshTimer != nil {
s .refreshTimer .Stop ()
s .refreshTimer = nil
}
s .mu .Unlock ()
s .SimulationScreen .Fini ()
s .page .Dispose ()
s .cancel ()
s .mach .Dispose ()
})
}
func (s *Screen ) Show () {
s .SimulationScreen .Show ()
s .mu .Lock ()
s .dirty = true
s .gen ++
first := !s .primed
s .primed = true
s .mu .Unlock ()
s .renderNow ()
if first {
s .refreshNow ()
return
}
if s .page .Clients () == 0 {
return
}
s .scheduleRefresh ()
}
func (s *Screen ) Sync () {
s .SimulationScreen .Show ()
s .mu .Lock ()
s .dirty = true
s .gen ++
s .mu .Unlock ()
s .renderNow ()
s .refreshNow ()
}
func (s *Screen ) renderNow () {
s .mu .Lock ()
defer s .mu .Unlock ()
s .html = s .renderGrid ()
s .dirty = false
}
func (s *Screen ) scheduleRefresh () {
s .mu .Lock ()
if s .refreshTimer != nil {
s .mu .Unlock ()
return
}
wait := time .Duration (0 )
if s .refreshInterval > 0 {
wait = s .refreshInterval - time .Since (s .lastRefresh )
}
if wait <= 0 {
s .mu .Unlock ()
s .refreshNow ()
return
}
s .refreshTimer = time .AfterFunc (wait , func () {
s .mu .Lock ()
s .refreshTimer = nil
s .mu .Unlock ()
s .refreshNow ()
})
s .mu .Unlock ()
}
func (s *Screen ) refreshNow () {
if s .ctx .Err () != nil {
return
}
s .mu .Lock ()
s .lastRefresh = time .Now ()
s .mu .Unlock ()
s .page .Refresh (s .ctx )
}
func (s *Screen ) ResizeGrid (cols , rows int ) bool {
if cols <= 0 || rows <= 0 || cols > maxDim || rows > maxDim {
return false
}
if w , h := s .Size (); w == cols && h == rows {
return false
}
s .SimulationScreen .SetSize (cols , rows )
_ = s .PostEvent (tcell .NewEventResize (cols , rows ))
s .mu .Lock ()
s .dirty = true
s .gen ++
s .mu .Unlock ()
return true
}
func (s *Screen ) Handler () http .Handler {
return s .page .NewHandler ()
}
func (s *Screen ) Machine () *am .Machine {
return s .mach
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .