9 "github.com/zyedidia/tcell"
10 "github.com/zyedidia/terminal"
14 VTIdle = iota // Waiting for a new command
15 VTRunning // Currently running a command
16 VTDone // Finished running a command
19 // A Terminal holds information for the terminal emulator
20 type Terminal struct {
28 // HasSelection returns whether this terminal has a valid selection
29 func (t *Terminal) HasSelection() bool {
30 return t.selection[0] != t.selection[1]
33 // GetSelection returns the selected text
34 func (t *Terminal) GetSelection(width int) string {
35 start := t.selection[0]
37 if start.GreaterThan(end) {
38 start, end = end, start
42 for y := start.Y; y <= end.Y; y++ {
43 for x := 0; x < width; x++ {
45 if l.GreaterEqual(start) && l.LessThan(end) {
46 c, _, _ := t.state.Cell(x, y)
54 // Start begins a new command in this terminal with a given view
55 func (t *Terminal) Start(execCmd []string, view *View) error {
56 if len(execCmd) <= 0 {
60 cmd := exec.Command(execCmd[0], execCmd[1:]...)
61 term, _, err := terminal.Start(&t.state, cmd)
67 t.title = execCmd[0] + ":" + strconv.Itoa(cmd.Process.Pid)
73 fmt.Fprintln(os.Stderr, "[Press enter to close]")
84 // Resize informs the terminal of a resize event
85 func (t *Terminal) Resize(width, height int) {
86 t.term.Resize(width, height)
89 // Stop stops execution of the terminal and sets the status
91 func (t *Terminal) Stop() {
97 // Close sets the status to VTIdle indicating that the terminal
98 // is ready for a new command to execute
99 func (t *Terminal) Close() {
103 // WriteString writes a given string to this terminal's pty
104 func (t *Terminal) WriteString(str string) {
105 t.term.File().WriteString(str)
108 // Display displays this terminal in a view
109 func (t *Terminal) Display(v *View) {
113 dividerStyle := defStyle
114 if style, ok := colorscheme["divider"]; ok {
117 for i := 0; i < v.Height; i++ {
118 screen.SetContent(v.x, v.y+i, '|', nil, dividerStyle.Reverse(true))
122 defer t.state.Unlock()
125 for y := 0; y < v.Height; y++ {
126 for x := 0; x < v.Width; x++ {
128 c, f, b := t.state.Cell(x, y)
130 fg, bg := int(f), int(b)
131 if f == terminal.DefaultFG {
132 fg = int(tcell.ColorDefault)
134 if b == terminal.DefaultBG {
135 bg = int(tcell.ColorDefault)
137 st := tcell.StyleDefault.Foreground(GetColor256(int(fg))).Background(GetColor256(int(bg)))
139 if l.LessThan(t.selection[1]) && l.GreaterEqual(t.selection[0]) || l.LessThan(t.selection[0]) && l.GreaterEqual(t.selection[1]) {
140 st = st.Reverse(true)
143 screen.SetContent(v.x+x+divider, v.y+y, c, nil, st)
146 if t.state.CursorVisible() && tabs[curTab].CurView == v.Num {
147 curx, cury := t.state.Cursor()
148 screen.ShowCursor(curx+v.x+divider, cury+v.y)