10 "github.com/zyedidia/micro/cmd/micro/buffer"
11 "github.com/zyedidia/micro/cmd/micro/screen"
12 "github.com/zyedidia/terminal"
18 TTIdle = iota // Waiting for a new command
19 TTRunning // Currently running a command
20 TTDone // Finished running a command
23 // A Terminal holds information for the terminal emulator
24 type Terminal struct {
29 Selection [2]buffer.Loc
36 // HasSelection returns whether this terminal has a valid selection
37 func (t *Terminal) HasSelection() bool {
38 return t.Selection[0] != t.Selection[1]
41 // GetSelection returns the selected text
42 func (t *Terminal) GetSelection(width int) string {
43 start := t.Selection[0]
45 if start.GreaterThan(end) {
46 start, end = end, start
50 for y := start.Y; y <= end.Y; y++ {
51 for x := 0; x < width; x++ {
53 if l.GreaterEqual(start) && l.LessThan(end) {
54 c, _, _ := t.State.Cell(x, y)
62 // Start begins a new command in this terminal with a given view
63 func (t *Terminal) Start(execCmd []string, getOutput bool) error {
64 if len(execCmd) <= 0 {
68 cmd := exec.Command(execCmd[0], execCmd[1:]...)
71 t.output = bytes.NewBuffer([]byte{})
73 Term, _, err := terminal.Start(&t.State, cmd, t.output)
78 t.getOutput = getOutput
80 t.title = execCmd[0] + ":" + strconv.Itoa(cmd.Process.Pid)
86 fmt.Fprintln(os.Stderr, "[Press enter to close]")
92 // closeterm <- view.Num
98 // Stop stops execution of the terminal and sets the Status
100 func (t *Terminal) Stop() {
101 t.Term.File().Close()
110 // Close sets the Status to TTIdle indicating that the terminal
111 // is ready for a new command to execute
112 func (t *Terminal) Close() {
114 // call the lua function that the user has given as a callback
116 // TODO: plugin callback on Term emulator
117 // _, err := Call(t.callback, t.output.String())
118 // if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
124 // WriteString writes a given string to this terminal's pty
125 func (t *Terminal) WriteString(str string) {
126 t.Term.File().WriteString(str)