Example #1
0
// Gets password from input
func getPass() string {
	gc.Echo(false)
	stdscr := gc.StdScr()
	var returnString []byte

	x := 0
	var c gc.Char
	var rawInput gc.Key
	for {
		rawInput = stdscr.GetChar()
		c = gc.Char(rawInput)
		if rawInput == gc.KEY_BACKSPACE || c == gc.Char(127) {
			if x != 0 {
				returnString = returnString[0 : len(returnString)-1]
				x--
			} else {
				continue
			}
		} else if rawInput == gc.KEY_RETURN {
			if x != 0 {
				return string(returnString)
			}
		} else if c > 31 && c < 127 {
			returnString = append(returnString, byte(c))
			x++
		} else {
			continue
		}
	}
}
Example #2
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 #3
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 #4
0
//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()
}
Example #5
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 #6
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 #7
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 #8
0
// contact Menu Window Navigation input handler
func contactsWindowNavigation(contactsMenuWin *gc.Window, contactMenu *gc.Menu) int {
	var c gc.Char
	var rawInput gc.Key
	for {
		gc.Update()
		rawInput = contactsMenuWin.GetChar()
		c = gc.Char(rawInput)
		if rawInput == gc.KEY_TAB || rawInput == gc.KEY_RETURN {
			return 0
		} else if c == gc.Char(27) {
			return 1
		} else if c == gc.Char('j') || rawInput == gc.KEY_DOWN {
			contactMenu.Driver(gc.REQ_DOWN)
			changeContact(contactsMenuWin, contactMenu)
		} else if c == gc.Char('k') || rawInput == gc.KEY_UP {
			contactMenu.Driver(gc.REQ_UP)
			changeContact(contactsMenuWin, contactMenu)
		} else if c == gc.Char('g') {
			contactMenu.Driver(gc.REQ_FIRST)
			changeContact(contactsMenuWin, contactMenu)
		} else if c == gc.Char('G') {
			contactMenu.Driver(gc.REQ_LAST)
			changeContact(contactsMenuWin, contactMenu)
		} else {
			continue
		}
	}
}
Example #9
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 #10
0
func GetMapper() func(rune) gc.Char {
	var mapping = map[rune]gc.Char{
		'|': gc.ACS_VLINE,
		'-': gc.ACS_HLINE,
		'<': gc.ACS_ULCORNER,
		'>': gc.ACS_URCORNER,
		'[': gc.ACS_LLCORNER,
		']': gc.ACS_LRCORNER,
		'X': gc.ACS_CKBOARD,
	}

	return func(r rune) gc.Char {
		v, p := mapping[r]
		if p {
			return v
		} else {
			return gc.Char(r)
		}
	}
}
Example #11
0
// 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)
		}
	}
}