Skip to content

Commit

Permalink
feat: it's alive!
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Nov 11, 2023
1 parent d08a7af commit bbdb6c1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
4 changes: 0 additions & 4 deletions pkg/sessions/search/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
69 changes: 64 additions & 5 deletions pkg/stories/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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":
Expand All @@ -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(),
Expand Down

0 comments on commit bbdb6c1

Please sign in to comment.