]> git.lizzy.rs Git - micro.git/blob - cmd/micro/action/termhandler.go
Add keymenu
[micro.git] / cmd / micro / action / termhandler.go
1 package action
2
3 import (
4         "os"
5
6         "github.com/zyedidia/clipboard"
7         "github.com/zyedidia/micro/cmd/micro/display"
8         "github.com/zyedidia/micro/cmd/micro/screen"
9         "github.com/zyedidia/micro/cmd/micro/shell"
10         "github.com/zyedidia/tcell"
11         "github.com/zyedidia/terminal"
12 )
13
14 type TermHandler struct {
15         *shell.Terminal
16         display.Window
17
18         mouseReleased bool
19         id            uint64
20 }
21
22 func NewTermHandler(x, y, w, h int, t *shell.Terminal, id uint64) *TermHandler {
23         th := new(TermHandler)
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 *TermHandler) ID() uint64 {
32         return t.id
33 }
34
35 func (t *TermHandler) Close() {}
36
37 func (t *TermHandler) Quit() {
38         t.Close()
39         if len(MainTab().Panes) > 1 {
40                 t.Unsplit()
41         } else if len(Tabs.List) > 1 {
42                 Tabs.RemoveTab(t.id)
43         } else {
44                 screen.Screen.Fini()
45                 InfoBar.Close()
46                 os.Exit(0)
47         }
48 }
49
50 func (t *TermHandler) Unsplit() {
51         n := MainTab().GetNode(t.id)
52         n.Unsplit()
53
54         MainTab().RemovePane(MainTab().GetPane(t.id))
55         MainTab().Resize()
56         MainTab().SetActive(len(MainTab().Panes) - 1)
57 }
58
59 // HandleEvent handles a tcell event by forwarding it to the terminal emulator
60 // If the event is a mouse event and the program running in the emulator
61 // does not have mouse support, the emulator will support selections and
62 // copy-paste
63 func (t *TermHandler) HandleEvent(event tcell.Event) {
64         if e, ok := event.(*tcell.EventKey); ok {
65                 if t.Status == shell.TTDone {
66                         switch e.Key() {
67                         case tcell.KeyEscape, tcell.KeyCtrlQ, tcell.KeyEnter:
68                                 t.Close()
69                                 t.Quit()
70                         default:
71                         }
72                 }
73                 if e.Key() == tcell.KeyCtrlC && t.HasSelection() {
74                         clipboard.WriteAll(t.GetSelection(t.GetView().Width), "clipboard")
75                         InfoBar.Message("Copied selection to clipboard")
76                 } else if t.Status != shell.TTDone {
77                         t.WriteString(event.EscSeq())
78                 }
79         } else if e, ok := event.(*tcell.EventMouse); !ok || t.State.Mode(terminal.ModeMouseMask) {
80                 t.WriteString(event.EscSeq())
81         } else {
82                 x, y := e.Position()
83                 v := t.GetView()
84                 x -= v.X
85                 y -= v.Y
86
87                 if e.Buttons() == tcell.Button1 {
88                         if !t.mouseReleased {
89                                 // drag
90                                 t.Selection[1].X = x
91                                 t.Selection[1].Y = y
92                         } else {
93                                 t.Selection[0].X = x
94                                 t.Selection[0].Y = y
95                                 t.Selection[1].X = x
96                                 t.Selection[1].Y = y
97                         }
98
99                         t.mouseReleased = false
100                 } else if e.Buttons() == tcell.ButtonNone {
101                         if !t.mouseReleased {
102                                 t.Selection[1].X = x
103                                 t.Selection[1].Y = y
104                         }
105                         t.mouseReleased = true
106                 }
107         }
108 }
109
110 func (t *TermHandler) HandleCommand(input string) {
111         InfoBar.Error("Commands are unsupported in term for now")
112 }