Skip to content

Commit

Permalink
feat: can open borg files!
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Nov 9, 2023
1 parent 332549a commit d1fc43f
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 28 deletions.
4 changes: 4 additions & 0 deletions pkg/cy/api/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ func (p *PathModule) Base(path string) string {
func (p *PathModule) Join(elem []string) string {
return filepath.Join(elem...)
}

func (p *PathModule) Glob(pattern string) ([]string, error) {
return filepath.Glob(pattern)
}
41 changes: 36 additions & 5 deletions pkg/cy/api/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ package api

import (
"fmt"
"io"

"github.com/cfoust/cy/pkg/bind"
"github.com/cfoust/cy/pkg/mux/screen/replay"
"github.com/cfoust/cy/pkg/mux/screen/tree"
"github.com/cfoust/cy/pkg/sessions"
"github.com/cfoust/cy/pkg/taro"
"github.com/cfoust/cy/pkg/util"
)

type ReplayModule struct {
Tree *tree.Tree
Lifetime util.Lifetime
Tree *tree.Tree
Binds *bind.BindScope
}

func (m *ReplayModule) send(context interface{}, msg taro.Msg) error {
Expand Down Expand Up @@ -128,11 +133,37 @@ func (m *ReplayModule) Select(context interface{}) error {
func (m *ReplayModule) Open(
groupId tree.NodeID,
path string,
) error {
_, err := sessions.Open(path)
) (tree.NodeID, error) {
group, ok := m.Tree.GroupById(groupId)
if !ok {
return 0, fmt.Errorf("node not found: %d", groupId)
}

reader, err := sessions.Open(path)
if err != nil {
return err
return 0, err
}

return nil
events := make([]sessions.Event, 0)
for {
event, err := reader.Read()
if err == io.EOF || err == io.ErrUnexpectedEOF {
break
}
if err != nil {
return 0, err
}
events = append(events, event)
}

ctx := m.Lifetime.Ctx()
replay := replay.New(
ctx,
events,
m.Binds,
replay.WithNoQuit,
)

pane := group.NewPane(ctx, replay)
return pane.Id(), nil
}
9 changes: 9 additions & 0 deletions pkg/cy/cy-boot.janet
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@
(def [lines cols] (frame/size))
(frame/set-size [lines (- cols 10)]))

(key/def
cy/open-log
"open an existing log file"
(-?>>
(path/glob "/Users/cfoust/.local/share/cy/*.borg")
(fzf/find)
(replay/open (tree/root))
(pane/attach)))

(key/bind :root [prefix "j"] ot/new-shell)
(key/bind :root [prefix "n"] ot/new-project)
(key/bind :root [prefix "k"] ot/jump-project)
Expand Down
17 changes: 11 additions & 6 deletions pkg/cy/janet.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (c *Cy) resolveGroup(target *janet.Value) (*tree.Group, error) {
return group, nil
}

func (c *Cy) initJanet(ctx context.Context) (*janet.VM, error) {
func (c *Cy) initJanet(ctx context.Context, dataDir string) (*janet.VM, error) {
vm, err := janet.New(ctx)
if err != nil {
return nil, err
Expand All @@ -87,13 +87,18 @@ func (c *Cy) initJanet(ctx context.Context) (*janet.VM, error) {
modules := map[string]interface{}{
"cmd": &api.Cmd{
Lifetime: util.NewLifetime(c.Ctx()),
DataDir: dataDir,
Tree: c.tree,
},
"group": &api.GroupModule{Tree: c.tree},
"pane": &api.PaneModule{Tree: c.tree},
"path": &api.PathModule{},
"replay": &api.ReplayModule{},
"tree": &api.TreeModule{Tree: c.tree},
"group": &api.GroupModule{Tree: c.tree},
"pane": &api.PaneModule{Tree: c.tree},
"path": &api.PathModule{},
"replay": &api.ReplayModule{
Lifetime: util.NewLifetime(c.Ctx()),
Tree: c.tree,
Binds: c.replayBinds,
},
"tree": &api.TreeModule{Tree: c.tree},
}

for name, module := range modules {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cy/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func Start(ctx context.Context, options Options) (*Cy, error) {
consoleWriter := zerolog.ConsoleWriter{Out: logs.Writer(), TimeFormat: time.RFC3339}
cy.log = log.Output(zerolog.MultiLevelWriter(consoleWriter, os.Stdout))

vm, err := cy.initJanet(ctx)
vm, err := cy.initJanet(ctx, options.DataDir)
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/mux/screen/replay/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ func (r *Replay) handleCopy() (taro.Model, tea.Cmd) {
r.viewportToTerm(r.cursor),
)
return r, func() tea.Msg {
r.emit <- CopyEvent{
Text: text,
return taro.PublishMsg{
Message: CopyEvent{
Text: text,
},
}
return nil
}
}
30 changes: 19 additions & 11 deletions pkg/mux/screen/replay/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/cfoust/cy/pkg/bind"
"github.com/cfoust/cy/pkg/emu"
"github.com/cfoust/cy/pkg/events"
"github.com/cfoust/cy/pkg/geom"
"github.com/cfoust/cy/pkg/sessions"
"github.com/cfoust/cy/pkg/sessions/search"
Expand All @@ -20,6 +19,9 @@ type Replay struct {
render *taro.Renderer
binds *bind.Engine[bind.Action]

// whether Replay will actually quit itself
preventExit bool

// the size of the terminal
terminal emu.Terminal

Expand Down Expand Up @@ -71,8 +73,6 @@ type Replay struct {
isWaiting bool
searchInput textinput.Model
matches []search.SearchResult

emit chan<- events.Msg
}

var _ taro.Model = (*Replay)(nil)
Expand Down Expand Up @@ -134,7 +134,6 @@ func (r *Replay) Init() tea.Cmd {
func newReplay(
events []sessions.Event,
binds *bind.Engine[bind.Action],
emit chan<- events.Msg,
) *Replay {
ti := textinput.New()
ti.Focus()
Expand All @@ -148,36 +147,45 @@ func newReplay(
searchInput: ti,
playbackRate: 1,
binds: binds,
emit: emit,
skipInactivity: true,
}
m.gotoIndex(-1, -1)
return m
}

type ReplayOption func(r *Replay)

func WithNoQuit(r *Replay) {
r.preventExit = true
}

func New(
ctx context.Context,
recorder *sessions.Recorder,
events []sessions.Event,
replayBinds *bind.BindScope,
replayEvents chan<- events.Msg,
options ...ReplayOption,
) *taro.Program {
events := recorder.Events()

engine := bind.NewEngine[bind.Action]()
engine.SetScopes(replayBinds)
go engine.Poll(ctx)
r := newReplay(events, engine)
for _, option := range options {
option(r)
}
program := taro.New(ctx, r)

go func() {
for {
select {
case <-ctx.Done():
return
case event := <-engine.Recv():
if bindEvent, ok := event.(bind.BindEvent); ok {
replayEvents <- bindEvent
program.Publish(bindEvent)
}
}
}
}()

return taro.New(ctx, newReplay(events, engine, replayEvents))
return program
}
4 changes: 4 additions & 0 deletions pkg/mux/screen/replay/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
)

func (r *Replay) quit() (taro.Model, tea.Cmd) {
if r.preventExit {
return r, nil
}

return r, tea.Quit
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/mux/screen/replayable/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ func (r *Replayable) EnterReplay() {
events := make(chan mux.Msg)
replay := replay.New(
r.Ctx(),
r.recorder,
r.recorder.Events(),
r.binds,
events,
)

r.NewLayer(
Expand Down
9 changes: 9 additions & 0 deletions pkg/taro/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ func Sequence(cmds ...tea.Cmd) tea.Cmd {
// sequenceMsg is used internally to run the given commands in order.
type sequenceMsg []tea.Cmd

// Indicates that this message should be emitted upwards.
type PublishMsg struct {
Message Msg
}

// NewProgram creates a new Program.
func NewProgram(model Model) *Program {
p := &Program{
Expand Down Expand Up @@ -185,6 +190,10 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
case tea.QuitMsg:
return model, nil

case PublishMsg:
p.Publish(msg.Message)
continue

case tea.BatchMsg:
for _, cmd := range msg {
cmds <- cmd
Expand Down

0 comments on commit d1fc43f

Please sign in to comment.