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