github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/dos/ui/screen.go (about)

     1  package ui
     2  
     3  import termbox "github.com/nsf/termbox-go"
     4  
     5  func (screen *Screen) Start() {
     6  	if screen.Actions == nil {
     7  		screen.Actions = make(chan Action)
     8  	}
     9  
    10  	err := termbox.Init()
    11  	if err != nil {
    12  		panic(err)
    13  	}
    14  	termbox.SetInputMode(termbox.InputEsc)
    15  
    16  	go screen.Listen()
    17  }
    18  
    19  func (screen *Screen) Stop() {
    20  	termbox.Close()
    21  }
    22  
    23  func (screen *Screen) Listen() {
    24  	for {
    25  		switch ev := termbox.PollEvent(); ev.Type {
    26  		case termbox.EventKey:
    27  			switch ev.Key {
    28  			case termbox.KeyCtrlC:
    29  				close(screen.Actions)
    30  				return
    31  			default:
    32  				screen.Actions <- KeyPress{ev.Ch, ev.Key}
    33  			}
    34  		case termbox.EventError:
    35  			//TODO: proper error handling
    36  			panic(ev.Err)
    37  		}
    38  	}
    39  }