aboutsummaryrefslogtreecommitdiff
path: root/editor/commands.go
blob: e3a77b99572262d9ae99d46a16fa47a0e443e552 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package editor

import (
	"fmt"
	"strings"
)

type CommandFunc func(args string) string

var poecmds map[string]CommandFunc

func (e *editor) initCommands() {
	poecmds = map[string]CommandFunc{
		//		"Exit":   CmdExit,
		//		"New":    CmdNew,
		//		"Del":    CmdDel,
		"Edit": e.CmdEdit,
		//		"Newcol": CmdNewcol,
	}
}

func (e *editor) Run(input string) {
	if input == "" {
		return
	}

	input = strings.Trim(input, "\t\n ")

	// check poe default commands
	cmd := strings.Split(string(input), " ")
	if fn, ok := poecmds[cmd[0]]; ok {
		fn(strings.TrimPrefix(input, cmd[0]))
		return
	}

	// Edit shortcuts for external commands and piping
	switch input[0] {
	case '!', '<', '>', '|':
		e.CmdEdit(input)
	}

	e.CmdEdit("!" + input)
}

//func CmdExit(args string) {
//	ok := true
//	for _, win := range AllWindows() {
//		if !win.CanClose() {
//			ok = false
//		}
//	}
//	if ok {
//		quit <- true
//	}
//}
//
//func CmdNewcol(args string) {
//	screen.Clear()
//	screen.Sync()
//	workspace.AddCol()
//	CmdNew("")
//}
//
//
//

func (e *editor) CmdEdit(args string) string {
	if len(args) < 2 {
		return ""
	}

	switch args[0] {
	case 'f':
		var names []string
		for _, buf := range e.buffers {
			names = append(names, fmt.Sprintf("%s", buf.Name()))
		}
		return fmt.Sprintf("buffers:\n%s\n", strings.Join(names, "\n"))
		//	case '!':
		//		os.Chdir(CurWin.Dir())
		//		cmd := strings.Split(string(args[1:]), " ")
		//		path, err := exec.LookPath(cmd[0])
		//		if err != nil { // path not found, break with silence
		//			//printMsg("path not found: %s\n", cmd[0])
		//			break
		//		}
		//		out, err := exec.Command(path, cmd[1:]...).Output()
		//		if err != nil {
		//			printMsg("error: %s\n", err)
		//			break
		//		}
		//		// if command produced output, print it
		//		outstr := string(out)
		//		if outstr != "" {
		//			printMsg("%s", outstr)
		//		}
	default:
		return "?"
	}
}