Esempio n. 1
0
func main() {

	defineFlags()
	if SHOW_VERSION {
		fmt.Println("kvmtop version " + VERSION)
		return
	}
	if !OUT_BATCH {
		screenx, err := goncurses.Init()
		if err != nil {
			log.Fatal("init", err)
		}
		screen = screenx
		defer goncurses.End()
	}

	initialize()
	for n := -1; RUNCOUNT == -1 || n < RUNCOUNT; n++ {
		start := time.Now()
		measureVirtualMachines(n)
		nextRun := start.Add(time.Duration(FREQUENCY) * time.Second)
		updateVirtualMachineLists()
		time.Sleep(nextRun.Sub(time.Now()))
	}
}
Esempio n. 2
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()
}
Esempio n. 3
0
func main() {
	stdscr, err := gc.Init()
	if err != nil {
		log.Fatal("init:", err)
	}
	defer gc.End()

	msg := "Enter a string: "
	row, col := stdscr.MaxYX()
	row, col = (row/2)-1, (col-len(msg))/2
	stdscr.MovePrint(row, col, msg)

	/* GetString will only retieve the specified number of characters. Any
	attempts by the user to enter more characters will elicit an audiable
	beep */
	var str string
	str, err = stdscr.GetString(10)
	if err != nil {
		stdscr.MovePrint(row+1, col, "GetString Error:", err)
	} else {
		stdscr.MovePrintf(row+1, col, "You entered: %s", str)
	}

	stdscr.Refresh()
	stdscr.GetChar()
}
Esempio n. 4
0
func main() {
	_, err := gc.Init()
	if err != nil {
		log.Fatal(err)
	}
	defer gc.End()

	// create a new pad of 50 rows and 200 columns...
	var pad *gc.Pad
	pad, err = gc.NewPad(50, 200)
	if err != nil {
		log.Fatal(err)
	}
	// ...and fill it with some characters
	for x := 0; x < 50; x++ {
		pad.MovePrint(x, x, "This is a pad.")
	}
	// Refresh the pad to show only a portion of the pad. Understanding
	// what these coordinates mean can be a bit tricky. The first two
	// coordinates are the position in the pad, in this case 0,5 (remember
	// the coordinates in ncurses are y,x). The second set of numbers are the
	// coordinates on the screen on which to display the content, so row 5,
	// column 10. The last set of numbers tell how high and how wide the
	// rectangle to displayed should be, in this case 15 rows long and 25
	// columns wide.
	pad.Refresh(0, 5, 5, 10, 15, 25)
	pad.GetChar()
}
Esempio n. 5
0
func main() {
	// Initialize goncurses. It's essential End() is called to ensure the
	// terminal isn't altered after the program ends
	stdscr, err := goncurses.Init()
	if err != nil {
		log.Fatal("init", err)
	}
	defer goncurses.End()

	// (Go)ncurses draws by cursor position and uses reverse cartisian
	// coordinate system (y,x). Initially, the cursor is positioned at the
	// coordinates 0,0 so the first call to Print will output the text at the
	// top, left position of the screen since stdscr is a window which
	// represents the terminal's screen size.
	stdscr.Print("Hello, World!")
	stdscr.MovePrint(3, 0, "Press any key to continue")

	// Refresh() flushes output to the screen. Internally, it is the same as
	// calling NoutRefresh() on the window followed by a call to Update()
	stdscr.Refresh()

	// GetChar will block execution until an input event, like typing a
	// character on your keyboard, is received
	stdscr.GetChar()
}
Esempio n. 6
0
// In addition to sending a message using janimo's library, also clears screen and resets buffer
func sendMsg(inputWin *gc.Window, msgWin *gc.Window) {
	if len(inputBuffer) != 0 {
		msg := string(inputBuffer)
		to := currentContact
		err := ts.SendMessage(to, msg)
		if err != nil {
			gc.End()
			log.Fatal("SendMessage failed yo: ", err)
		}

		var b bytes.Buffer
		t := time.Now()
		if t.Hour() < 10 {
			b.WriteString("0")
		}
		b.WriteString(strconv.Itoa(t.Hour()))
		b.WriteString(":")
		if t.Minute() < 10 {
			b.WriteString("0")
		}
		b.WriteString(strconv.Itoa(t.Minute()))
		b.WriteString(": ")
		b.WriteString(msg)

		printToMsgWindow(b.String(), msgWin, true)
		b.Reset()
		insertMessage("You", currentContact, []byte(msg), nil)
		inputBuffer = []byte{}
		inputWin.Erase()
	}
}
Esempio n. 7
0
File: files.go Progetto: 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()
}
Esempio n. 8
0
func ExitNC() {
	winMutex.Lock()
	if win != nil {
		win = nil
		nc.End()
		ncquit <- true
	}
	winMutex.Unlock()
}
Esempio n. 9
0
// creates a curses based TUI for the textsecure library
func main() {
	stdscr, err := gc.Init()
	if err != nil {
		log.Fatal("Error initializing curses:", err)
	}
	defer gc.End()
	configCurses(stdscr)

	client := &ts.Client{
		GetConfig:           getConfig,
		GetLocalContacts:    getLocalContacts,
		GetVerificationCode: getVerificationCode,
		GetStoragePassword:  passphraseUnlock,
		MessageHandler:      recieveMessage,
		RegistrationDone:    registrationDone,
	}
	err = ts.Setup(client)
	if err != nil {
		log.Fatal("Could not initialize textsecure library", err)
	}

	db = setupDatabase()

	contacts, err := ts.GetRegisteredContacts()
	if err != nil {
		log.Fatal("Could not get contacts: %s\n", err)
	}

	telToName = make(map[string]string)
	for _, c := range contacts {
		telToName[c.Tel] = c.Name
	}
	nameToTel = make(map[string]string)
	for _, c := range contacts {
		nameToTel[c.Name] = c.Tel
	}

	contactsWin, messageWinBorder, msgWin, inputBorderWin, inputWin := createMainWindows(stdscr)
	menu_items, contactMenu, contactsMenuWin := makeContactsMenu(contacts, contactsWin)
	globalInputWin = inputWin
	globalMsgWin = msgWin

	contactsMenuWin.Touch()
	contactMenu.Post()
	contactsWin.Refresh()
	messageWinBorder.Refresh()
	inputBorderWin.Refresh()
	msgWin.Refresh()
	msgWinSize_y, msgWinSize_x = msgWin.MaxYX()

	currentContact = getTel(contactMenu.Current(nil).Name())
	changeContact(contactsMenuWin, contactMenu)
	inputWin.Move(0, 0)
	go ts.StartListening()
	inputHandler(inputWin, stdscr, contactsMenuWin, contactMenu, msgWin)
	cleanup(menu_items, contactMenu)
}
Esempio n. 10
0
func main() {
	stdscr, err := gc.Init()
	if err != nil {
		return
	}
	defer gc.End()

	stdscr.Border(gc.ACS_VLINE, gc.ACS_VLINE, gc.ACS_HLINE, gc.ACS_HLINE,
		gc.ACS_ULCORNER, gc.ACS_URCORNER, gc.ACS_LLCORNER, gc.ACS_LRCORNER)
	stdscr.GetChar()
}
Esempio n. 11
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
		}
	}
}
Esempio n. 12
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
			}
		}
	}
}
Esempio n. 13
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()
		}
	}
}
Esempio n. 14
0
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()
}
Esempio n. 15
0
func GetXY() (int, int) {
	win, err := nc.Init()
	if err != nil {
		log.Fatal(err)
		os.Exit(1)
	}
	win.Clear()
	h, w := win.MaxYX()
	nc.End()
	fmt.Printf("screen: %d x %d\n", w, h)

	return w, h
}
Esempio n. 16
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()
}
Esempio n. 17
0
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()
}
Esempio n. 18
0
func main() {
	stdscr, err := gc.Init()
	if err != nil {
		log.Fatal(err)
	}
	defer gc.End()

	gc.StartColor()
	gc.Echo(false)
	gc.Raw(true)

	var colours = []int16{gc.C_BLACK, gc.C_BLUE, gc.C_CYAN, gc.C_GREEN,
		gc.C_MAGENTA, gc.C_RED, gc.C_WHITE, gc.C_YELLOW}
	var attributes = []struct {
		attr gc.Char
		text string
	}{
		{gc.A_NORMAL, "normal"},
		{gc.A_STANDOUT, "standout"},
		{gc.A_UNDERLINE | gc.A_BOLD, "underline"},
		{gc.A_REVERSE, "reverse"},
		{gc.A_BLINK, "blink"},
		{gc.A_DIM, "dim"},
		{gc.A_BOLD, "bold"},
	}
	stdscr.MovePrint(0, 0, "Normal terminal colors: ")
	for i, c := range colours {
		gc.InitPair(int16(i), c, c)
		stdscr.ColorOn(int16(i))
		stdscr.AddChar(' ')
		stdscr.ColorOff(int16(i))
	}
	stdscr.MovePrint(1, 0, "Bold terminal colors:   ")
	stdscr.AttrOn(gc.A_BLINK)
	for i, _ := range colours {
		stdscr.ColorOn(int16(i))
		stdscr.AddChar(' ')
		stdscr.ColorOff(int16(i))
	}
	stdscr.AttrOff(gc.A_BLINK)
	stdscr.Move(2, 0)
	for _, a := range attributes {
		stdscr.AttrOn(a.attr)
		stdscr.Println(a.text)
		stdscr.AttrOff(a.attr)
	}
	stdscr.GetChar()
}
Esempio n. 19
0
func main() {
	stdscr, _ := gc.Init()
	defer gc.End()

	row, col := stdscr.MaxYX()
	msg := "Just a string "
	stdscr.MovePrint(row/2, (col-len(msg))/2, msg)

	stdscr.MovePrintf(row-3, 0, "This screen has %d rows and %d columns. ",
		row, col)
	stdscr.Println()
	stdscr.Print("Try resizing your terminal window and then " +
		"run this program again.")
	stdscr.Refresh()
	stdscr.GetChar()
}
Esempio n. 20
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)
		}
	}
}
Esempio n. 21
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
		}
	}
}
Esempio n. 22
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()
}
Esempio n. 23
0
func main() {
	gc.SlkInit(gc.SLK_323)
	scr, err := gc.Init()
	if err != nil {
		log.Fatal("INIT:", err)
	}
	defer gc.End()

	gc.StartColor()
	gc.InitPair(SOFTKEYS, gc.C_YELLOW, gc.C_BLUE)

	scr.Print("Type any key to exit...")
	labels := [...]string{"ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
		"EIGHT"}
	for i := range labels {
		gc.SlkSet(i+1, labels[i], gc.SLK_CENTER)
	}
	gc.SlkColor(SOFTKEYS)
	gc.SlkNoutRefresh()
	scr.Refresh()
	scr.GetChar()
}
Esempio n. 24
0
//Returns an instance of the CLI. Call once
func GetCli() hncli {
	if !singleDone {
		hc = hncli{}

		root, e := goncurses.Init()

		if e != nil {
			goncurses.End()
			log.Fatal(e)
		}

		h, w := root.MaxYX()

		hc.root = root
		hc.main = root.Sub(h-MENU_HEIGHT, w, 0, 0)
		hc.help = root.Sub(MENU_HEIGHT, w, h-MENU_HEIGHT, 0)

		hc.Height = h - MENU_HEIGHT

		singleDone = true
	}

	return hc
}
Esempio n. 25
0
//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
}
Esempio n. 26
0
func (ui *NcurseUi) KillScreen() {
	goncurses.End()
}
Esempio n. 27
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])
		}
	}
}
Esempio n. 28
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()
}
Esempio n. 29
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])
		}
	}
}
Esempio n. 30
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)
}