| 1 |
package shell |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"os/exec" |
| 6 |
"strconv" |
| 7 |
|
| 8 |
"github.com/micro-editor/terminal" |
| 9 |
"github.com/zyedidia/micro/v2/internal/buffer" |
| 10 |
"github.com/zyedidia/micro/v2/internal/screen" |
| 11 |
) |
| 12 |
|
| 13 |
type TermType int |
| 14 |
type CallbackFunc func(string) |
| 15 |
|
| 16 |
const ( |
| 17 |
TTClose = iota // Should be closed |
| 18 |
TTRunning // Currently running a command |
| 19 |
TTDone // Finished running a command |
| 20 |
) |
| 21 |
|
| 22 |
var CloseTerms chan bool |
| 23 |
|
| 24 |
func init() { |
| 25 |
CloseTerms = make(chan bool) |
| 26 |
} |
| 27 |
|
| 28 |
// A Terminal holds information for the terminal emulator |
| 29 |
type Terminal struct { |
| 30 |
State terminal.State |
| 31 |
Term *terminal.VT |
| 32 |
title string |
| 33 |
Status TermType |
| 34 |
Selection [2]buffer.Loc |
| 35 |
wait bool |
| 36 |
getOutput bool |
| 37 |
output *bytes.Buffer |
| 38 |
callback CallbackFunc |
| 39 |
} |
| 40 |
|
| 41 |
// HasSelection returns whether this terminal has a valid selection |
| 42 |
func (t *Terminal) HasSelection() bool { |
| 43 |
return t.Selection[0] != t.Selection[1] |
| 44 |
} |
| 45 |
|
| 46 |
func (t *Terminal) Name() string { |
| 47 |
return t.title |
| 48 |
} |
| 49 |
|
| 50 |
// GetSelection returns the selected text |
| 51 |
func (t *Terminal) GetSelection(width int) string { |
| 52 |
start := t.Selection[0] |
| 53 |
end := t.Selection[1] |
| 54 |
if start.GreaterThan(end) { |
| 55 |
start, end = end, start |
| 56 |
} |
| 57 |
var ret string |
| 58 |
var l buffer.Loc |
| 59 |
for y := start.Y; y <= end.Y; y++ { |
| 60 |
for x := 0; x < width; x++ { |
| 61 |
l.X, l.Y = x, y |
| 62 |
if l.GreaterEqual(start) && l.LessThan(end) { |
| 63 |
c, _, _ := t.State.Cell(x, y) |
| 64 |
ret += string(c) |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
return ret |
| 69 |
} |
| 70 |
|
| 71 |
// Start begins a new command in this terminal with a given view |
| 72 |
func (t *Terminal) Start(execCmd []string, getOutput bool, wait bool, callback func(out string, userargs []any), userargs []any) error { |
| 73 |
if len(execCmd) <= 0 { |
| 74 |
return nil |
| 75 |
} |
| 76 |
|
| 77 |
cmd := exec.Command(execCmd[0], execCmd[1:]...) |
| 78 |
t.output = nil |
| 79 |
if getOutput { |
| 80 |
t.output = bytes.NewBuffer([]byte{}) |
| 81 |
cmd.Stdout = t.output |
| 82 |
} |
| 83 |
Term, _, err := terminal.Start(&t.State, cmd) |
| 84 |
if err != nil { |
| 85 |
return err |
| 86 |
} |
| 87 |
t.Term = Term |
| 88 |
t.getOutput = getOutput |
| 89 |
t.Status = TTRunning |
| 90 |
t.title = execCmd[0] + ":" + strconv.Itoa(cmd.Process.Pid) |
| 91 |
t.wait = wait |
| 92 |
t.callback = func(out string) { |
| 93 |
callback(out, userargs) |
| 94 |
} |
| 95 |
|
| 96 |
go func() { |
| 97 |
for { |
| 98 |
err := Term.Parse() |
| 99 |
if err != nil { |
| 100 |
Term.Write([]byte("Press enter to close")) |
| 101 |
screen.Redraw() |
| 102 |
break |
| 103 |
} |
| 104 |
screen.Redraw() |
| 105 |
} |
| 106 |
t.Stop() |
| 107 |
}() |
| 108 |
|
| 109 |
return nil |
| 110 |
} |
| 111 |
|
| 112 |
// Stop stops execution of the terminal and sets the Status |
| 113 |
// to TTDone |
| 114 |
func (t *Terminal) Stop() { |
| 115 |
t.Term.File().Close() |
| 116 |
t.Term.Close() |
| 117 |
if t.wait { |
| 118 |
t.Status = TTDone |
| 119 |
} else { |
| 120 |
t.Close() |
| 121 |
CloseTerms <- true |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// Close sets the Status to TTClose indicating that the terminal |
| 126 |
// is done and should be closed |
| 127 |
func (t *Terminal) Close() { |
| 128 |
t.Status = TTClose |
| 129 |
// call the lua function that the user has given as a callback |
| 130 |
if t.getOutput { |
| 131 |
if t.callback != nil { |
| 132 |
Jobs <- JobFunction{ |
| 133 |
Function: func(out string, args []any) { |
| 134 |
t.callback(out) |
| 135 |
}, |
| 136 |
Output: t.output.String(), |
| 137 |
Args: nil, |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// WriteString writes a given string to this terminal's pty |
| 144 |
func (t *Terminal) WriteString(str string) { |
| 145 |
t.Term.File().WriteString(str) |
| 146 |
} |