github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/cmd/syft/cli/ui/handler.go (about) 1 package ui 2 3 import ( 4 "sync" 5 6 tea "github.com/charmbracelet/bubbletea" 7 "github.com/wagoodman/go-partybus" 8 9 "github.com/anchore/bubbly" 10 "github.com/anchore/bubbly/bubbles/taskprogress" 11 stereoscopeEvent "github.com/anchore/stereoscope/pkg/event" 12 syftEvent "github.com/anchore/syft/syft/event" 13 ) 14 15 var _ interface { 16 bubbly.EventHandler 17 bubbly.MessageListener 18 bubbly.HandleWaiter 19 } = (*Handler)(nil) 20 21 type HandlerConfig struct { 22 TitleWidth int 23 AdjustDefaultTask func(taskprogress.Model) taskprogress.Model 24 } 25 26 type Handler struct { 27 WindowSize tea.WindowSizeMsg 28 Running *sync.WaitGroup 29 Config HandlerConfig 30 31 bubbly.EventHandler 32 33 onNewCatalogerTask *sync.Once 34 } 35 36 func DefaultHandlerConfig() HandlerConfig { 37 return HandlerConfig{ 38 TitleWidth: 30, 39 } 40 } 41 42 func New(cfg HandlerConfig) *Handler { 43 d := bubbly.NewEventDispatcher() 44 45 h := &Handler{ 46 EventHandler: d, 47 Running: &sync.WaitGroup{}, 48 Config: cfg, 49 onNewCatalogerTask: &sync.Once{}, 50 } 51 52 // register all supported event types with the respective handler functions 53 d.AddHandlers(map[partybus.EventType]bubbly.EventHandlerFn{ 54 stereoscopeEvent.PullDockerImage: simpleHandler(h.handlePullDockerImage), 55 stereoscopeEvent.PullContainerdImage: simpleHandler(h.handlePullContainerdImage), 56 stereoscopeEvent.ReadImage: simpleHandler(h.handleReadImage), 57 stereoscopeEvent.FetchImage: simpleHandler(h.handleFetchImage), 58 syftEvent.FileIndexingStarted: simpleHandler(h.handleFileIndexingStarted), 59 syftEvent.AttestationStarted: simpleHandler(h.handleAttestationStarted), 60 syftEvent.CatalogerTaskStarted: h.handleCatalogerTaskStarted, 61 }) 62 63 return h 64 } 65 66 func simpleHandler(fn func(partybus.Event) []tea.Model) bubbly.EventHandlerFn { 67 return func(e partybus.Event) ([]tea.Model, tea.Cmd) { 68 return fn(e), nil 69 } 70 } 71 72 func (m *Handler) OnMessage(msg tea.Msg) { 73 if msg, ok := msg.(tea.WindowSizeMsg); ok { 74 m.WindowSize = msg 75 } 76 } 77 78 func (m *Handler) Wait() { 79 m.Running.Wait() 80 }