func main() { stdscr, err := goncurses.Init() if err != nil { log.Fatal("init", err) } defer goncurses.End() goncurses.Raw(true) // turn on raw "uncooked" input goncurses.Echo(false) // turn echoing of typed characters off goncurses.Cursor(0) // hide cursor stdscr.Keypad(true) // allow keypad input stdscr.Print("Press a key...") stdscr.Refresh() if ch := stdscr.GetChar(); ch == goncurses.KEY_F2 { stdscr.Print("The F2 key was pressed.") } else { stdscr.Print("The key pressed is: ") stdscr.AttrOn(goncurses.A_BOLD) stdscr.AddChar(goncurses.Char(ch)) stdscr.AttrOff(goncurses.A_BOLD) } stdscr.Refresh() stdscr.GetChar() }
func main() { scr, err := gc.Init() if err != nil { log.Fatal("init:", err) } defer gc.End() gc.Echo(false) scr.Println("Type characters to have them appear on the screen.") scr.Println("Press 'q' to exit.") scr.Println() // Accept input concurrently via a goroutine and connect a channel in := make(chan gc.Char) ready := make(chan bool) go func(w *gc.Window, ch chan<- gc.Char) { for { // Block until all write operations are complete <-ready // Send typed character down the channel (which is blocking // in the main loop) ch <- gc.Char(w.GetChar()) } }(scr, in) // Once a character has been received on the 'in' channel the // 'ready' channel will block until it recieves another piece of data. // This happens only once the received character has been written to // the screen. The 'in' channel then blocks on the next loop until // another 'true' is sent down the 'ready' channel signalling to the // input goroutine that it's okay to receive input for { var c gc.Char select { case c = <-in: // blocks while waiting for input from goroutine scr.Print(string(c)) scr.Refresh() case ready <- true: // sends once above block completes } // Exit when 'q' is pressed if c == gc.Char('q') { break } } }
func main() { stdscr, err := gc.Init() if err != nil { log.Fatal("init:", err) } defer gc.End() // HasColors can be used to determine whether the current terminal // has the capability of using colours. You could then chose whether or // not to use some other mechanism, like using A_REVERSE, instead if !gc.HasColors() { log.Fatal("Example requires a colour capable terminal") } // Must be called after Init but before using any colour related functions if err := gc.StartColor(); err != nil { log.Fatal(err) } gc.Echo(false) // Initialize a colour pair. Should only fail if an improper pair value // is given if err := gc.InitPair(1, gc.C_RED, gc.C_WHITE); err != nil { log.Fatal("InitPair failed: ", err) } gc.InitPair(2, gc.C_BLACK, gc.C_CYAN) stdscr.Println("Type any key to proceed and again to exit") // An example of trying to set an invalid color pair err = gc.InitPair(-1, gc.C_BLACK, gc.C_CYAN) stdscr.Println("An intentional error:", err) stdscr.Keypad(true) stdscr.MovePrint(12, 30, "Hello, World!!!") stdscr.Refresh() stdscr.GetChar() // Note that background doesn't just accept colours but will fill // any blank positions with the supplied character too. Note that newly // added text with spaces in it will have the blanks converted to the fill // character, if given stdscr.SetBackground(gc.Char('*') | gc.ColorPair(2)) // ColorOn/Off is a shortcut to calling AttrOn/Off(gc.ColorPair(pair)) stdscr.ColorOn(1) stdscr.MovePrint(13, 30, "Hello, World in Color!!!") stdscr.ColorOff(1) stdscr.Refresh() stdscr.GetChar() }