Example #1
0
func InitNcurseUi() (ui *NcurseUi) {
	ui = &NcurseUi{}

	var err error
	ui.contents, err = goncurses.Init()
	HandleErr(err)

	y, x := ui.contents.MaxYX()

	ui.initColors()

	ui.contents.Resize(y-1, x-MENU_WIDTH)
	ui.contents.MoveWindow(1, MENU_WIDTH)
	ui.contents.Keypad(true)

	ui.titleBar, err = goncurses.NewWindow(1, x, 0, 0)
	HandleErr(err)
	ui.menuBar, err = goncurses.NewWindow(y-1, MENU_WIDTH, 1, 0)
	HandleErr(err)

	ui.menuBar.Border(' ', '|', ' ', ' ', ' ', '|', ' ', ' ')

	ui.titleBar.ColorOn(10)
	ui.titleBar.SetBackground(goncurses.Char('.') | goncurses.ColorPair(10))

	return
}
Example #2
0
//creates the three Main big windows that make up the GUI
func createMainWindows(stdscr *gc.Window) (*gc.Window, *gc.Window, *gc.Window, *gc.Window, *gc.Window) {
	rows, cols := stdscr.MaxYX()
	height, width := rows, int(float64(cols)*.2)

	var contactsWin, messageWinBorder, inputBorderWin, inputWin *gc.Window
	var err error
	contactsWin, err = gc.NewWindow(height, width, 0, 0)
	if err != nil {
		log.Fatal("Failed to create Contact Window:", err)
	}
	err = contactsWin.Border('|', '|', '-', '-', '+', '+', '+', '+')
	if err != nil {
		log.Fatal("Failed to create Border of Contact Window:", err)
	}

	// messageWinBorder is just the border. msgWin is the actual input window
	// doing this simplifies handling text a fair amount.
	// also the derived function never errors. Which seems dangerous
	begin_x := width + 1
	height = int(float64(rows) * .8)
	width = int(float64(cols) * .8)
	messageWinBorder, err = gc.NewWindow(height, width, 0, begin_x)
	if err != nil {
		log.Fatal("Failed to create Message Border Window:", err)
	}
	err = messageWinBorder.Border('|', '|', '-', '-', '+', '+', '+', '+')
	if err != nil {
		log.Fatal("Failed to create Border of Message Window:", err)
	}
	msgWin, err := gc.NewWindow((height - 2), (width - 2), 1, (begin_x + 1))
	if err != nil {
		log.Fatal("Failed to create the message Window:", err)
	}

	begin_y := int(float64(rows)*.8) + 1
	height = int(float64(rows) * .2)
	inputBorderWin, err = gc.NewWindow(height, width, begin_y, begin_x)
	if err != nil {
		log.Fatal("Failed to create InputBorder Window:", err)
	}
	err = inputBorderWin.Border('|', '|', '-', '-', '+', '+', '+', '+')
	if err != nil {
		log.Fatal("Failed to create Border of the InputBorder Window:", err)
	}
	// inputBorderWin is just the border. InputWin is the actual input window
	// doing this simplifies handling text a fair amount.
	// also the derived function never errors. Which seems dangerous
	inputWin, err = gc.NewWindow((height - 2), (width - 2), (begin_y + 1), (begin_x + 1))
	if err != nil {
		log.Fatal("Failed to create the inputWin Window:", err)
	}

	inputWin.ScrollOk(true)
	inputWin.Keypad(true)
	msgWin.ScrollOk(true)

	return contactsWin, messageWinBorder, msgWin, inputBorderWin, inputWin
}
Example #3
0
func (cw *chatWin) dialog(messages chan message) {
	stdscr, err := goncurses.Init()
	if err != nil {
		log.Fatal("init:", err)
	}
	defer goncurses.End()

	goncurses.Raw(false)
	stdscr.ScrollOk(true)

	rows, cols := stdscr.MaxYX()
	chats, err := goncurses.NewWindow(rows-MESSAGE_WIN_SIZE, cols, 0, 0)
	if err != nil {
		log.Fatal("Error setting up chat window")
	}

	chats.ScrollOk(true)
	chats.Box(goncurses.ACS_VLINE, goncurses.ACS_HLINE)
	chats.Refresh()

	messageWin, err := goncurses.NewWindow(MESSAGE_WIN_SIZE, cols, rows-MESSAGE_WIN_SIZE, 0)
	if err != nil {
		log.Fatal("Error setting up chat window")
	}
	cw.msgRows, cw.msgCols = messageWin.MaxYX()
	// allow special characters to come through (e.g. arrow keys)
	messageWin.Keypad(true)
	// sets terminal to non-blocking
	// this also seems to print characters as they're typed, which causes some weird workarounds
	goncurses.HalfDelay(1)

	messageWin.Print(PROMPT)
	for {
		select {
		case msg := <-messages:
			y, _ := chats.CursorYX()
			chats.MovePrint(y+1, 1, fmt.Sprintf("%s: %s", msg.From, msg.Message))
			chats.Refresh()
			messageWin.Refresh()
		default:
			line := cw.handleInput(messageWin)
			if line != "" {
				log.Println("Entered message:", line)
				msg := message{From: cw.username, Message: line}
				// TODO: uncomment once turnpike is fixed to not send events to the sender
				// y, _ := chats.CursorYX()
				// chats.MovePrint(y+1, 1, fmt.Sprintf("%s: %s", msg.From, msg.Message))
				// chats.Refresh()
				cw.msgs <- msg
			}
		}
	}
}
Example #4
0
func (fp *FilePanel) Draw() {
	window, _ := gc.NewWindow(fp.Height, fp.Width, fp.Y, fp.X)
	window.Box(0, 0)

	files, _ := ioutil.ReadDir(fp.Directory)
	fp.Files = files

	PrintHiglighted(window, 1, 1, 0, fp.Selected, "..", fp.IsActive)
	for i, f := range files {
		PrintHiglighted(window, i+2, 1, i, fp.Selected-1, RowOutput(f.Name(), f.Size(), f.Mode().IsDir(), fp.Width), fp.IsActive)
	}

	var msg = " [ " + fp.Directory + " ] "
	if fp.IsActive {
		window.AttrOn(gc.A_BOLD)
		window.AttrOn(gc.A_STANDOUT)
	}
	window.MovePrint(0, (fp.Width-len(msg))/2, msg)
	if fp.IsActive {
		window.AttrOff(gc.A_BOLD)
		window.AttrOff(gc.A_STANDOUT)
	}

	fp.Panel = gc.NewPanel(window)
}
Example #5
0
func spawnAsteroid(my, mx int) {
	var y, x, sy, sx int
	switch rand.Intn(4) {
	case 0:
		y, x = 1, rand.Intn(mx-2)+1
		sy, sx = speeds[5:][rand.Intn(4)], speeds[rand.Intn(9)]
	case 1:
		y, x = rand.Intn(my-2)+1, 1
		sy, sx = speeds[rand.Intn(9)], speeds[5:][rand.Intn(4)]
	case 2:
		y, x = rand.Intn(my-2)+1, mx-2
		sy, sx = speeds[rand.Intn(9)], speeds[rand.Intn(4)]
	case 3:
		y, x = my-2, rand.Intn(mx-2)+1
		sy, sx = speeds[rand.Intn(4)], speeds[rand.Intn(9)]
	}
	w, err := gc.NewWindow(1, 1, y, x)
	if err != nil {
		log.Println("spawnAsteroid:", err)
	}
	a := &Asteroid{Window: w, alive: true, sy: sy, sx: sx, y: y * 100,
		x: x * 100}
	a.ColorOn(2)
	a.Print("@")
	objects = append(objects, a)
}
Example #6
0
func newBullet(y, x int) *Bullet {
	w, err := gc.NewWindow(1, 1, y, x)
	if err != nil {
		log.Println("newBullet:", err)
	}
	w.AttrOn(gc.A_BOLD | gc.ColorPair(4))
	w.Print("-")
	return &Bullet{w, true}
}
Example #7
0
func newShip(y, x int) *Ship {
	w, err := gc.NewWindow(5, 7, y, x)
	if err != nil {
		log.Fatal("newShip:", err)
	}
	for i := 0; i < len(ship_ascii); i++ {
		w.MovePrint(i, 0, ship_ascii[i])
	}
	return &Ship{w, 5}
}
Example #8
0
func (c *Circuit) display() {
	stdscr, err := gc.Init()
	if err != nil {
		log.Println(err)
	}
	defer gc.End()

	rows, cols := stdscr.MaxYX()
	height, width := len(c.circuit)+1, len(c.circuit[0])+1
	y, x := (rows-height)/2, (cols-width)/2

	var win *gc.Window
	win, err = gc.NewWindow(height, width, y, x)
	if err != nil {
		log.Println(err)
	}
	defer win.Delete()

	win.Timeout(500)

	for i := 0; i != height-1; i++ {
		for j := 0; j != width-1; j++ {
			if c.circuit[i][j] == "" {
				continue
			}
			char := gc.Char([]rune(c.circuit[i][j])[0])
			win.MoveAddChar(i, j, char)
		}
	}

main:
	for {
		for _, com := range c.Components {
			com.Update()
		}

		for _, com := range c.Components {
			cx, cy, _, _ := com.Space()
			for coord, out := range com.Visual() {
				char := gc.Char([]rune(*out)[0])
				win.MoveAddChar(cx+coord.X, cy+coord.Y, char)
			}
		}

		win.NoutRefresh()
		gc.Update()

		switch win.GetChar() {
		case 'q':
			break main
		}
	}
}
Example #9
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 #10
0
func newExplosion(y, x int) *Explosion {
	w, err := gc.NewWindow(3, 3, y-1, x-1)
	if err != nil {
		log.Println("newExplosion:", err)
	}
	w.ColorOn(4)
	w.MovePrint(0, 0, `\ /`)
	w.AttrOn(gc.A_BOLD)
	w.MovePrint(1, 0, ` X `)
	w.AttrOn(gc.A_DIM)
	w.MovePrint(2, 0, `/ \`)
	return &Explosion{w, 5}
}
Example #11
0
// Returns a new Menu containing the items and a new Window containing the menu.
func newFeatureMenu(items []*gc.MenuItem, h, w, y, x int) (menu *gc.Menu, menuWin *gc.Window, err error) {
	if menu, err = gc.NewMenu(items); err != nil {
		return
	}
	if menuWin, err = gc.NewWindow(h, w, y, x); err != nil {
		return
	}
	menuWin.Keypad(true)
	menu.SetWindow(menuWin)
	menu.SubWindow(menuWin.Derived(h-2, w-2, 1, 1))
	menu.Format(h-2, 1)
	menu.Mark("")
	menuWin.Box(0, 0)
	return
}
Example #12
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 #13
0
func createWindow(h, w, y, x, bgColor int, borders bool) func() {
	win, err := gc.NewWindow(h, w, y, x)
	if err != nil {
		log.Fatal(err)
	}
	if bgColor != 0 {
		win.SetBackground(gc.Char(' ') | gc.ColorPair(int16(bgColor)))
	}
	if borders {
		win.Box(gc.ACS_VLINE, gc.ACS_HLINE)
	}
	win.NoutRefresh()

	return func() {
		win.Erase()
		win.SetBackground(gc.Char(' '))
		win.NoutRefresh()
		win.Delete()
	}
}
Example #14
0
// Returns an articleType that can be downloaded and displayed.
func ncurses(articles map[string][]articleType) (article articleType, err error) {
	// Initialize the standard screen
	stdscr, err := gc.Init()
	if err != nil {
		return
	}
	defer gc.End()
	h, w := stdscr.MaxYX()

	// Initialize the library
	gc.Raw(true)
	gc.Echo(false)
	gc.Cursor(0)
	stdscr.Keypad(true)

	// Build the user menu items
	userItems := make([]*gc.MenuItem, len(articles))
	i := 0
	for val := range articles {
		userItems[i], err = gc.NewItem(val, "")
		if err != nil {
			return
		}
		defer userItems[i].Free()
		i++
	}

	// Build the first user's article items
	articleItems := make([]*gc.MenuItem, len(articles[userItems[0].Name()]))
	i = 0
	for _, val := range articles[userItems[0].Name()] {
		articleItems[i], err = gc.NewItem(val.title, "")
		if err != nil {
			return
		}
		defer articleItems[i].Free()
		i++
	}

	// Create the user menu
	userMenu, userMenuWin, err := newFeatureMenu(userItems, h, w/2, 0, 0)
	if err != nil {
		return
	}
	// Post the user menu and refresh the user window
	userMenu.Post()
	defer userMenu.UnPost()
	userMenuWin.Refresh()

	// Create the article menu
	articleMenu, articleMenuWin, err := newFeatureMenu(articleItems, h/2, w/2, 0, w/2)
	if err != nil {
		return
	}
	// Post the article menu and refresh the article window
	articleMenu.Post()
	defer articleMenu.UnPost()
	articleMenuWin.Refresh()

	// Create the information window used to display article information
	aInfoWin, err := gc.NewWindow(h/2+1, w/2, h/2, w/2+1)
	if err != nil {
		return
	}
	displayInfo(aInfoWin, 0, 0, userItems, articles)

	active := 0
	activeA := 0
	currentMenuWin := userMenuWin
	currentMenu := userMenu
	for {
		gc.Update()
		ch := currentMenuWin.GetChar()
		switch ch {
		case 'q':
			return
		case gc.KEY_UP:
			if currentMenu == userMenu {
				if active != 0 {
					active--
					articleItems = make([]*gc.MenuItem, len(articles[userItems[active].Name()]))
					i = 0
					for _, val := range articles[userItems[active].Name()] {
						articleItems[i], _ = gc.NewItem(val.title, "")
						defer articleItems[i].Free()
						i++
					}
					articleMenu.UnPost()
					articleMenu.SetItems(articleItems)
					articleMenu.Post()
					articleMenuWin.Refresh()
					displayInfo(aInfoWin, active, activeA, userItems, articles)
				}
			} else {
				if activeA != 0 {
					activeA--
					displayInfo(aInfoWin, active, activeA, userItems, articles)
				}
			}
			currentMenu.Driver(gc.DriverActions[ch])
		case gc.KEY_DOWN:
			if currentMenu == userMenu {
				if active != len(userItems)-1 {
					active++
					articleItems = make([]*gc.MenuItem, len(articles[userItems[active].Name()]))
					i = 0
					for _, val := range articles[userItems[active].Name()] {
						articleItems[i], _ = gc.NewItem(val.title, "")
						defer articleItems[i].Free()
						i++
					}
					articleMenu.UnPost()
					articleMenu.SetItems(articleItems)
					articleMenu.Post()
					articleMenuWin.Refresh()
					displayInfo(aInfoWin, active, activeA, userItems, articles)
				}
			} else {
				if activeA != len(articleItems)-1 {
					activeA++
					aInfoWin.MovePrint(1, 1, "Auteur : ", userItems[active].Name())
					aInfoWin.Refresh()
					displayInfo(aInfoWin, active, activeA, userItems, articles)
				}
			}
			currentMenu.Driver(gc.DriverActions[ch])
		case gc.KEY_LEFT:
			// If the LEFT key is pressed, then set the active menu and active
			// menu window to the user ones. Also resets the current article counter
			if currentMenu == articleMenu {
				currentMenu = userMenu
				currentMenuWin = userMenuWin
				activeA = 0
			}
		case gc.KEY_RIGHT:
			// If the RIGHT key is pressed, then set the active menu and active
			// menu window to the article one.
			if currentMenu == userMenu {
				currentMenu = articleMenu
				currentMenuWin = articleMenuWin
			}
		case gc.KEY_ENTER, gc.KEY_RETURN, gc.Key('\r'):
			// If one of the Enter/Return key is pressed, depending on the current
			// menu, switch to the article menu or start downloading the selected
			// article.
			if currentMenu == userMenu {
				currentMenu = articleMenu
				currentMenuWin = articleMenuWin
				activeA = 0
			} else {
				article = articles[userItems[active].Name()][activeA]
				return
			}
		default:
			currentMenu.Driver(gc.DriverActions[ch])
		}
	}
}
Example #15
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 #16
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 #17
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 #18
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)
	}
}
Example #19
0
func windw() {
	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 := 20, 40
	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)

windw:
	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 windw
		case 'h':
			if x > 0 {
				x--
			}
		case 'l':
			if x < cols-width {
				x++
			}
		case 'k':
			if y > 1 {
				y--
			}
		case 'j':
			if y < rows-height {
				y++
			}
		}
	}
	win.Delete()
}