func main() { opts := options.Client{} opts.Parse() if opts.ServerIP == "" { zc := zeroconf.NewClient("_goptyscreen._tcp.") zc.Dial() opts.ServerIP = zc.Host opts.Port = zc.Port } defer goncurses.End() _, err := goncurses.Init() if err != nil { panic(err) } goncurses.Cursor(0) // no cursor please goncurses.StartColor() list := pty_client.NewList(opts.ServerIP, opts.Port) list.Fetch() key_port, screen_port := list.GetSelection() goncurses.End() pty_client.Connect(opts.ServerIP, key_port, screen_port) }
func initializeNcurses() (stdscr *gc.Window, colors *colorsDefined, resetScreen resetScreenFn) { stdscr, err := gc.Init() if err != nil { log.Fatal(err) } defer gc.End() resetScreen = func() { gc.End() } // Turn off character echo, hide the cursor and disable input buffering gc.Echo(false) gc.CBreak(true) gc.StartColor() // initialize colors whiteOnBlack := int16(1) gc.InitPair(whiteOnBlack, gc.C_WHITE, gc.C_BLACK) greenOnBlack := int16(2) gc.InitPair(greenOnBlack, gc.C_GREEN, gc.C_BLACK) redOnBlack := int16(3) gc.InitPair(redOnBlack, gc.C_RED, gc.C_BLACK) // Set the cursor visibility. // Options are: 0 (invisible/hidden), 1 (normal) and 2 (extra-visible) gc.Cursor(0) colors = &colorsDefined{whiteOnBlack, greenOnBlack, redOnBlack} return }
func (view *View) RunForever() string { window, err := goncurses.Init() view.Window = window if err != nil { panic(err) } defer goncurses.End() goncurses.CBreak(true) goncurses.Echo(false) window.Clear() var ch goncurses.Key view.InitPaint() for { ch = window.GetChar() if ch == 'q' { break } if handler, ok := HandlerMap[ch]; ok { handler(view.World) } view.World.RunMonsterActions() if view.World.Player.Hitpoints <= 0 { return "You have been slain!" } if len(view.World.AliveMonsters()) == 0 { return "You have vanquished all the monsters!" } view.Paint() } return "" }
func main() { s, err := gc.Init() if err != nil { log.Fatal("init:", err) } defer gc.End() gc.Cursor(0) s.Move(20, 0) s.Print("Key check in ") for i := 3; i >= 1; i-- { s.MovePrint(20, 13, i) s.Refresh() time.Sleep(500 * time.Millisecond) } s.Println() gc.Echo(false) // task requirement next two lines s.Timeout(0) k := s.GetChar() if k == 0 { s.Println("No key pressed") } else { s.Println("You pressed", gc.KeyString(k)) } s.Refresh() s.Timeout(-1) gc.FlushInput() gc.Cursor(1) s.GetChar() }
func main() { s, err := goncurses.Init() if err != nil { log.Fatal("init:", err) } defer goncurses.End() height, width := s.MaxYX() fmt.Println(height, width) }
func main() { s, err := gc.Init() if err != nil { log.Fatal("init:", err) } defer gc.End() s.Move(5, 2) s.Println("Hello") s.GetChar() }
func main() { s, err := gc.Init() if err != nil { log.Fatal("init:", err) } defer gc.End() gc.Cursor(0) time.Sleep(3 * time.Second) gc.Cursor(1) s.GetChar() }
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 } } } }
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() }
func main() { s, err := goncurses.Init() if err != nil { log.Fatal("goncurses:", err) } defer goncurses.End() s.Println("Clearing screen...") s.Refresh() time.Sleep(1 * time.Second) s.Clear() // clear screen // Goncurses saves the screen on Init and restores it on End. This // GetChar() allows you to see the effect of the program before it exits. s.GetChar() // press any key to continue }
func NewSupervisor() (supervisor *Supervisor) { supervisor = new(Supervisor) defer goncurses.End() _, err := goncurses.Init() if err != nil { panic(err) } goncurses.Cursor(2) // high visibilty cursor goncurses.StartColor() supervisor.list_window = supervisor_views.NewList() supervisor.command_window = supervisor_views.NewCommand() supervisor.list_window.Refresh() supervisor.command_window.Refresh() return }
func main() { s, err := gc.Init() if err != nil { log.Fatal("init:", err) } defer gc.End() // determine color support if !gc.HasColors() { log.Fatal("no color support") } // set background color gc.StartColor() gc.InitPair(1, gc.C_WHITE, gc.C_BLUE) s.ColorOn(1) s.SetBackground(gc.Char(' ') | gc.ColorPair(1)) // blinking, different background color s.AttrOn(gc.A_BLINK) gc.InitPair(2, gc.C_WHITE, gc.C_RED) s.ColorOn(2) s.Print(" Blinking Red ") s.GetChar() }
func main() { s, err := gc.Init() if err != nil { log.Fatal("init:", err) } defer gc.End() gc.StartColor() const ( red = 1 green = 2 blue = 3 ) gc.InitPair(red, gc.C_RED, gc.C_BLACK) gc.InitPair(green, gc.C_GREEN, gc.C_BLACK) gc.InitPair(blue, gc.C_BLUE, gc.C_BLACK) s.ColorOn(red) s.Println("Red") s.ColorOn(green) s.Println("Green") s.ColorOn(blue) s.Println("Blue") s.GetChar() }
func main() { scr, _ := gc.Init() defer gc.End() rows, cols := scr.Maxyx() gc.InitPair(ChatAreaColor, gc.C_WHITE, gc.C_BLACK) gc.InitPair(InputAreaColor, gc.C_BLACK, gc.C_WHITE) chatArea := scr.Derived(rows-1, cols, 0, 0) chatArea.SetBackground(gc.Character(' ' | gc.ColorPair(ChatAreaColor))) inputArea := scr.Derived(1, cols, rows-1, 0) chatArea.SetBackground(gc.Character(' ' | gc.ColorPair(InputAreaColor))) chat := make(chan string) go func() { for msg := range chat { msg = fmt.Sprintf("%v| %v", time.Now().Format("15:04:05"), msg) chatArea.Scroll(1) chatArea.MovePrint(rows-2, 0, msg) chatArea.Refresh() } }() defer close(chat) userInputChan := make(chan string) go func() { for msg := range userInputChan { if len(msg) > 0 && msg[0] == '/' { chat <- fmt.Sprintf("Command: %s", msg[1:]) } else { chat <- msg } } }() defer close(userInputChan) gc.Echo(false) gc.CBreak(true) gc.Raw(true) chatArea.ScrollOk(true) scr.Keypad(true) chat <- "Welcome to snails shitty chat thing." chat <- "Press esc to quit, it may or may not break stuff. " chat <- "If it does, do a 'reset' to fix it." buffer := "" for { chatArea.Refresh() key := inputArea.GetChar() switch key { case gc.Key(27): return case gc.KEY_RETURN: userInputChan <- buffer buffer = "" chatArea.Refresh() case gc.Key(127): //backspace l := len(buffer) if l > 0 { buffer = buffer[:l-1] } default: buffer = fmt.Sprintf("%s%c", buffer, key) } inputArea.Clear() inputArea.MovePrint(0, 0, buffer) } }
// Why realMain? See https://groups.google.com/forum/#!topic/golang-nuts/_Twwb5ULStM // So that defer will run propoerly func realMain() (exitStatus int) { // initialize ncurses stdscr, colors, resetScreen := initializeNcurses() // clean up the screen before we die defer func() { if err := recover(); err != nil { resetScreen() fmt.Fprintf(os.Stderr, "exiting from error: %s \n", err) ERROR.Println("exiting from error: ", err) exitStatus = 1 os.Exit(1) } }() // draw the stuff on the screen msgWin, workerCountWin, durWin, reqSecWin, barsWin, scaleWin, maxWin := drawDisplay(stdscr) // create our various channels infoMsgsCh := make(chan ncursesMsg) exitCh := make(chan int) changeNumRequestersCh := make(chan interface{}) changeNumRequestersListenerCh := make(chan interface{}) reqMadeOnSecCh := make(chan interface{}) reqMadeOnSecListenerCh := make(chan interface{}) reqMadeOnSecSlaveListenerCh := make(chan interface{}) failsOnSecCh := make(chan int) durationCh := make(chan int64) durationDisplayCh := make(chan string) reqSecDisplayCh := make(chan string) bytesPerSecCh := make(chan bytesPerSecMsg) bytesPerSecDisplayCh := make(chan string) barsToDrawCh := make(chan currentBars) // start all the worker goroutines go windowRunloop(infoMsgsCh, exitCh, changeNumRequestersCh, msgWin) go barsController(reqMadeOnSecListenerCh, failsOnSecCh, barsToDrawCh, reqSecDisplayCh) go requesterController(infoMsgsCh, changeNumRequestersListenerCh, reqMadeOnSecCh, failsOnSecCh, durationCh, bytesPerSecCh, *testUrl, *introduceRandomFails) go durationWinController(durationCh, durationDisplayCh) go bytesPerSecController(bytesPerSecCh, bytesPerSecDisplayCh) numRequestersBcaster := bcast.MakeNew(changeNumRequestersCh, INFO) numRequestersBcaster.Join(changeNumRequestersListenerCh) reqMadeOnSecBcaster := bcast.MakeNew(reqMadeOnSecCh, INFO) reqMadeOnSecBcaster.Join(reqMadeOnSecListenerCh) if *listen > 0 { port := *listen // we don't want to join until we start the listener joinUp := func() { reqMadeOnSecBcaster.Join(reqMadeOnSecSlaveListenerCh) } go slave.ListenForMaster(port, changeNumRequestersCh, reqMadeOnSecSlaveListenerCh, joinUp, INFO) } else if len(slaveList) > 0 { connectToSlaves(slaveList, numRequestersBcaster, reqMadeOnSecCh) } currentScale := int64(1) // This is the main loop controlling the ncurses display. Since ncurses // wasn't designed with concurrency in mind, only one goroutine should // write to a window, so I'm putting all the window writing in here. main: for { select { case msg := <-infoMsgsCh: updateMsgWin(msg, msgWin, workerCountWin) case msg := <-durationDisplayCh: // that %7s should really be determined from durWidth durWin.MovePrint(1, 1, fmt.Sprintf("%11s", msg)) durWin.NoutRefresh() case msg := <-reqSecDisplayCh: reqSecWin.MovePrint(1, 1, fmt.Sprintf("%14s", msg)) reqSecWin.NoutRefresh() case msg := <-barsToDrawCh: currentScale = calculateScale(msg.max) // 25 is the number of rows in the window, s/b dynamic or defined elsewhere maxWin.MovePrint(1, 1, fmt.Sprintf("%5d", msg.max)) maxWin.NoutRefresh() scaleWin.MovePrint(1, 1, fmt.Sprintf("%5d", currentScale)) scaleWin.NoutRefresh() updateBarsWin(msg, barsWin, *colors, currentScale) case msg := <-bytesPerSecDisplayCh: //msgAsFloat64, err := strconv.ParseFloat(msg, 64) //if err != nil { // panic(fmt.Sprintf("converting %s failed: %v", msg, err)) //} //if msgAsFloat64 > 0 { if msg != " 0.00" { INFO.Println("bytes/sec for each second: ", msg) } case exitStatus = <-exitCh: break main } gc.Update() } msgWin.Delete() gc.End() INFO.Println("exiting with status ", exitStatus) return exitStatus }
func main() { stdscr, _ := gc.Init() defer gc.End() yMAX, xMAX := stdscr.MaxYX() yMAX = yMAX - 5 xMAX = xMAX - 5 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{ " P - 152.111.192.51", " W - 152.111.192.52", " E - 152.111.192.53", " R - 152.111.192.54", " T - 152.111.192.55", "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(yMAX, xMAX, 1, 1) menuwin.Keypad(true) menu.SetWindow(menuwin) //dwin := menuwin.Derived(6, 38, 3, 1) //menu.SubWindow(dwin) menu.Option(gc.O_SHOWDESC, true) menu.Format(5, 2) menu.Mark("*") // MovePrint 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(4, 1, gc.ACS_HLINE, WIDTH-2) // menuwin.HLine(12, 10, 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() ch := menuwin.GetChar() if ch == 'p' { cmd2 := exec.Command("clear") cmd2.Stdout = os.Stdout cmd2.Stdin = os.Stdin //cmd2.Stderr = os.Stderr cmd2.Run() cmd := exec.Command("ssh", "[email protected]") cmd.Stdout = os.Stdout cmd.Stdin = os.Stdin cmd.Stderr = os.Stderr cmd.Run() return } else if ch == 'q' { return } else if ch == 27 { return } else { menu.Driver(gc.DriverActions[ch]) } } }
func cli() { var e error scr, e = goncurses.Init() if e != nil { log.Fatal(e) } defer goncurses.End() exit := false pageNum := 0 p := NewPageCache() for !exit { scr.Refresh() h, _ := scr.MaxYX() scr.Clear() height := h - BOTTOM_MARGIN start := height * pageNum end := start + height for end > len(p.Articles) { p.GetNext() } for i, ar := range p.Articles[start:end] { scr.Printf("%d. (%d): %s\n", start+i+1, ar.Karma, ar.Title) } scr.Print("\n(n: next, p: previous, <num>c: view comments, <num>o: open in browser, q: quit) ") scr.Refresh() doneWithInput := false input := "" for !doneWithInput { c := scr.GetChar() if c == 127 { c = goncurses.Key(goncurses.KEY_BACKSPACE) } ch := goncurses.KeyString(c) switch ch { case "c": if num, err := strconv.Atoi(input); err == nil { if num < 1 { doneWithInput = true break } for num-1 > len(p.Articles) { p.GetNext() } text := p.Articles[num-1].PrintComments() line := 0 cont := true for cont { scr.Clear() scr.Print(pagify(text, line)) scr.Print("\n\n(d/u scroll 30 lines; j/k: scroll 1 line; n/p scroll 1 page; q: quit)") scr.Refresh() a := scr.GetChar() switch goncurses.KeyString(a) { case "d": line += 30 break case "u": line -= 30 break case "j": line += 1 break case "k": line -= 1 break case "n": line += h break case "p": line -= h break case "q": cont = false break default: scr.DelChar() scr.DelChar() break } // Verify lines are not negative. Bad mojo if line < 0 { line = 0 } } doneWithInput = true } else { scr.Clear() scr.Print("\n\nPlease enter a number to select a comment\n\n") scr.Refresh() scr.GetChar() doneWithInput = true } case "o": if num, err := strconv.Atoi(input); err == nil { for num-1 > len(p.Articles) { p.GetNext() } viewInBrowser := exec.Command("xdg-open", p.Articles[num-1].Url) viewInBrowser.Start() doneWithInput = true } else { scr.Clear() scr.Print("\n\nPlease enter a number to view an article\n\n") scr.Refresh() doneWithInput = true } case "q": doneWithInput = true exit = true case "n": pageNum += 1 doneWithInput = true case "p": if pageNum > 0 { pageNum -= 1 } doneWithInput = true case "enter": continue case "backspace": //Not the prettiest but whatever cy, cx := scr.CursorYX() if len(input) > 0 { input = input[:len(input)-1] scr.MoveDelChar(cy, cx-3) scr.DelChar() scr.DelChar() } else { scr.MoveDelChar(cy, cx-2) scr.DelChar() scr.DelChar() } default: input += ch break } } } }
//Get a new page by passing a url func NewPage(url string) *Page { p := Page{ Url: url, Articles: make([]*Article, 0), } url = YC_ROOT + url head, _ := http.NewRequest("HEAD", url, nil) if resp, err := client.Do(head); err == nil { c := resp.Cookies() cfduid = c[0].Raw } else { goncurses.End() log.Println(resp) log.Println(err) } req, err := http.NewRequest("GET", url, nil) if err != nil { log.Fatal(err) } doc := doReq(req) //Get all the trs with subtext for children then go back one (for the first row) rows := doc.Find(".subtext").ParentsFilteredUntil("tr", "tbody").Prev() var a bool p.NextUrl, a = doc.Find("td.title").Last().Find("a").Attr("href") if !a { goncurses.End() log.Println("Could not retreive next hackernews page. Time to go outside?") } for len(p.NextUrl) > 0 && p.NextUrl[0] == '/' { p.NextUrl = p.NextUrl[1:] } rows.Each(func(i int, row *goquery.Selection) { ar := Article{ Rank: len(p.Articles) + i, } title := row.Find(".title").Eq(1) link := title.Find("a").First() ar.Title = link.Text() if url, exists := link.Attr("href"); exists { ar.Url = url } row = row.Next() row.Find("span").Each(func(i int, s *goquery.Selection) { if karma, err := strconv.Atoi(strings.Split(s.Text(), " ")[0]); err == nil { ar.Karma = karma } else { log.Println(err) } if idSt, exists := s.Attr("id"); exists { if id, err := strconv.Atoi(strings.Split(idSt, "_")[1]); err == nil { ar.Id = id } else { log.Println(err) } } }) sub := row.Find("td.subtext") t := sub.Text() ar.Created = parseCreated(t) ar.User = sub.Find("a").First().Text() comStr := strings.Split(sub.Find("a").Last().Text(), " ")[0] if comNum, err := strconv.Atoi(comStr); err == nil { ar.NumComments = comNum } p.Articles = append(p.Articles, &ar) }) return &p }