Example #1
0
// makes the menu inside the contacts window
func makeContactsMenu(contacts []ts.Contact, contactsWin *gc.Window) ([]*gc.MenuItem, *gc.Menu, *gc.Window) {
	menu_items := make([]*gc.MenuItem, len(contacts))
	var err error
	for i, val := range contacts {
		menu_items[i], err = gc.NewItem((val.Name), "")
		if err != nil {
			log.Fatal("Error making item for contact menu... ", err)
		}
	}
	contactMenu, err := gc.NewMenu(menu_items)
	if err != nil {
		log.Fatal("Error creating contact menu from menu_items... ", err)
	}
	contactsWinSizeY, contactsWinSizeX := contactsWin.MaxYX()
	contactsWin.Keypad(true)
	contactsMenuWin := contactsWin.Derived((contactsWinSizeY - 5), (contactsWinSizeX - 2), 3, 1)
	contactMenu.SetWindow(contactsMenuWin)
	contactMenu.Format(len(contacts), 1)
	contactMenu.Mark(" * ")

	title := "Contacts"
	contactsWin.MovePrint(1, (contactsWinSizeX/2)-(len(title)/2), title)
	contactsWin.HLine(2, 1, '-', contactsWinSizeX-2)
	contactsMenuWin.Keypad(true)
	contactsMenuWin.ScrollOk(true)
	return menu_items, contactMenu, contactsMenuWin
}
Example #2
0
func (ui *NcurseUi) PrintSubmissions(submissions []*geddit.Submission) (last string) {
	freeMenu(ui.menu)
	ui.titleBar.Printf("Frontpage")
	ui.titleBar.Refresh()

	ui.contents.Clear()
	menuItems := make([]*goncurses.MenuItem, len(submissions))
	menuMap = make(map[int]*menuItem)
	for i, sub := range submissions {
		//fmt.Sprintf("%d: Title: %s\n   Author: %s, Subreddit: %s\n   Votes: %d\n\n", i, sub.Title, sub.Author, sub.Subreddit, sub.Score)
		menuMap[i] = &menuItem{
			id: sub.FullID,
		}
		item, err := goncurses.NewItem(fmt.Sprintf("%d", i), "")
		HandleErr(err)
		ui.contents.ColorOn(1)
		ui.Println(sub.Title)
		if sub.Score > 0 {
			ui.contents.ColorOn(3)
		} else {
			ui.contents.ColorOn(2)
		}
		ui.Println(fmt.Sprintf("[%5d]", sub.Score))
		ui.contents.ColorOn(4)
		ui.Println(sub.Subreddit + " - " + sub.Author)
		ui.contents.ColorOn(1)

		menuItems[i] = item
		last = sub.FullID
	}

	if len(menuItems) > 0 {
		var err error
		ui.menu, err = goncurses.NewMenu(menuItems)
		HandleErr(err)

		ui.menu.SetSpacing(0, 3, 0)
		ui.menu.SetBackground(goncurses.ColorPair(20))
		ui.menu.SetWindow(ui.menuBar)

		ui.menu.Post()
	}

	ui.menuBar.Refresh()
	ui.contents.Refresh()

	return
}
Example #3
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 #4
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 #5
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 #6
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 #7
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 #8
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])
		}
	}
}