Compare commits

...

5 Commits

Author SHA1 Message Date
vilmibm 4ee66d876e experimental WIP
this commit experiments with node and edge rendering.

For these I'm trying a Page element with a list of Node and Edge
primitives.

Node primitives are textviews.

Edges are a new, custom primitive.

the edge only draws a line from 0,0 to 5,5 right now but shows that I'm
able to implement a Draw that just writes arbitrary characters to the
screen.

I need to do several things, some of which suggest that i'm abusing the
Pages primitive in terrible ways:

- Add and remove nodes as needed. Pages has no Clear() or Reset(). I
  will have to manually keep track of the page -> node relationship
- Give nodes and edges page names that are unique. I won't really be
  using these and I must avoid collisions.
- Relate edges to nodes somehow so they can be drawn properly
- Ensure that all edges are drawn before all nodes

and a lot more

A tremendous amount of work is ahead but I at least have some basic
stuff proven to be workable.
2023-06-11 22:00:46 -04:00
vilmibm 2bc70d3377 handle ex cmds; you can quit 2023-06-11 20:35:49 -04:00
vilmibm 2003843965 WIP layout 2023-06-11 19:48:45 -04:00
vilmibm 80fad75a59 basic main 2023-06-11 18:58:01 -04:00
vilmibm c9df23dd3c sundry 2023-06-11 18:57:50 -04:00
4 changed files with 346 additions and 0 deletions

2
.gitignore vendored 100644
View File

@ -0,0 +1,2 @@
*.swp
porphyry

26
go.mod 100644
View File

@ -0,0 +1,26 @@
module git.tilde.town/vilmibm/porphyry
go 1.20
require (
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/gdamore/tcell/v2 v2.6.0
github.com/mattn/go-runewidth v0.0.14
github.com/spf13/cobra v1.7.0
)
require (
github.com/gdamore/encoding v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.8 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/rivo/tview v0.0.0-20230530133550-8bd761dda819 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
)

81
go.sum 100644
View File

@ -0,0 +1,81 @@
github.com/AlecAivazis/survey/v2 v2.3.6 h1:NvTuVHISgTHEHeBFqt6BHOe4Ny/NwGZr7w+F8S9ziyw=
github.com/AlecAivazis/survey/v2 v2.3.6/go.mod h1:4AuI9b7RjAR+G7v9+C4YSlX/YL3K3cWNXgWXOhllqvI=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI=
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.6.0 h1:OKbluoP9VYmJwZwq/iLb4BxwKcwGthaa1YNBJIyCySg=
github.com/gdamore/tcell/v2 v2.6.0/go.mod h1:be9omFATkdr0D9qewWW3d+MEvl5dha+Etb5y65J2H8Y=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/tview v0.0.0-20230530133550-8bd761dda819 h1:qRMCGgwKl66uWe7Hnzl5bCvZlfrLNIxOx7K00j5XeNc=
github.com/rivo/tview v0.0.0-20230530133550-8bd761dda819/go.mod h1:nVwGv4MP47T0jvlk7KuTTjjuSmrGO4JF0iaiNt4bufE=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

237
main.go 100644
View File

