aboutsummaryrefslogtreecommitdiff
path: root/ui/cli.go
blob: a358e58643594c77f0db0fca198b7cd5aa94bd62 (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
56
57
58
59
60
61
62
63
64
65
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

	if c.ed.Len() == 0 {
		c.ed.NewBuffer()
	}

	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': // list files
			ids, bs := c.ed.Buffers()
			fmt.Printf("%v\n%v\n", ids, bs)
		case 'e': // edit/load file name
			if len(input) <= 1 {
				c.ed.NewBuffer()
			}
			c.ed.LoadBuffers(input[1:])
		case 'b': // select active buffer
			if len(input) > 1 {
				id, _ := strconv.ParseInt(input[1], 10, 64)
				c.ed.Buffer(id)
				break
			}
			c.ed.NewBuffer()
		case 'a': // append
			if len(input) < 2 {
				break
			}
			//c.ed.Current().Write([]byte(strings.Join(input[1:], " ")))
		default:
			fmt.Println("?")
		}
	}
}