Skip to content

Commit

Permalink
more testing with bubbletea
Browse files Browse the repository at this point in the history
  • Loading branch information
srlehn committed Nov 23, 2023
1 parent 19e1b0a commit f303a41
Show file tree
Hide file tree
Showing 69 changed files with 3,154 additions and 2,009 deletions.
3 changes: 3 additions & 0 deletions env/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package env

// TODO move internal/environ here
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ require (
github.com/anthonynsimon/bild v0.13.0
github.com/aymanbagabas/go-pty v0.2.0
github.com/bamiaux/rez v0.0.0-20170731184118-29f4463c688b
github.com/charmbracelet/bubbles v0.16.1
github.com/charmbracelet/bubbletea v0.24.2
github.com/charmbracelet/lipgloss v0.9.1
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81
github.com/creack/pty v1.1.20
github.com/creack/pty v1.1.21
github.com/creack/pty/v2 v2.0.1
github.com/disintegration/gift v1.2.1
github.com/disintegration/imaging v1.6.2
Expand All @@ -28,6 +29,8 @@ require (
github.com/mattn/go-sixel v0.0.5
github.com/mattn/go-tty v0.0.5
github.com/mitchellh/go-ps v1.0.0
github.com/muesli/reflow v0.3.0
github.com/muesli/termenv v0.15.2
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/pkg/term v1.2.0-beta.2
github.com/rivo/tview v0.0.0-20230621164836-6cc0565babaf
Expand Down Expand Up @@ -56,11 +59,12 @@ require (
github.com/dsoprea/go-logging v0.0.0-20200710184922-b02d349568dd // indirect
github.com/dsoprea/go-png-image-structure/v2 v2.0.0-20210512210324-29b889a6093d // indirect
github.com/dsoprea/go-utility/v2 v2.0.0-20221003172846-a3e1774ef349 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/esimov/pigo v1.4.6 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-text/typesetting v0.0.0-20231113130822-cf4b5dada737 // indirect
github.com/go-text/typesetting v0.0.0-20231120180320-af78120ccb13 // indirect
github.com/golang/geo v0.0.0-20230421003525-6adc56603217 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
Expand All @@ -71,8 +75,6 @@ require (
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/nsf/termbox-go v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
Expand Down
12 changes: 8 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/aymanbagabas/go-pty v0.2.0 h1:fC2ATGqFG+zH0Hr0Ks46h0lrRU2ExmQxR7iHUjB
github.com/aymanbagabas/go-pty v0.2.0/go.mod h1:Ul2Zd4Z3Cby9ByGdurtjlAafh6pjgrjiHNAsx4LC5yE=
github.com/bamiaux/rez v0.0.0-20170731184118-29f4463c688b h1:5Ci5wpOL75rYF6RQGRoqhEAU6xLJ6n/D4SckXX1yB74=
github.com/bamiaux/rez v0.0.0-20170731184118-29f4463c688b/go.mod h1:obBQGGIFbbv9KWg92Qu9UHeD94JXmHD1jovY/z6I3O8=
github.com/charmbracelet/bubbles v0.16.1 h1:6uzpAAaT9ZqKssntbvZMlksWHruQLNxg49H5WdeuYSY=
github.com/charmbracelet/bubbles v0.16.1/go.mod h1:2QCp9LFlEsBQMvIYERr7Ww2H2bA7xen1idUDIzm/+Xc=
github.com/charmbracelet/bubbletea v0.24.2 h1:uaQIKx9Ai6Gdh5zpTbGiWpytMU+CfsPp06RaW2cx/SY=
github.com/charmbracelet/bubbletea v0.24.2/go.mod h1:XdrNrV4J8GiyshTtx3DNuYkR1FDaJmO3l2nejekbsgg=
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
Expand All @@ -34,8 +36,8 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.20 h1:VIPb/a2s17qNeQgDnkfZC35RScx+blkKF8GV68n80J4=
github.com/creack/pty v1.1.20/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0=
github.com/creack/pty v1.1.21/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/creack/pty/v2 v2.0.1 h1:RDY1VY5b+7m2mfPsugucOYPIxMp+xal5ZheSyVzUA+k=
github.com/creack/pty/v2 v2.0.1/go.mod h1:2dSssKp3b86qYEMwA/FPwc3ff+kYpDdQI8osU8J7gxQ=
github.com/d4l3k/messagediff v1.2.2-0.20190829033028-7e0a312ae40b/go.mod h1:Oozbb1TVXFac9FtSIxHBMnBCq2qeH/2KkEQxENCrlLo=
Expand Down Expand Up @@ -65,6 +67,8 @@ github.com/dsoprea/go-utility/v2 v2.0.0-20221003142440-7a1927d49d9d/go.mod h1:LV
github.com/dsoprea/go-utility/v2 v2.0.0-20221003160719-7bc88537c05e/go.mod h1:VZ7cB0pTjm1ADBWhJUOHESu4ZYy9JN+ZPqjfiW09EPU=
github.com/dsoprea/go-utility/v2 v2.0.0-20221003172846-a3e1774ef349 h1:DilThiXje0z+3UQ5YjYiSRRzVdtamFpvBQXKwMglWqw=
github.com/dsoprea/go-utility/v2 v2.0.0-20221003172846-a3e1774ef349/go.mod h1:4GC5sXji84i/p+irqghpPFZBF8tRN/Q7+700G0/DLe8=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/esimov/caire v1.4.6 h1:Mys4hQMEkXO5XkgGl9D/lAXjMvAM5yZAW2T/ytGu2mk=
github.com/esimov/caire v1.4.6/go.mod h1:UL1pA62vPCK0Dsc5X8dOJ9j6zUx7mJJVHPGtlve0RFI=
github.com/esimov/pigo v1.4.6 h1:wpB9FstbqeGP/CZP+nTR52tUJe7XErq8buG+k4xCXlw=
Expand Down Expand Up @@ -93,8 +97,8 @@ github.com/go-errors/errors v1.5.1/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3Bop
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
github.com/go-text/typesetting v0.0.0-20231113130822-cf4b5dada737 h1:f3RLpvFfXcwZENOc4rPcnD+mbObAAn/HPe7GBpOptZw=
github.com/go-text/typesetting v0.0.0-20231113130822-cf4b5dada737/go.mod h1:evDBbvNR/KaVFZ2ZlDSOWWXIUKq0wCOEtzLxRM8SG3k=
github.com/go-text/typesetting v0.0.0-20231120180320-af78120ccb13 h1:pCPCcxk4AaH9MFIRj9l2sTIjtP8gBpRVwk6uOxA8fXk=
github.com/go-text/typesetting v0.0.0-20231120180320-af78120ccb13/go.mod h1:evDBbvNR/KaVFZ2ZlDSOWWXIUKq0wCOEtzLxRM8SG3k=
github.com/go-text/typesetting-utils v0.0.0-20230616150549-2a7df14b6a22 h1:LBQTFxP2MfsyEDqSKmUBZaDuDHN1vpqDyOZjcqS7MYI=
github.com/go-text/typesetting-utils v0.0.0-20230616150549-2a7df14b6a22/go.mod h1:DDxDdQEnB70R8owOx3LVpEFvpMK9eeH1o2r0yZhFI9o=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
Expand Down
17 changes: 17 additions & 0 deletions internal/environ/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ type Properties interface {
type Enver interface {
Environ() []string
LookupEnv(v string) (string, bool)
Getenv(string) string
}

// https://pkg.go.dev/github.com/muesli/termenv#Environ
type müsliTermEnvEnviron interface {
Environ() []string
Getenv(string) string
}

var _ müsliTermEnvEnviron = (Enver)(nil)

type PropertyExporter interface {
ExportProperties() map[string]string
}
Expand Down Expand Up @@ -95,6 +104,14 @@ func (p *propertiesGeneric) LookupEnv(v string) (string, bool) {
return p.Property(propkeys.EnvPrefix + v)
}

func (p *propertiesGeneric) Getenv(v string) string {
s, ok := p.Property(propkeys.EnvPrefix + v)
if ok {
return s
}
return ``
}

func (p *propertiesGeneric) Environ() []string {
if p == nil {
*p = propertiesGeneric{Locker: &sync.Mutex{}, properties: make(map[string]string)}
Expand Down
9 changes: 8 additions & 1 deletion internal/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,17 @@ type StackFrame = errorsGo.StackFrame
func NewStackFrame(pc uintptr) (frame StackFrame) { return errorsGo.NewStackFrame(pc) }

// NilReceiver returns an error with the function name if any of the arguments are nil
func NilReceiver(args ...any) error {
// func NilReceiver(arg any) error {
func NilReceiver(args ...any) error { // TODO
// return errMsgNilTester(`nil receiver`, 3, arg)
return errMsgNilTester(`nil receiver or struct field`, 3, args...)
}

// NilReceiver returns an error with the function name if any of the arguments are nil
func NilReceiverField(args ...any) error {
return errMsgNilTester(`nil receiver struct field`, 3, args...)
}

// NilParam returns an error with the function name if any of the arguments are nil
func NilParam(args ...any) error {
return errMsgNilTester(`nil parameter`, 3, args...)
Expand Down
2 changes: 1 addition & 1 deletion internal/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (p *parser) Parse(r rune) bool {
ret = true
}
case csi: // [0x40-0x7E]
if r >= 0x40 && r <= 0x7E {
if r <= 0x7E && r >= 0x40 {
p.state = ``
seqType = csi
ret = true
Expand Down
4 changes: 4 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sort"
"strconv"
"strings"
"unsafe"

"golang.org/x/exp/constraints"
"golang.org/x/exp/maps"
Expand Down Expand Up @@ -128,6 +129,9 @@ func fileLinePrefix(skip int) string {
return strings.TrimPrefix(filename, modulePath+`/`) + `:` + strconv.Itoa(line) + `: `
}

func StringToBytes(s string) []byte { return unsafe.Slice(unsafe.StringData(s), len(s)) }
func BytesToString(b []byte) string { return unsafe.String(unsafe.SliceData(b), len(b)) }

func storePosAndJumpToPosStr(x, y uint) string { return fmt.Sprintf("\033[s\033[%d;%dH", y, x) }

const restorePosStr = "\033[u"
Expand Down
2 changes: 1 addition & 1 deletion query/qdefault/qdefault.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ read:
}()
case <-time.After(q.timeOutMax):
q.prevQueryFailed.Store(true)
return ``, errors.New("time out")
return ``, errors.New(`time out`)
case <-timeoutChan:
if p == nil {
break read
Expand Down
7 changes: 7 additions & 0 deletions term/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,13 @@ func (c *termCheckerCore) NewTerminal(opts ...Option) (*Terminal, error) {
}
}

for _, f := range tm.afterSetupFuncs {
if f == nil {
continue
}
f(tm)
}

return tm, nil
}

Expand Down
19 changes: 16 additions & 3 deletions term/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,19 @@ func logBuildInfo(loggerProv logx.LoggerProvider) {
logx.Info(`build info`, loggerProv, args...)
}

func AfterSetup(f func(*Terminal)) Option {
return OptFunc(func(tm *Terminal) error {
if err := errors.NilReceiver(tm); err != nil {
return err
}
if f == nil {
return nil
}
tm.afterSetupFuncs = append(tm.afterSetupFuncs, f)
return nil
})
}

var (
TUIMode Option = tuiMode
CLIMode Option = cliMode
Expand All @@ -260,12 +273,12 @@ var noCleanUpOnInterrupt Option = OptFunc(func(t *Terminal) error { t.SetPropert

// replaceTerminal is for injecting an already set up *terminal.Terminal into *termCheckerCore.NewTerminal()
// replacing its dummy one.
func replaceTerminal(t *Terminal) Option {
func replaceTerminal(tm *Terminal) Option {
return OptFunc(func(tOld *Terminal) error {
if t == nil || tOld == nil {
if tm == nil || tOld == nil {
return errors.New(`cannot swap nil terminals`)
}
*tOld = *t
*tOld = *tm
return nil
})
}
Expand Down
16 changes: 12 additions & 4 deletions term/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ type Terminal struct {
arger
window wm.Window
closer
drawers []Drawer
resizer Resizer
passages mux.Muxers
printMu *sync.Mutex
drawers []Drawer
resizer Resizer
passages mux.Muxers
printMu *sync.Mutex
afterSetupFuncs []func(*Terminal)

ttyDefault TTY
querierDefault Querier
Expand Down Expand Up @@ -879,6 +880,13 @@ func (t *Terminal) SetCursor(xPosCells, yPosCells uint) (err error) {
return t.surveyor.SetCursor(xPosCells, yPosCells, t.tty, t.querier, t.window, t.properties)
}

func (t *Terminal) Env() environ.Enver {
if t == nil || t.properties == nil {
return nil
}
return environ.EnvToProperties(t.properties.Environ())
}

// default
func init() { wm.SetImpl(wminternal.DummyImpl()) }

Expand Down
Loading

0 comments on commit f303a41

Please sign in to comment.