@ -0,0 +1,237 @@
package main
import (
"fmt"
"math/rand"
"os"
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/spf13/cobra"
)
type mode string
const (
modeNormal mode = "normal"
modeFocus mode = "focus"
modeSearch mode = "search"
modeEx mode = "ex"
)
type UI struct {
Mode mode
App *tview.Application
Fields []field
Nodes []node
// UI things
Pages *tview.Pages
TopFlex *tview.Flex
Field *tview.Pages
FieldBar *tview.Flex
BottomBar *tview.Pages
ExInput *tview.InputField
ExOutput *tview.TextView
}
func (ui *UI) setMode(m mode) {
ui.Mode = m
switch ui.Mode {
case modeNormal:
ui.App.SetFocus(ui.TopFlex)
ui.ExInput.SetText("")
case modeEx:
ui.App.SetFocus(ui.ExInput)
}
}
func (ui *UI) handleInput(event *tcell.EventKey) *tcell.EventKey {
switch ui.Mode {
case modeNormal:
switch event.Rune() {
case ':':
ui.setMode(modeEx)
return nil
}
case modeEx:
case modeFocus:
case modeSearch:
default:
panic("mode?")
}
return event
}
func (ui *UI) handleExInput(key tcell.Key) {
text := ui.ExInput.GetText()
ui.setMode(modeNormal)
if key != tcell.KeyEnter {
return
}
switch text {
case "q", "quit":
ui.quit()
}
ui.ExOutput.SetText("")
fmt.Fprintf(ui.ExOutput, "did not understand '%s', sorry", text)
}
type field struct {
Name string
Selected bool
}
type node struct {
Text string
Edges []*node
X int
Y int
}
type Edge struct {
}
func (e Edge) Draw(screen tcell.Screen) {
screen.SetContent(0, 0, '!', []rune{}, tcell.StyleDefault)
}
func (e Edge) GetRect() (int, int, int, int) {
return 0, 0, 0, 0
}
func (e Edge) SetRect(x, y, width, height int) {
}
func (e Edge) InputHandler() func(_ *tcell.EventKey, _ func(p tview.Primitive)) {
return func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {}
}
func (e Edge) HasFocus() bool {
return false
}
func (e Edge) Focus(delegate func(p tview.Primitive)) {
}
func (e Edge) Blur() {
}
func (e Edge) MouseHandler() func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (consumed bool, capture tview.Primitive) {
return func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (consumed bool, capture tview.Primitive) {
return false, nil
}
}
func NewUI() *UI {
app := tview.NewApplication()
ui := UI{
Mode: modeNormal,
App: app,
Fields: []field{{"scratch", true}, {"test", false}},
Nodes: []node{},
Pages: tview.NewPages(),
TopFlex: tview.NewFlex(),
Field: tview.NewPages(),
FieldBar: tview.NewFlex(),
BottomBar: tview.NewPages(),
ExInput: tview.NewInputField(),
ExOutput: tview.NewTextView(),
}
app.SetInputCapture(ui.handleInput)
ui.TopFlex.SetDirection(tview.FlexRow)
ui.TopFlex.AddItem(ui.Field, 0, 20, true)
ui.TopFlex.AddItem(ui.FieldBar, 0, 1, false)
ui.TopFlex.AddItem(ui.BottomBar, 0, 1, false)
ui.Pages.AddPage("main", ui.TopFlex, true, true)
ui.BottomBar.AddPage("output", ui.ExOutput, true, true)
ui.BottomBar.AddPage("input", ui.ExInput, true, false)
ui.ExInput.SetLabel(":")
ui.ExInput.SetDoneFunc(ui.handleExInput)
ui.ExOutput.SetMaxLines(1)
fmt.Fprintf(ui.ExOutput, "porphyry has started. :q to quit")
ui.FieldBar.SetDirection(tview.FlexColumn)
ui.Nodes = append(ui.Nodes, node{
Text: "foobar\nbaz\nquux",
})
ui.Nodes = append(ui.Nodes, node{
Text: "hello\nthere\nhow",
})
x := 0
y := 0
rand.Seed(time.Now().Unix())
for _, n := range ui.Nodes {
b := tview.NewTextView()
b.SetText(n.Text)
b.SetBorder(true)
b.SetRect(x, y, 10, 5)
x += 20
ui.Field.AddPage(fmt.Sprintf("%d", rand.Intn(10000)), b, false, true)
}
ui.Field.AddPage("edge", Edge{}, false, true)
app.SetBeforeDrawFunc(func(_ tcell.Screen) bool {
// Handle nodes
// TODO
// Handle field bar
ui.FieldBar.Clear()
for _, f := range ui.Fields {
t := tview.NewTextView().SetTextStyle(tcell.StyleDefault.Bold(f.Selected))
t.SetMaxLines(1)
t.SetBorder(true)
fmt.Fprintf(t, f.Name)
ui.FieldBar.AddItem(t, 0, 1, false)
}
// Handle ex mode prompt
if ui.Mode == modeEx {
ui.BottomBar.SwitchToPage("input")
} else {
ui.BottomBar.SwitchToPage("output")
}
return false
})
app.SetRoot(ui.Pages, true)
return &ui
}
func (ui *UI) quit() {
ui.App.Stop()
}
func _main() error {
cmd := &cobra.Command{
Use: "porphyry",
RunE: func(cmd *cobra.Command, args []string) error {
return NewUI().App.Run()
},
}
return cmd.Execute()
}
func main() {
if err := _main(); err != nil {
fmt.Fprintf(os.Stderr, err.Error()+"\n")
}
}