8 "github.com/zyedidia/micro/v2/internal/buffer"
9 "github.com/zyedidia/micro/v2/internal/screen"
10 "github.com/zyedidia/terminal"
14 type CallbackFunc func(string)
17 TTClose = iota // Should be closed
18 TTRunning // Currently running a command
19 TTDone // Finished running a command
22 var CloseTerms chan bool
25 CloseTerms = make(chan bool)
28 // A Terminal holds information for the terminal emulator
29 type Terminal struct {
34 Selection [2]buffer.Loc
41 // HasSelection returns whether this terminal has a valid selection
42 func (t *Terminal) HasSelection() bool {
43 return t.Selection[0] != t.Selection[1]
46 func (t *Terminal) Name() string {
50 // GetSelection returns the selected text
51 func (t *Terminal) GetSelection(width int) string {
52 start := t.Selection[0]
54 if start.GreaterThan(end) {
55 start, end = end, start
59 for y := start.Y; y <= end.Y; y++ {
60 for x := 0; x < width; x++ {
62 if l.GreaterEqual(start) && l.LessThan(end) {
63 c, _, _ := t.State.Cell(x, y)
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 []interface{}), userargs []interface{}) error {
73 if len(execCmd) <= 0 {
77 cmd := exec.Command(execCmd[0], execCmd[1:]...)
80 t.output = bytes.NewBuffer([]byte{})
82 Term, _, err := terminal.Start(&t.State, cmd, t.output)
87 t.getOutput = getOutput
89 t.title = execCmd[0] + ":" + strconv.Itoa(cmd.Process.Pid)
91 t.callback = func(out string) {
92 callback(out, userargs)
99 Term.Write([]byte("Press enter to close"))
111 // Stop stops execution of the terminal and sets the Status
113 func (t *Terminal) Stop() {
114 t.Term.File().Close()
124 // Close sets the Status to TTClose indicating that the terminal
125 // is done and should be closed
126 func (t *Terminal) Close() {
128 // call the lua function that the user has given as a callback
130 if t.callback != nil {
132 Function: func(out string, args []interface{}) {
135 Output: t.output.String(),
142 // WriteString writes a given string to this terminal's pty
143 func (t *Terminal) WriteString(str string) {
144 t.Term.File().WriteString(str)