Example #1
0
func main() {
	// Initialize goncurses. It's essential End() is called to ensure the
	// terminal isn't altered after the program ends
	stdscr, err := goncurses.Init()
	if err != nil {
		log.Fatal("init", err)
	}
	defer goncurses.End()

	// (Go)ncurses draws by cursor position and uses reverse cartisian
	// coordinate system (y,x). Initially, the cursor is positioned at the
	// coordinates 0,0 so the first call to Print will output the text at the
	// top, left position of the screen since stdscr is a window which
	// represents the terminal's screen size.
	stdscr.Print("Hello, World!")
	stdscr.MovePrint(3, 0, "Press any key to continue")

	// Refresh() flushes output to the screen. Internally, it is the same as
	// calling NoutRefresh() on the window followed by a call to Update()
	stdscr.Refresh()

	// GetChar will block execution until an input event, like typing a
	// character on your keyboard, is received
	stdscr.GetChar()
}
Example #2
0
func main() {
	stdscr, err := gc.Init()
	if err != nil {
		log.Fatal("init:", err)
	}
	defer gc.End()

	msg := "Enter a string: "
	row, col := stdscr.MaxYX()
	row, col = (row/2)-1, (col-len(msg))/2
	stdscr.MovePrint(row, col, msg)

	/* GetString will only retieve the specified number of characters. Any
	attempts by the user to enter more characters will elicit an audiable
	beep */
	var str string
	str, err = stdscr.GetString(10)
	if err != nil {
		stdscr.MovePrint(row+1, col, "GetString Error:", err)
	} else {
		stdscr.MovePrintf(row+1, col, "You entered: %s", str)
	}

	stdscr.Refresh()
	stdscr.GetChar()
}
Example #3
0
func main() {
	_, err := gc.Init()
	if err != nil {
		log.Fatal(err)
	}
	defer gc.End()

	// create a new pad of 50 rows and 200 columns...
	var pad *gc.Pad
	pad, err = gc.NewPad(50, 200)
	if err != nil {
		log.Fatal(err)
	}
	// ...and fill it with some characters
	for x := 0; x < 50; x++ {
		pad.MovePrint(x, x, "This is a pad.")
	}
	// Refresh the pad to show only a portion of the pad. Understanding
	// what these coordinates mean can be a bit tricky. The first two
	// coordinates are the position in the pad, in this case 0,5 (remember
	// the coordinates in ncurses are y,x). The second set of numbers are the
	// coordinates on the screen on which to display the content, so row 5,
	// column 10. The last set of numbers tell how high and how wide the
	// rectangle to displayed should be, in this case 15 rows long and 25
	// columns wide.
	pad.Refresh(0, 5, 5, 10, 15, 25)
	pad.GetChar()
}
Example #4
0
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()
}
Example #5
0
func main() {
	stdscr, err := gc.Init()
	if err != nil {
		return
	}
	defer gc.End()

	stdscr.Border(gc.ACS_VLINE, gc.ACS_VLINE, gc.ACS_HLINE, gc.ACS_HLINE,
		gc.ACS_ULCORNER, gc.ACS_URCORNER, gc.ACS_LLCORNER, gc.ACS_LRCORNER)
	stdscr.GetChar()
}
Example #6
0
func main() {
	stdscr, _ := gc.Init()
	defer gc.End()

	gc.StartColor()
	gc.CBreak(true)
	gc.Echo(true)
	stdscr.Keypad(true)
	stdscr.Print("Hit 'tab' to cycle through windows, 'q' to quit")

	gc.InitPair(1, gc.C_RED, gc.C_BLACK)
	gc.InitPair(2, gc.C_GREEN, gc.C_BLACK)
	gc.InitPair(3, gc.C_BLUE, gc.C_BLACK)
	gc.InitPair(4, gc.C_CYAN, gc.C_BLACK)

	var panels [3]*gc.Panel
	y, x := 4, 10

	for i := 0; i < 3; i++ {
		h, w := 10, 40
		title := "Window Number %d"

		window, _ := gc.NewWindow(h, w, y+(i*4), x+(i*10))
		window.Box(0, 0)
		window.MoveAddChar(2, 0, gc.ACS_LTEE)
		window.HLine(2, 1, gc.ACS_HLINE, w-2)
		window.MoveAddChar(2, w-1, gc.ACS_RTEE)
		window.ColorOn(int16(i + 1))
		window.MovePrintf(1, (w/2)-(len(title)/2), title, i+1)
		window.ColorOff(int16(i + 1))
		panels[i] = gc.NewPanel(window)

	}

	active := 2

	for {
		gc.UpdatePanels()
		gc.Update()

		switch stdscr.GetChar() {
		case 'q':
			return
		case gc.KEY_TAB:
			active += 1
			if active > 2 {
				active = 0
			}
			panels[active].Top()
		}
	}
}
Example #7
0
func ExampleInit() {
	// You should always test to make sure ncurses has initialized properly.
	// In order for your error messages to be visible on the terminal you will
	// need to either log error messages or output them to to stderr.
	stdscr, err := goncurses.Init()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	defer goncurses.End()
	stdscr.Print("Press enter to continue...")
	stdscr.Refresh()
}
Example #8
0
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()
}
Example #9
0
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()
}
Example #10
0
func main() {
	stdscr, err := gc.Init()
	if err != nil {
		log.Fatal(err)
	}
	defer gc.End()

	gc.StartColor()
	gc.Echo(false)
	gc.Raw(true)

	var colours = []int16{gc.C_BLACK, gc.C_BLUE, gc.C_CYAN, gc.C_GREEN,
		gc.C_MAGENTA, gc.C_RED, gc.C_WHITE, gc.C_YELLOW}
	var attributes = []struct {
		attr gc.Char
		text string
	}{
		{gc.A_NORMAL, "normal"},
		{gc.A_STANDOUT, "standout"},
		{gc.A_UNDERLINE | gc.A_BOLD, "underline"},
		{gc.A_REVERSE, "reverse"},
		{gc.A_BLINK, "blink"},
		{gc.A_DIM, "dim"},
		{gc.A_BOLD, "bold"},
	}
	stdscr.MovePrint(0, 0, "Normal terminal colors: ")
	for i, c := range colours {
		gc.InitPair(int16(i), c, c)
		stdscr.ColorOn(int16(i))
		stdscr.AddChar(' ')
		stdscr.ColorOff(int16(i))
	}
	stdscr.MovePrint(1, 0, "Bold terminal colors:   ")
	stdscr.AttrOn(gc.A_BLINK)
	for i, _ := range colours {
		stdscr.ColorOn(int16(i))
		stdscr.AddChar(' ')
		stdscr.ColorOff(int16(i))
	}
	stdscr.AttrOff(gc.A_BLINK)
	stdscr.Move(2, 0)
	for _, a := range attributes {
		stdscr.AttrOn(a.attr)
		stdscr.Println(a.text)
		stdscr.AttrOff(a.attr)
	}
	stdscr.GetChar()
}
Example #11
0
func main() {
	stdscr, _ := gc.Init()
	defer gc.End()

	row, col := stdscr.MaxYX()
	msg := "Just a string "
	stdscr.MovePrint(row/2, (col-len(msg))/2, msg)

	stdscr.MovePrintf(row-3, 0, "This screen has %d rows and %d columns. ",
		row, col)
	stdscr.Println()
	stdscr.Print("Try resizing your terminal window and then " +
		"run this program again.")
	stdscr.Refresh()
	stdscr.GetChar()
}
Example #12
0
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)
		}
	}
}
Example #13
0
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
		}
	}
}
Example #14
0
func main() {
	stdscr, _ := gc.Init()
	defer gc.End()

	var panels [3]*gc.Panel
	y, x := 2, 4

	for i := 0; i < 3; i++ {
		window, _ := gc.NewWindow(10, 40, y+i, x+(i*5))
		window.Box(0, 0)
		panels[i] = gc.NewPanel(window)
	}

	gc.UpdatePanels()
	gc.Update()

	stdscr.GetChar()
}
Example #15
0
func main() {
	gc.SlkInit(gc.SLK_323)
	scr, err := gc.Init()
	if err != nil {
		log.Fatal("INIT:", err)
	}
	defer gc.End()

	gc.StartColor()
	gc.InitPair(SOFTKEYS, gc.C_YELLOW, gc.C_BLUE)

	scr.Print("Type any key to exit...")
	labels := [...]string{"ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
		"EIGHT"}
	for i := range labels {
		gc.SlkSet(i+1, labels[i], gc.SLK_CENTER)
	}
	gc.SlkColor(SOFTKEYS)
	gc.SlkNoutRefresh()
	scr.Refresh()
	scr.GetChar()
}
Example #16
0
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])
		}
	}
}
Example #17
0
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)
}
Example #18
0
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(gc.ACS_VLINE, gc.ACS_HLINE)
		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()
}
Example #19
0
func main() {
	stdscr, _ := gc.Init()
	defer gc.End()

	gc.Echo(false)
	gc.CBreak(true)
	gc.StartColor()
	stdscr.Keypad(true)

	gc.InitPair(1, gc.C_WHITE, gc.C_BLUE)
	gc.InitPair(2, gc.C_YELLOW, gc.C_BLUE)

	fields := make([]*gc.Field, 2)
	fields[0], _ = gc.NewField(1, 10, 4, 18, 0, 0)
	defer fields[0].Free()
	fields[0].SetForeground(gc.ColorPair(1))
	fields[0].SetBackground(gc.ColorPair(2) | gc.A_UNDERLINE | gc.A_BOLD)
	fields[0].SetOptionsOff(gc.FO_AUTOSKIP)

	fields[1], _ = gc.NewField(1, 10, 6, 18, 0, 0)
	defer fields[1].Free()
	fields[1].SetForeground(gc.ColorPair(1))
	fields[1].SetBackground(gc.A_UNDERLINE)
	fields[1].SetOptionsOff(gc.FO_AUTOSKIP)
	fields[1].SetPad('*')

	form, _ := gc.NewForm(fields)
	form.Post()
	defer form.UnPost()
	defer form.Free()
	stdscr.Refresh()

	fields[0].SetBuffer("Buffer 0")

	stdscr.AttrOn(gc.ColorPair(2) | gc.A_BOLD)
	stdscr.MovePrint(4, 10, "Value 1:")
	stdscr.AttrOff(gc.ColorPair(2) | gc.A_BOLD)
	stdscr.MovePrint(6, 10, "Value 2:")
	stdscr.Refresh()

	form.Driver(gc.REQ_FIRST_FIELD)

	ch := stdscr.GetChar()
	for ch != 'q' {
		switch ch {
		case gc.KEY_DOWN, gc.KEY_TAB:
			form.Driver(gc.REQ_NEXT_FIELD)
			form.Driver(gc.REQ_END_LINE)
		case gc.KEY_UP:
			form.Driver(gc.REQ_PREV_FIELD)
			form.Driver(gc.REQ_END_LINE)
		case gc.KEY_BACKSPACE:
			form.Driver(gc.REQ_CLR_FIELD)
		default:
			form.Driver(ch)
		}
		ch = stdscr.GetChar()
	}
	stdscr.MovePrint(20, 0, fields[1].Buffer())
	stdscr.GetChar()
}
Example #20
0
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])
		}
	}
}
Example #21
0
func main() {
	stdscr, _ := gc.Init()
	defer gc.End()

	gc.StartColor()
	gc.Raw(true)
	gc.Echo(false)
	gc.Cursor(0)
	stdscr.Keypad(true)

	// build the menu items
	menu_items := []string{
		"Choice 1",
		"Choice 2",
		"Choice 3",
		"Choice 4",
		"Choice 5",
		"Choice 6",
		"Choice 7",
		"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()

	menu.Option(gc.O_ONEVALUE, false)

	y, _ := stdscr.MaxYX()
	stdscr.MovePrint(y-3, 0, "Use up/down arrows to move, spacebar to "+
		"toggle and enter to print. 'q' to exit")
	stdscr.Refresh()

	menu.Post()
	defer menu.UnPost()

	for {
		gc.Update()
		ch := stdscr.GetChar()

		switch ch {
		case 'q':
			return
		case ' ':
			menu.Driver(gc.REQ_TOGGLE)
		case gc.KEY_RETURN, gc.KEY_ENTER:
			var list string
			for _, item := range menu.Items() {
				if item.Value() {
					list += "\"" + item.Name() + "\" "
				}
			}
			stdscr.Move(20, 0)
			stdscr.ClearToEOL()
			stdscr.MovePrint(20, 0, list)
			stdscr.Refresh()
		default:
			menu.Driver(gc.DriverActions[ch])
		}
	}
}
Example #22
0
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)
		}
	}
}
Example #23
0
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)
	}
}