コード例 #1
0
ファイル: init.go プロジェクト: trotha01/goncurses
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()
}
コード例 #2
0
ファイル: setup.go プロジェクト: AyaTech/textsecure-client
//sets up the initial configuration of curses. Keeps code in main cleaner.
func configCurses(stdscr *gc.Window) {
	if !gc.HasColors() {
		log.Fatal("Example requires a colour capable terminal")
	}
	if err := gc.StartColor(); err != nil {
		log.Fatal("Starting Colors failed:", err)
	}
	gc.Echo(false)

	if err := gc.InitPair(1, gc.C_RED, gc.C_BLACK); err != nil {
		log.Fatal("InitPair failed: ", err)
	}
	gc.InitPair(2, gc.C_BLUE, gc.C_BLACK)
	gc.InitPair(3, gc.C_GREEN, gc.C_BLACK)
	gc.InitPair(4, gc.C_YELLOW, gc.C_BLACK)
	gc.InitPair(5, gc.C_CYAN, gc.C_BLACK)
	gc.InitPair(6, gc.C_MAGENTA, gc.C_WHITE)
	gc.InitPair(7, gc.C_MAGENTA, gc.C_BLACK)

	//set background color to black
	stdscr.SetBackground(gc.Char(' ') | gc.ColorPair(0))

	stdscr.Keypad(true)
	gc.Cursor(1)
	gc.CBreak(true)
	stdscr.Clear()
}
コード例 #3
0
ファイル: files.go プロジェクト: kuzzmi/monk
func main() {
	stdscr, err := gc.Init()
	if err != nil {
		log.Fatal(err)
	}
	defer gc.End()

	gc.Echo(false)
	gc.CBreak(true)
	gc.Cursor(0)

	var panels [2]fp.FilePanel
	rows, cols := stdscr.MaxYX()
	height, width := rows, cols/2
	y, x := 0, 0
	activePanel := 0

	panels[0] = fp.FilePanel{Height: height, Width: width, Y: y, X: x, Directory: "./", Selected: 0, IsActive: true}
	panels[1] = fp.FilePanel{Height: height, Width: width, Y: y, X: x + width, Directory: "/home/kuzzmi/", Selected: 2, IsActive: false}

	panels[0].Draw()
	panels[1].Draw()

	gc.UpdatePanels()
	gc.Update()

	stdscr.Keypad(true)
	// stdscr.GetChar()

main:
	for {
		switch panels[activePanel].Panel.Window().GetChar() {
		case 'q':
			break main
		case gc.KEY_RETURN:
			panels[activePanel].Execute()
		case gc.KEY_TAB:
			panels[0].ToggleActivity()
			panels[1].ToggleActivity()
			gc.UpdatePanels()
			gc.Update()
			if activePanel == 0 {
				activePanel = 1
			} else {
				activePanel = 0
			}
		case 'u':
			panels[activePanel].GoUp()
		case 'k':
			panels[activePanel].Select(panels[activePanel].Selected - 1)
		case 'j':
			panels[activePanel].Select(panels[activePanel].Selected + 1)
		case 'i':
			panels[activePanel].HideHidden()
		}
	}
	stdscr.Delete()
}
コード例 #4
0
ファイル: acs.go プロジェクト: trotha01/goncurses
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()
}
コード例 #5
0
ファイル: curses.go プロジェクト: avoronkov/seabattle
func NewCursesView() (i view.Interface, err error) {
	v := new(CursesView)
	if v.win, err = cur.Init(); err != nil {
		return
	}
	cur.Echo(false)
	cur.Cursor(0)

	if err = v.initCursesColors(); err != nil {
		return
	}

	i = v
	return
}
コード例 #6
0
ファイル: kvmtop.go プロジェクト: cha87de/kvmtop
func initialize() {
	// catche SIGINT signals
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	signal.Notify(c, syscall.SIGTERM)
	go func() {
		<-c
		shutdown()
	}()

	// Listen on key events
	if !OUT_BATCH {
		goncurses.Echo(false) // turn echoing of typed characters off
		goncurses.Cursor(0)   // hide cursor
		go func() {
			for true {
				ch := screen.GetChar()
				if ch == 'q' {
					shutdown()
				}
			}
		}()
	}

	// Activate devices according to user arguments
	if DEV_UUID {
		static.Initialize()
	}
	if DEV_CPU {
		cpu.Initialize()
	}
	if DEV_MEMORY {
		memory.Initialize()
	}
	if DEV_NETWORK {
		network.Initialize()
	}
	if DEV_DISK {
		disk.Initialize()
	}

	// create arrays for storing measurements
	virtualmachines = make(map[string]models.VirtualMachine)
	ppid2vmname = make(map[string]string)

	// first initial querying of measurements
	updateVirtualMachineLists()
}
コード例 #7
0
ファイル: menu.go プロジェクト: trotha01/goncurses
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)
		}
	}
}
コード例 #8
0
ファイル: mouse.go プロジェクト: trotha01/goncurses
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)
	}
}
コード例 #9
0
ファイル: menutoggle.go プロジェクト: trotha01/goncurses
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])
		}
	}
}
コード例 #10
0
ファイル: menugrey.go プロジェクト: trotha01/goncurses
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])
		}
	}
}
コード例 #11
0
func main() {
	stdscr, err := gc.Init()
	if err != nil {
		log.Fatal("init", err)
	}
	defer gc.End()

	if err := gc.StartColor(); err != nil {
		log.Fatal(err)
	}

	lines, cols := stdscr.MaxYX()

	gc.Raw(true)   // turn on raw "uncooked" input
	gc.Echo(false) // turn echoing of typed characters off
	gc.Cursor(0)   // hide cursor
	stdscr.Timeout(50)
	stdscr.Keypad(true) // allow keypad input

	gc.InitPair(1, gc.C_YELLOW, gc.C_BLACK)
	gc.InitPair(2, gc.C_RED, gc.C_BLACK)
	gc.InitPair(3, gc.C_GREEN, gc.C_BLACK)
	gc.InitPair(4, gc.C_BLUE, gc.C_BLACK)
	gc.InitPair(5, gc.C_WHITE, gc.C_BLACK)
	gc.InitPair(6, gc.C_BLACK, gc.C_BLACK)
	gc.InitPair(7, gc.C_MAGENTA, gc.C_BLACK)

	stdscr.SetBackground(gc.ColorPair(3))

	rand.Seed(time.Now().UTC().UnixNano())

	state := State{Money: 150}

	var build *Thing

	things := make([]Thing, 0, 20)
	var toUpdate int64 = 0
	lastTimestamp := int64(time.Nanosecond) * time.Now().UTC().UnixNano() / int64(time.Millisecond)

	frames := make(map[string][][]string)

	count := rand.Intn(15) + 25

	for i := 0; i < count; i++ {
		for {
			var kind Kind
			var pop int
			r := rand.Intn(2)

			switch r {
			case 0:
				kind = &SmallHouse{Frames: &frames}
				pop = 10
			case 1:
				kind = &House{Frames: &frames}
				pop = 40
			}

			house := &Thing{2 + rand.Intn(cols-8), 2 + rand.Intn(lines-8), kind}

			overlap := false
			for _, thing := range things {
				if house.Overlap(&thing) {
					overlap = true
					break
				}
			}

			if !overlap {
				things = append(things, *house)
				state.Population += pop
				break
			}
		}
	}

	var overlap bool
	var price float32
	showInfo := true

	for {
		now := int64(time.Nanosecond) * time.Now().UTC().UnixNano() / int64(time.Millisecond)
		delta := now - lastTimestamp
		lastTimestamp = now
		toUpdate -= delta

		c := stdscr.GetChar()

		switch c {
		case 'q':
			break
		case 'a':
			if build != nil && build.X > 0 {
				build.X--
			}
		case 'd':
			if build != nil && build.X+build.GetSize().X < cols {
				build.X++
			}
		case 'w':
			if build != nil && build.Y > 0 {
				build.Y--
			}
		case 's':
			if build != nil && build.Y+build.GetSize().Y < lines {
				build.Y++
			}
		case ' ':
			if build != nil && !overlap && state.Money >= price {
				things = append(things, *build)
				state.Money -= price
				build = nil
			}
		case '1':
			if state.Money >= 100 {
				build = &Thing{cols / 2, lines / 2, &Shop{Frames: &frames}}
				price = 100
			}
		case '2':
			if state.Money >= 600 {
				build = &Thing{cols / 2, lines / 2, &Factory{Frames: &frames}}
				price = 600
			}
		case '3':
			if state.Money >= 1100 {
				build = &Thing{cols / 2, lines / 2, &BigFactory{Frames: &frames}}
				price = 1100
			}
		case '4':
			if state.Money >= 2000 {
				build = &Thing{cols / 2, lines / 2, &Office{Frames: &frames}}
				price = 2000
			}
		}

		if c == 'q' {
			break
		}

		stdscr.Erase()

		if state.Population == 0 {
			stdscr.AttrOn(gc.ColorPair(3) | gc.A_BOLD)
			stdscr.MovePrint(lines/2, cols/2-4, "You won!")
			continue
		}

		if showInfo && c != 0 {
			showInfo = false
		}

		if showInfo {
			info := `
  __  __         _____       _     _ _
 |  \/  |       / ____|     | |   | | |
 | \  / |_ __  | |  __  ___ | | __| | |__   ___ _ __ __ _
 | |\/| | '__| | | |_ |/ _ \| |/ _  | '_ \ / _ \ '__/ _  |
 | |  | | |_   | |__| | (_) | | (_| | |_) |  __/ | | (_| |
 |_|  |_|_(_)   \_____|\___/|_|\__,_|_.__/ \___|_|  \__, |
                                                     __/ |
                                                     |___/



			You are Mr. Goldberg, a famous and well respected businessman.
			You're looking to expand your business to a new city.
			It looks promising, but has one serious problem - too many
			people living in it, getting in the way!
			Maybe if you build some factories near their homes they will
			eventually move out and stop bothering you?

			Instructions:
			 * earn money to build stuff
			 * pollution will cause people to move out
			 * you win when city population reaches 0!
			 * 1/2/3/4 to choose building, WASD to move, SPACE to build it, Q to quit

			                        [ any key to start ]
			`

			stdscr.AttrOn(gc.ColorPair(5) | gc.A_BOLD)
			for i, s := range strings.Split(info, "\n") {
				stdscr.MovePrint(lines/2+i-16, cols/2-30, strings.Trim(s, "\t"))
			}
			continue
		}

		overlap = false
		if toUpdate <= 0 {
			state.MoneyDelta = 0
			state.PollutionDelta = 0
		}

		for _, thing := range things {
			stdscr.AttrOn(thing.GetColor())
			thing.Draw(stdscr)
			stdscr.AttrOff(thing.GetColor())
			if toUpdate <= 0 {
				thing.Update(&state)
			}
			if build != nil && build.Overlap(&thing) {
				overlap = true
			}
		}

		if build != nil {
			if overlap {
				stdscr.AttrOn(gc.ColorPair(2))
			} else {
				stdscr.AttrOn(gc.ColorPair(7))
			}
			build.Draw(stdscr)
		}

		if toUpdate <= 0 {
			toUpdate = 1000
		}

		stdscr.AttrOn(gc.ColorPair(1) | gc.A_BOLD)
		stdscr.MovePrintf(1, 1, "Cash:       $%.2f (+$%.2f/s)", state.Money, state.MoneyDelta)
		stdscr.AttrOff(gc.ColorPair(1) | gc.A_BOLD)
		stdscr.AttrOn(gc.ColorPair(3) | gc.A_BOLD)
		stdscr.MovePrintf(2, 1, "Population: %d", state.Population)
		stdscr.AttrOff(gc.ColorPair(3) | gc.A_BOLD)
		stdscr.AttrOn(gc.ColorPair(2) | gc.A_BOLD)
		stdscr.MovePrintf(3, 1, "Pollution:  %d (+%d/s)", state.Pollution, state.PollutionDelta)
		stdscr.AttrOff(gc.ColorPair(2) | gc.A_BOLD)

		if state.Money >= 100 {
			stdscr.AttrOn(gc.ColorPair(5) | gc.A_BOLD)
		} else {
			stdscr.AttrOn(gc.ColorPair(6) | gc.A_BOLD)
		}

		stdscr.MovePrint(1, cols-50, "1: Shop ($100) [2$/s]")

		if state.Money >= 600 {
			stdscr.AttrOn(gc.ColorPair(5) | gc.A_BOLD)
		} else {
			stdscr.AttrOn(gc.ColorPair(6) | gc.A_BOLD)
		}

		stdscr.MovePrint(3, cols-50, "2: Factory ($600) [5$/s, 1 pollution/s]")

		if state.Money >= 1100 {
			stdscr.AttrOn(gc.ColorPair(5) | gc.A_BOLD)
		} else {
			stdscr.AttrOn(gc.ColorPair(6) | gc.A_BOLD)
		}

		stdscr.MovePrint(5, cols-50, "3: Big factory ($1100) [10$/s, 3 pollution/s]")
		stdscr.AttrOff(gc.A_BOLD)

		if state.Money >= 2000 {
			stdscr.AttrOn(gc.ColorPair(5) | gc.A_BOLD)
		} else {
			stdscr.AttrOn(gc.ColorPair(6) | gc.A_BOLD)
		}

		stdscr.MovePrint(7, cols-50, "4: Office ($2000) [+20% money generated]")
		stdscr.AttrOff(gc.A_BOLD)

		stdscr.Refresh()
	}

	stdscr.GetChar()
}
コード例 #12
0
ファイル: input.go プロジェクト: AyaTech/textsecure-client
// handles keyboard input
func inputHandler(inputWin *gc.Window, stdscr *gc.Window, contactsMenuWin *gc.Window, contactMenu *gc.Menu, msgWin *gc.Window) {
	var NLlocate = map[int]newLine{}
	var c gc.Char
	var rawInput gc.Key
	max_y, max_x := inputWin.MaxYX()
	for {
		rawInput = inputWin.GetChar()
		c = gc.Char(rawInput)
		// debugLog.Println(rawInput)
		// debugLog.Println(c)

		//Escape to Quit
		if c == gc.Char(27) {
			break
		} else if rawInput == gc.KEY_BACKSPACE || c == gc.Char(127) {
			//Delete Key
			y, x := inputWin.CursorYX()
			var del = byte('F')
			if x != 0 {
				inputWin.MoveDelChar(y, x-1)
				del = inputBuffer[placer]
				copy(inputBuffer[placer:len(inputBuffer)-1], inputBuffer[placer+1:])
				inputBuffer = inputBuffer[0 : len(inputBuffer)-1]
				placer--
				if del != byte('\n') && NLlocate[y+scroll]._cursorX > x {
					temp := newLine{NLlocate[y+scroll]._cursorX - 1, NLlocate[y+scroll]._placer - 1}
					NLlocate[y+scroll] = temp
				}
				//debugLog.Println(inputBuffer)
			} else if y != 0 { //when x==0 and y!=0
				inputWin.Move(y-1, max_x-1)
				inputWin.MoveDelChar(y-1, max_x-1)
				del = inputBuffer[placer]
				copy(inputBuffer[placer:len(inputBuffer)-1], inputBuffer[placer+1:])
				inputBuffer = inputBuffer[0 : len(inputBuffer)-1]
				//debugLog.Println(inputBuffer)
				placer--
			}
			if del == byte('\n') {
				inputWin.Erase()
				inputWin.Print(string(inputBuffer))
				inputWin.Move(y-1, NLlocate[y-1+scroll]._cursorX)
				temp, check := NLlocate[y+scroll]
				var temp_cursor = temp._cursorX
				var temp_placer = temp._placer
				if check && NLlocate[y-1+scroll]._cursorX+temp_cursor >= max_x {
					_newLine := newLine{NLlocate[y-1+scroll]._cursorX + temp_cursor - max_x, NLlocate[y+scroll]._placer - 1}
					NLlocate[y+scroll] = _newLine
					delete(NLlocate, y-1)
				} else if check { // check if there are any '\n' this line
					var largest = -1          // if yes, select all '\n' and move
					for i := range NLlocate { // placer by 1 and adjust cursor
						if i >= y+scroll { // accordingly
							if next_nl, ok := NLlocate[i+1]; ok {
								new_nl := newLine{next_nl._cursorX, next_nl._placer - 1}
								NLlocate[i] = new_nl
							}
						}
						if i > largest {
							largest = i
						}
					}
					delete(NLlocate, largest) // delete last map entry
					_newLine := newLine{NLlocate[y-1+scroll]._cursorX + temp_cursor, NLlocate[y-1+scroll]._placer + temp_placer - 1}
					NLlocate[y-1+scroll] = _newLine
				} else {
					delete(NLlocate, y-1+scroll)
				}
			}
		} else if c == gc.KEY_PAGEDOWN {
			//debugLog.Println("HIT DOWN")
			msgWin.Scroll(-10)
			msgWin.Refresh()
			inputWin.Refresh()
		} else if c == gc.KEY_PAGEUP {
			//debugLog.Println("HIT UP")
			msgWin.Scroll(10)
			msgWin.Refresh()
			inputWin.Refresh()
		} else if c == gc.KEY_LEFT {
			y, x := inputWin.CursorYX()
			if x != 0 {
				inputWin.Move(y, x-1)
				placer--
			} else if y != 0 {
				inputWin.Move(y-1, max_x-1)
				placer--
			}
			if len(inputBuffer) > 0 && inputBuffer[placer+1] == byte('\n') {
				inputWin.Move(y-1, NLlocate[y-1+scroll]._cursorX)
			}
		} else if c == gc.KEY_RIGHT {
			y, x := inputWin.CursorYX()
			placer++
			if inputBuffer == nil || placer == len(inputBuffer) {
				inputBuffer = append(inputBuffer, byte(' '))
			}
			if inputBuffer[placer] == byte('\n') || x >= max_x-1 {
				inputWin.Move(y+1, 0)
			} else {
				inputWin.Move(y, x+1)
			}
		} else if c == gc.KEY_UP {
			y, x := inputWin.CursorYX()
			if y == 0 && placer == 0 {
				continue
			} else if y == 0 && scroll > 0 {
				inputWin.Move(0, x)
				inputWin.Scroll(-1)
				scroll -= 1
				if NLlocate[y-2+scroll]._placer != 0 {
					inputWin.Erase()
					inputWin.Print(string(inputBuffer[(NLlocate[y-2+scroll]._placer):]))
				} else if placer-max_x-x > 0 {
					inputWin.Erase()
					inputWin.Print(string(inputBuffer[(placer - x - max_x):]))
				} else {
					inputWin.Erase()
					inputWin.Print(string(inputBuffer))
				}
			}
			if y != 0 {
				inputWin.Move(y-1, x)
				placer -= max_x
				if placer < 0 {
					placer = 0
				}
			}
			if NLlocate[y-1+scroll]._placer != 0 {
				if NLlocate[y-1+scroll]._cursorX < x {
					placer = NLlocate[y-1+scroll]._placer
					inputWin.Move(y-1, NLlocate[y-1+scroll]._cursorX)
				} else {
					placer = NLlocate[y-1+scroll]._placer - (NLlocate[y-1+scroll]._cursorX - x)
				}
			}
		} else if c == gc.KEY_DOWN {
			y, x := inputWin.CursorYX()
			if y != max_y {
				inputWin.Move(y+1, x)
				if NLlocate[y+scroll]._placer == 0 {
					placer += max_x
				} else {
					placer = NLlocate[y+scroll]._placer + x + 1
				}
			} else if y == max_y {
				inputWin.Scroll(1)
				scroll += 1
				inputWin.Move(max_y-1, x)
				if NLlocate[y+scroll]._placer == 0 {
					placer += max_x
				} else {
					placer = NLlocate[y+scroll]._placer + x + 1
				}
			}
			if placer >= len(inputBuffer) {
				for i := len(inputBuffer); i < placer+1; i++ {
					inputBuffer = append(inputBuffer, byte(' '))
				}
			}
		} else if rawInput == gc.KEY_TAB {
			y, x := inputWin.CursorYX()
			gc.Cursor(0)
			escapeHandler := contactsWindowNavigation(contactsMenuWin, contactMenu)
			if escapeHandler == 1 {
				return
			}
			gc.Cursor(1)
			inputWin.Move(y, x)
		} else if rawInput == gc.KEY_RETURN {
			placer = -1
			for i := range NLlocate {
				delete(NLlocate, i)
			}
			sendMsg(inputWin, globalMsgWin)
		} else if rawInput == gc.KEY_SRIGHT {
			y, x := inputWin.CursorYX()
			if inputBuffer == nil || placer == len(inputBuffer)-1 {
				if y == max_y-1 {
					scroll++
				}
				inputWin.Print("\n")
				temp := newLine{x, placer}
				NLlocate[y+scroll] = temp
				placer++
				inputBuffer = append(inputBuffer, byte('\n'))
			} else {
				inputWin.Erase()
				inputBuffer = append(inputBuffer, byte('\n'))
				copy(inputBuffer[placer+1:], inputBuffer[placer:])
				inputBuffer[placer+1] = byte('\n')
				inputWin.Print(string(inputBuffer))
				temp := newLine{x, placer}
				placer++
				nextholder, check := NLlocate[y+1+scroll]
				if check {
					for i := range NLlocate {
						if i == y+scroll {
							_newLine := newLine{NLlocate[i]._cursorX + 1 - x, NLlocate[i]._placer + 1}
							nextholder := NLlocate[i+1]
							_ = nextholder
							NLlocate[i+1] = _newLine
						} else if i > y {
							temp := NLlocate[i+1]
							NLlocate[i+1] = nextholder
							nextholder = temp
						}
					}
				}
				NLlocate[y+scroll] = temp
				inputWin.Move(y+1, 0)
			}
		} else if rawInput == gc.KEY_SLEFT {
		} else {
			y, x := inputWin.CursorYX()
			if inputBuffer == nil || placer == len(inputBuffer)-1 {
				inputWin.Print(string(c))
				inputBuffer = append(inputBuffer, byte(c))
			} else {
				inputWin.Erase()
				inputBuffer = append(inputBuffer, byte(c))
				copy(inputBuffer[placer+1:], inputBuffer[placer:])
				inputBuffer[placer+1] = byte(c)
				inputWin.Print(string(inputBuffer))
				for i := range NLlocate {
					if i > y+scroll {
						tempLine := newLine{NLlocate[i]._cursorX, NLlocate[i]._placer + 1}
						NLlocate[i] = tempLine
					}
				}
				if NLlocate[y+scroll]._cursorX >= x {
					if NLlocate[y+scroll]._cursorX == max_x {
						copy(inputBuffer[NLlocate[y+scroll]._placer:len(inputBuffer)-1], inputBuffer[NLlocate[y+scroll]._placer+1:])
						inputBuffer = inputBuffer[0 : len(inputBuffer)-1]
						delete(NLlocate, y)
					} else {
						temp := newLine{NLlocate[y+scroll]._cursorX + 1, NLlocate[y+scroll]._placer + 1}
						NLlocate[y+scroll] = temp
					}
				}
			}
			placer++
			inputWin.Move(y, x+1)
		}
	}
}
コード例 #13
0
ファイル: ncurses.go プロジェクト: aubm/gotris
func initConfiguration() {
	gc.CBreak(true)
	gc.Echo(false)
	gc.Cursor(0)
}
コード例 #14
0
ファイル: starfield.go プロジェクト: trotha01/goncurses
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)
}
コード例 #15
0
ファイル: window.go プロジェクト: kuzzmi/monk
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()
}
コード例 #16
0
ファイル: curses.go プロジェクト: Depado/mdblog-curses-reader
// 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])
		}
	}
}
コード例 #17
0
ファイル: submenu.go プロジェクト: trotha01/goncurses
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)
		}
	}
}
コード例 #18
0
ファイル: scrollmenu.go プロジェクト: trotha01/goncurses
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])
		}
	}
}