]> git.lizzy.rs Git - micro.git/blob - internal/action/termpane.go
Merge
[micro.git] / internal / action / termpane.go
1 package action
2
3 import (
4         "errors"
5         "runtime"
6
7         "github.com/zyedidia/micro/v2/internal/clipboard"
8         "github.com/zyedidia/micro/v2/internal/display"
9         "github.com/zyedidia/micro/v2/internal/screen"
10         "github.com/zyedidia/micro/v2/internal/shell"
11         "github.com/zyedidia/tcell"
12         "github.com/zyedidia/terminal"
13 )
14
15 type TermPane struct {
16         *shell.Terminal
17         display.Window
18
19         mouseReleased bool
20         id            uint64
21         tab           *Tab
22 }
23
24 func NewTermPane(x, y, w, h int, t *shell.Terminal, id uint64, tab *Tab) (*TermPane, error) {
25         if !TermEmuSupported {
26                 return nil, errors.New("Terminal emulator is not supported on this system")
27         }
28
29         th := new(TermPane)
30         th.Terminal = t
31         th.id = id
32         th.mouseReleased = true
33         th.Window = display.NewTermWindow(x, y, w, h, t)
34         th.tab = tab
35         return th, nil
36 }
37
38 func (t *TermPane) ID() uint64 {
39         return t.id
40 }
41
42 func (t *TermPane) SetID(i uint64) {
43         t.id = i
44 }
45
46 func (t *TermPane) SetTab(tab *Tab) {
47         t.tab = tab
48 }
49
50 func (t *TermPane) Tab() *Tab {
51         return t.tab
52 }
53
54 func (t *TermPane) Close() {}
55
56 func (t *TermPane) Quit() {
57         t.Close()
58         if len(MainTab().Panes) > 1 {
59                 t.Unsplit()
60         } else if len(Tabs.List) > 1 {
61                 Tabs.RemoveTab(t.id)
62         } else {
63                 screen.Screen.Fini()
64                 InfoBar.Close()
65                 runtime.Goexit()
66         }
67 }
68
69 func (t *TermPane) Unsplit() {
70         n := MainTab().GetNode(t.id)
71         n.Unsplit()
72
73         MainTab().RemovePane(MainTab().GetPane(t.id))
74         MainTab().Resize()
75         MainTab().SetActive(len(MainTab().Panes) - 1)
76 }
77
78 // HandleEvent handles a tcell event by forwarding it to the terminal emulator
79 // If the event is a mouse event and the program running in the emulator
80 // does not have mouse support, the emulator will support selections and
81 // copy-paste
82 func (t *TermPane) HandleEvent(event tcell.Event) {
83         if e, ok := event.(*tcell.EventKey); ok {
84                 if t.Status == shell.TTDone {
85                         switch e.Key() {
86                         case tcell.KeyEscape, tcell.KeyCtrlQ, tcell.KeyEnter:
87                                 t.Close()
88                                 t.Quit()
89                         default:
90                         }
91                 }
92                 if e.Key() == tcell.KeyCtrlC && t.HasSelection() {
93                         clipboard.Write(t.GetSelection(t.GetView().Width), clipboard.ClipboardReg)
94                         InfoBar.Message("Copied selection to clipboard")
95                 } else if t.Status != shell.TTDone {
96                         t.WriteString(event.EscSeq())
97                 }
98         } else if _, ok := event.(*tcell.EventPaste); ok {
99                 if t.Status != shell.TTDone {
100                         t.WriteString(event.EscSeq())
101                 }
102         } else if e, ok := event.(*tcell.EventMouse); e != nil && (!ok || t.State.Mode(terminal.ModeMouseMask)) {
103                 // t.WriteString(event.EscSeq())
104         } else if e != nil {
105                 x, y := e.Position()
106                 v := t.GetView()
107                 x -= v.X
108                 y -= v.Y
109
110                 if e.Buttons() == tcell.Button1 {
111                         if !t.mouseReleased {
112                                 // drag
113                                 t.Selection[1].X = x
114                                 t.Selection[1].Y = y
115                         } else {
116                                 t.Selection[0].X = x
117                                 t.Selection[0].Y = y
118                                 t.Selection[1].X = x
119                                 t.Selection[1].Y = y
120                         }
121
122                         t.mouseReleased = false
123                 } else if e.Buttons() == tcell.ButtonNone {
124                         if !t.mouseReleased {
125                                 t.Selection[1].X = x
126                                 t.Selection[1].Y = y
127                         }
128                         t.mouseReleased = true
129                 }
130         }
131
132         if t.Status == shell.TTClose {
133                 t.Quit()
134         }
135 }
136
137 func (t *TermPane) HandleCommand(input string) {
138         InfoBar.Error("Commands are unsupported in term for now")
139 }