func (win Interface) createTempWindow(text string, writeable bool, h int, w int) *gc.Panel { // gc.Raw(false) // gc.Echo(true) // gc.Cursor(1) _, cols := win.files.Window().MaxYX() input, _ := gc.NewWindow(11, cols, 19, 0) input.Box(0, 0) panel := gc.NewPanel(input) panel.Top() if len(text) > 0 { input.MovePrint(1, 1, text) } if writeable { gc.Raw(false) gc.Echo(true) gc.Cursor(1) _, err := input.GetString(10) if err != nil { println("Input error") } setDefaultOptions() input.Refresh() input.Erase() panel.Hide() input.Refresh() win.Refresh() } else { input.Refresh() panel.Window().ScrollOk(true) win.tmp = panel } return win.tmp }
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() { stdscr, err := gc.Init() if err != nil { log.Fatal(err) } defer gc.End() gc.Cursor(0) gc.Echo(false) stdscr.Print("A reversed plus-minus symbol: ") stdscr.AddChar(gc.ACS_PLMINUS | gc.A_REVERSE) stdscr.Refresh() stdscr.GetChar() }
func main() { stdscr, err := goncurses.Init() if err != nil { log.Fatal("init:", err) } defer goncurses.End() goncurses.Raw(true) goncurses.Echo(false) goncurses.Cursor(0) stdscr.Clear() stdscr.Keypad(true) menu_items := []string{"Choice 1", "Choice 2", "Choice 3", "Choice 4", "Exit"} items := make([]*goncurses.MenuItem, len(menu_items)) for i, val := range menu_items { items[i], _ = goncurses.NewItem(val, "") defer items[i].Free() } menu, err := goncurses.NewMenu(items) if err != nil { stdscr.Print(err) return } defer menu.Free() menu.Post() stdscr.MovePrint(20, 0, "'q' to exit") stdscr.Refresh() for { goncurses.Update() ch := stdscr.GetChar() switch goncurses.KeyString(ch) { case "q": return case "down": menu.Driver(goncurses.REQ_DOWN) case "up": menu.Driver(goncurses.REQ_UP) } } }
func (w Interface) getInput(msg string) string { input, _ := gc.NewWindow(3, 30, 20, 10) input.Box(0, 0) input.MovePrint(1, 1, msg) panel := gc.NewPanel(input) panel.Top() gc.Raw(false) gc.Echo(true) gc.Cursor(1) str, err := input.GetString(10) if err != nil { println("Input error") } panel.Delete() setDefaultOptions() return str }
func main() { stdscr, _ := gc.Init() defer gc.End() gc.StartColor() gc.Raw(true) gc.Echo(false) gc.Cursor(0) stdscr.Keypad(true) gc.InitPair(1, gc.C_RED, gc.C_BLACK) // build the menu items menu_items := []string{"Choice 1", "Choice 2", "Choice 3", "Choice 4", "Exit"} items := make([]*gc.MenuItem, len(menu_items)) for i, val := range menu_items { items[i], _ = gc.NewItem(val, "") defer items[i].Free() } // create the menu menu, _ := gc.NewMenu(items) defer menu.Free() menuwin, _ := gc.NewWindow(10, 40, 4, 14) menuwin.Keypad(true) menu.SetWindow(menuwin) dwin := menuwin.Derived(6, 38, 3, 1) menu.SubWindow(dwin) menu.Mark(" * ") // Print centered menu title y, x := menuwin.MaxYX() title := "My Menu" menuwin.Box(0, 0) menuwin.ColorOn(1) menuwin.MovePrint(1, (x/2)-(len(title)/2), title) menuwin.ColorOff(1) menuwin.MoveAddChar(2, 0, gc.ACS_LTEE) menuwin.HLine(2, 1, gc.ACS_HLINE, x-3) menuwin.MoveAddChar(2, x-2, gc.ACS_RTEE) y, x = stdscr.MaxYX() stdscr.MovePrint(y-2, 1, "'q' to exit") stdscr.Refresh() menu.Post() defer menu.UnPost() menuwin.Refresh() for { gc.Update() ch := menuwin.GetChar() switch ch { case 'q': return case gc.KEY_DOWN: menu.Driver(gc.REQ_DOWN) case gc.KEY_UP: menu.Driver(gc.REQ_UP) } } }
func main() { stdscr, err := gc.Init() if err != nil { log.Fatal("init:", err) } defer gc.End() gc.StartColor() gc.Raw(true) gc.Echo(false) gc.Cursor(0) stdscr.Keypad(true) gc.InitPair(1, gc.C_RED, gc.C_BLACK) gc.InitPair(2, gc.C_GREEN, gc.C_BLACK) gc.InitPair(3, gc.C_MAGENTA, gc.C_BLACK) // build the menu items menu_items := []string{ "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5", "Exit"} items := make([]*gc.MenuItem, len(menu_items)) for i, val := range menu_items { items[i], _ = gc.NewItem(val, "") defer items[i].Free() if i == 2 || i == 4 { items[i].Selectable(false) } } // create the menu menu, _ := gc.NewMenu(items) defer menu.Free() y, _ := stdscr.MaxYX() stdscr.MovePrint(y-3, 0, "Use up/down arrows to move; 'q' to exit") stdscr.Refresh() menu.SetForeground(gc.ColorPair(1) | gc.A_REVERSE) menu.SetBackground(gc.ColorPair(2) | gc.A_BOLD) menu.Grey(gc.ColorPair(3) | gc.A_BOLD) menu.Post() defer menu.UnPost() for { gc.Update() ch := stdscr.GetChar() switch ch { case ' ': menu.Driver(gc.REQ_TOGGLE) case 'q': return case gc.KEY_RETURN: stdscr.Move(20, 0) stdscr.ClearToEOL() stdscr.Printf("Item selected is: %s", menu.Current(nil).Name()) menu.PositionCursor() default: menu.Driver(gc.DriverActions[ch]) } } }
func main() { stdscr, _ := gc.Init() defer gc.End() gc.StartColor() gc.Raw(true) gc.Echo(false) gc.Cursor(0) stdscr.Keypad(true) gc.InitPair(1, gc.C_RED, gc.C_BLACK) gc.InitPair(2, gc.C_CYAN, gc.C_BLACK) // build the menu items menu_items := []string{ "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5", "Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10", "Exit"} items := make([]*gc.MenuItem, len(menu_items)) for i, val := range menu_items { items[i], _ = gc.NewItem(val, "") defer items[i].Free() } // create the menu menu, _ := gc.NewMenu(items) defer menu.Free() menuwin, _ := gc.NewWindow(HEIGHT, WIDTH, 4, 14) menuwin.Keypad(true) menu.SetWindow(menuwin) dwin := menuwin.Derived(6, 38, 3, 1) menu.SubWindow(dwin) menu.Format(5, 1) menu.Mark(" * ") // Print centered menu title title := "My Menu" menuwin.Box(0, 0) menuwin.ColorOn(1) menuwin.MovePrint(1, (WIDTH/2)-(len(title)/2), title) menuwin.ColorOff(1) menuwin.MoveAddChar(2, 0, gc.ACS_LTEE) menuwin.HLine(2, 1, gc.ACS_HLINE, WIDTH-2) menuwin.MoveAddChar(2, WIDTH-1, gc.ACS_RTEE) y, _ := stdscr.MaxYX() stdscr.ColorOn(2) stdscr.MovePrint(y-3, 1, "Use up/down arrows or page up/down to navigate. 'q' to exit") stdscr.ColorOff(2) stdscr.Refresh() menu.Post() defer menu.UnPost() menuwin.Refresh() for { gc.Update() if ch := menuwin.GetChar(); ch == 'q' { return } else { menu.Driver(gc.DriverActions[ch]) } } }
func main() { f, err := os.Create("err.log") if err != nil { log.Fatal(err) } defer f.Close() log.SetOutput(f) var stdscr *gc.Window stdscr, err = gc.Init() if err != nil { log.Println("Init:", err) } defer gc.End() rand.Seed(time.Now().Unix()) gc.StartColor() gc.Cursor(0) gc.Echo(false) gc.HalfDelay(1) gc.InitPair(1, gc.C_WHITE, gc.C_BLACK) gc.InitPair(2, gc.C_YELLOW, gc.C_BLACK) gc.InitPair(3, gc.C_MAGENTA, gc.C_BLACK) gc.InitPair(4, gc.C_RED, gc.C_BLACK) gc.InitPair(5, gc.C_BLUE, gc.C_BLACK) gc.InitPair(6, gc.C_GREEN, gc.C_BLACK) lines, cols := stdscr.MaxYX() pl, pc := lines, cols*3 ship := newShip(lines/2, 5) objects = append(objects, ship) field := genStarfield(pl, pc) text := stdscr.Duplicate() c := time.NewTicker(time.Second / 2) c2 := time.NewTicker(time.Second / 16) px := 0 loop: for { text.MovePrintf(0, 0, "Life: [%-5s]", lifeToText(ship.life)) stdscr.Erase() stdscr.Copy(field.Window, 0, px, 0, 0, lines-1, cols-1, true) drawObjects(stdscr) stdscr.Overlay(text) stdscr.Refresh() select { case <-c.C: spawnAsteroid(stdscr.MaxYX()) if px+cols >= pc { break loop } px++ case <-c2.C: updateObjects(stdscr.MaxYX()) drawObjects(stdscr) default: if !handleInput(stdscr, ship) || ship.Expired(-1, -1) { break loop } } } msg := "Game Over" end, err := gc.NewWindow(5, len(msg)+4, (lines/2)-2, (cols-len(msg))/2) if err != nil { log.Fatal("game over:", err) } end.MovePrint(2, 2, msg) end.Box(gc.ACS_VLINE, gc.ACS_HLINE) end.Refresh() gc.Nap(2000) }
func main() { stdscr, err := gc.Init() if err != nil { log.Fatal(err) } defer gc.End() // Turn off character echo, hide the cursor and disable input buffering gc.Echo(false) gc.CBreak(true) gc.Cursor(0) stdscr.Print("Use arrow keys to move the window. Press 'q' to exit") stdscr.NoutRefresh() // Determine the center of the screen and offset those coordinates by // half of the window size we are about to create. These coordinates will // be used to move our window around the screen rows, cols := stdscr.MaxYX() height, width := 5, 10 y, x := (rows-height)/2, (cols-width)/2 // Create a new window centered on the screen and enable the use of the // keypad on it so the arrow keys are available var win *gc.Window win, err = gc.NewWindow(height, width, y, x) if err != nil { log.Fatal(err) } win.Keypad(true) main: for { // Clear the section of screen where the box is currently located so // that it is blanked by calling Erase on the window and refreshing it // so that the chances are sent to the virtual screen but not actually // output to the terminal win.Erase() win.NoutRefresh() // Move the window to it's new location (if any) and redraw it win.MoveWindow(y, x) win.Box(0, 0) win.NoutRefresh() // Update will flush only the characters which have changed between the // physical screen and the virtual screen, minimizing the number of // characters which must be sent gc.Update() // In order for the window to display correctly, we must call GetChar() // on it rather than stdscr switch win.GetChar() { case 'q': break main case gc.KEY_LEFT: if x > 0 { x-- } case gc.KEY_RIGHT: if x < cols-width { x++ } case gc.KEY_UP: if y > 1 { y-- } case gc.KEY_DOWN: if y < rows-height { y++ } } } win.Delete() }
func main() { var active int menu := []string{"Choice 1", "Choice 2", "Choice 3", "Choice 4", "Exit"} stdscr, err := gc.Init() if err != nil { log.Fatal("init:", err) } defer gc.End() gc.Raw(true) gc.Echo(false) gc.Cursor(0) stdscr.Keypad(true) y, x := 5, 2 win, err := gc.NewWindow(HEIGHT, WIDTH, y, x) if err != nil { log.Fatal("new_window:", err) } stdscr.MovePrint(0, 0, "Use up/down arrow keys, enter to "+ "select or 'q' to quit") stdscr.MovePrint(1, 0, "You may also left mouse click on an entry to select") stdscr.Refresh() printmenu(win, menu, active) // Check to see if the Mouse is available. This function was not available // in older versions of ncurses (5.7 and older) and may return false even // if the mouse is in fact avaiable for use. if gc.MouseOk() { stdscr.MovePrint(3, 0, "WARN: Mouse support not detected.") } // Adjust the default mouse-click sensitivity to make it more responsive gc.MouseInterval(50) // If, for example, you are temporarily disabling the mouse or are // otherwise altering mouse button detection temporarily, you could // pass a pointer to a MouseButton object as the 2nd argument to // record that information. Invocation may look something like: var prev gc.MouseButton gc.MouseMask(gc.M_B1_PRESSED, nil) // only detect left mouse clicks gc.MouseMask(gc.M_ALL, &prev) // temporarily enable all mouse clicks gc.MouseMask(prev, nil) // change it back for { ch := stdscr.GetChar() switch ch { case 'q': return case gc.KEY_UP: if active == 0 { active = len(menu) - 1 } else { active -= 1 } case gc.KEY_DOWN: if active == len(menu)-1 { active = 0 } else { active += 1 } case gc.KEY_MOUSE: /* pull the mouse event off the queue */ md := gc.GetMouse() new := getactive(x, y, md.X, md.Y, menu) if new != -1 { active = new } stdscr.MovePrintf(23, 0, "Choice #%d: %s selected", active+1, menu[active]) stdscr.ClearToEOL() stdscr.Refresh() case gc.KEY_RETURN, gc.KEY_ENTER, gc.Key('\r'): stdscr.MovePrintf(23, 0, "Choice #%d: %s selected", active+1, menu[active]) stdscr.ClearToEOL() stdscr.Refresh() default: stdscr.MovePrintf(23, 0, "Character pressed = %3d/%c", ch, ch) stdscr.ClearToEOL() stdscr.Refresh() } printmenu(win, menu, active) } }
func main() { config := getConfig() wins := createInterface() for { ch := wins.GetChar() switch ch { case 'q': if wins.tmp != nil { print("yay") wins.tmp.Delete() } else { gc.Raw(false) gc.Echo(true) gc.Cursor(1) return } case gc.KEY_DOWN, 'j': wins.Action(gc.REQ_DOWN) wins.Refresh() case gc.KEY_UP, 'k': wins.Action(gc.REQ_UP) wins.Refresh() case ' ': wins.Action(gc.REQ_TOGGLE) wins.Action(gc.REQ_DOWN) wins.Refresh() // Upload case 'u': pan := wins.createTempWindow("", false, 20, 80) str := wins.getInput("Album: ") wins.Refresh() if len(str) > 0 { var list []string for _, item := range wins.files.Items() { if item.Value() { list = append(list, item.Description()) } } r := createRsync(config) r = r.addFiles(list) str = r.Upload(str) wins.cmdUpload(str, pan) } // Select all case 'V': for _, item := range wins.files.Items() { item.SetValue(true) } wins.Refresh() case gc.KEY_ENTER, gc.KEY_RETURN: default: wins.files.Driver(gc.DriverActions[ch]) } } }
func setDefaultOptions() { gc.Raw(true) gc.Echo(false) gc.Cursor(0) }