aboutsummaryrefslogtreecommitdiff
path: root/ui/cli.go
blob: a7cf9e425723208ea678c2ebe17948dc5eac1446 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package ui

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"

	"github.com/prodhe/poe/editor"
)

// Cli implements ui.Interface with simple command-line driven user actions.
type Cli struct {
	ed editor.Editor
}

func (c *Cli) Init(e editor.Editor) error {
	c.ed = e
	return nil
}

func (c *Cli) Close() {
}

func (c *Cli) Listen() {
	scanner := bufio.NewScanner(os.Stdin)
	var input []string
outer:
	for {
		scanner.Scan()
		input = strings.Split(scanner.Text(), " ")
		switch input[0][0] { // first rune in input
		case 'q': // quit
			break outer
		case 'f':
			ids, bs := c.ed.Buffers()
			fmt.Printf("%v\n%v\n", ids, bs)
		case 'b':
			if len(input) > 1 {
				id, _ := strconv.ParseInt(input[1], 10, 64)
				c.ed.Buffer(id)
				break
			}
			c.ed.NewBuffer()
		case 'a':
			if len(input) < 2 {
				break
			}
			c.ed.Current().Write([]byte(strings.Join(input[1:], " ")))
		default:
			fmt.Println("?")
		}
	}
}