github.com/elves/elvish@v0.15.0/pkg/cli/widget.go (about) 1 package cli 2 3 import ( 4 "github.com/elves/elvish/pkg/cli/term" 5 ) 6 7 // Widget is the basic component of UI; it knows how to handle events and how to 8 // render itself. 9 type Widget interface { 10 Renderer 11 Handler 12 } 13 14 // Renderer wraps the Render method. 15 type Renderer interface { 16 // Render onto a region of bound width and height. 17 Render(width, height int) *term.Buffer 18 } 19 20 // Handler wraps the Handle method. 21 type Handler interface { 22 // Try to handle a terminal event and returns whether the event has been 23 // handled. 24 Handle(event term.Event) bool 25 } 26 27 // DummyHandler is a trivial implementation of Handler. 28 type DummyHandler struct{} 29 30 // Handle always returns false. 31 func (DummyHandler) Handle(term.Event) bool { return false } 32 33 // MapHandler is a map-backed implementation of Handler. 34 type MapHandler map[term.Event]func() 35 36 // Handle handles the event by calling the function corresponding to the event 37 // in the map. If there is no corresponding function, it returns false. 38 func (m MapHandler) Handle(event term.Event) bool { 39 fn, ok := m[event] 40 if ok { 41 fn() 42 } 43 return ok 44 } 45 46 // FuncHandler is a function-based implementation of Handler. 47 type FuncHandler func(term.Event) bool 48 49 // Handle handles the event by calling the function. 50 func (f FuncHandler) Handle(event term.Event) bool { 51 return f(event) 52 }