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