From bbdb6c1c0e42d1a8e3df648cd9cd4b783af2ec11 Mon Sep 17 00:00:00 2001 From: Caleb Foust Date: Sat, 11 Nov 2023 06:31:33 -0500 Subject: [PATCH] feat: it's alive! --- pkg/sessions/search/module.go | 4 -- pkg/stories/browser.go | 69 ++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/pkg/sessions/search/module.go b/pkg/sessions/search/module.go index 33cbbcd9..5408a5a8 100644 --- a/pkg/sessions/search/module.go +++ b/pkg/sessions/search/module.go @@ -9,7 +9,6 @@ import ( "github.com/cfoust/cy/pkg/geom" P "github.com/cfoust/cy/pkg/io/protocol" "github.com/cfoust/cy/pkg/sessions" - "github.com/rs/zerolog/log" ) // Address refers to a point inside of a recording. @@ -69,8 +68,6 @@ func Search(events []sessions.Event, pattern string, progress chan<- int) (resul return } - log.Info().Msgf("Search %d", len(events)) - // this MUST be set because of how the cell reader works if pattern[0] != '^' { pattern = "^" + pattern @@ -96,7 +93,6 @@ func Search(events []sessions.Event, pattern string, progress chan<- int) (resul percent := 0 for index, event := range events { newPercent := int(float64(index) / float64(len(events)) * 100) - log.Info().Msgf("event %d/%d %d", index+1, len(events), newPercent) if newPercent > percent && progress != nil { percent = newPercent progress <- percent diff --git a/pkg/stories/browser.go b/pkg/stories/browser.go index fa2abfee..378a5331 100644 --- a/pkg/stories/browser.go +++ b/pkg/stories/browser.go @@ -10,12 +10,12 @@ import ( "github.com/cfoust/cy/pkg/util" tea "github.com/charmbracelet/bubbletea" - "github.com/rs/zerolog/log" ) // A Browser lets the user switch between different stories. type Browser struct { util.Lifetime + size geom.Vec2 render *taro.Renderer fuzzy *taro.Program viewer *taro.Program @@ -30,22 +30,81 @@ func (s *Browser) Init() tea.Cmd { func (s *Browser) View(state *tty.State) { tty.Copy(geom.Vec2{}, state, s.fuzzy.State()) + + if s.viewer == nil { + return + } + + viewState := s.viewer.State() + tty.Copy(geom.Vec2{C: 30}, state, viewState) +} + +type loadedStory struct { + screen *taro.Program +} + +func (s *Browser) loadStory(story Story) tea.Cmd { + size := s.size + size.C -= 30 + return func() tea.Msg { + screen := story.init(s.Ctx()) + config := story.config + if !config.Size.IsZero() { + screen.Resize(config.Size) + } + + viewer := NewViewer( + s.Ctx(), + screen, + config, + ) + viewer.Resize(size) + return loadedStory{screen: viewer} + } } func (s *Browser) Update(msg tea.Msg) (taro.Model, tea.Cmd) { switch msg := msg.(type) { + case loadedStory: + s.viewer = msg.screen case tea.WindowSizeMsg: - s.fuzzy.Resize(geom.Size{ + size := geom.Size{ R: msg.Height, C: msg.Width, + } + s.size = size + s.fuzzy.Resize(geom.Size{ + R: msg.Height, + C: 30, }) + + if s.viewer != nil { + s.viewer.Resize(geom.Size{ + R: msg.Height, + C: msg.Width - 30, + }) + } return s, nil case taro.ScreenUpdate: + cmds := []taro.Cmd{s.watcher.Wait()} + switch msg := msg.Msg.(type) { case fuzzy.SelectedEvent: - log.Info().Msgf("selected %#v", msg) + if story, ok := msg.Option.Result.(Story); ok { + if s.viewer != nil { + s.viewer.Cancel() + s.viewer = nil + } + + cmds = append( + cmds, + s.loadStory(story), + ) + return s, tea.Batch(cmds...) + } } - return s, s.watcher.Wait() + + return s, tea.Batch(cmds...) case taro.KeyMsg: switch msg.String() { case "q": @@ -72,7 +131,7 @@ func NewBrowser( ) } - fuzzy := fuzzy.NewFuzzy(ctx, options, fuzzy.WithReverse) + fuzzy := fuzzy.NewFuzzy(ctx, options, fuzzy.WithSticky, fuzzy.WithReverse) browser := &Browser{ Lifetime: util.NewLifetime(ctx), render: taro.NewRenderer(),