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