turbo-editors/turbo-corepublic Fork 0
v1.0.2
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

view_events.go · 91 lines · 2.5 KBGo Blame HistoryRaw
🛟 Updated. 28d5985 k33g 11h ago1package terminal
2
3import "github.com/gdamore/tcell/v2"
4
5// HandleKey sends a key press to the shell.
6//
7// Shift with Page Up or Page Down reads back through the history instead,
8// which is where every terminal puts that. Everything else goes to the shell,
9// and brings the view back to the live screen on the way — a terminal that
10// stayed in its history while you typed would hide your own command.
11//
12// Once the program has gone, only the scrolling keys are still taken. A dead
13// terminal that went on swallowing keys could never be closed with Ctrl-W: the
14// write would fail silently and the key would be consumed anyway, leaving the
15// mouse as the only way out of a window whose command has finished.
16func (v *View) HandleKey(ev *tcell.EventKey) bool {
17 if !v.Focused() {
18 return false
19 }
20 if v.handleScrollKey(ev) {
21 return true
22 }
23 if v.Exited() {
24 return false
25 }
26
27 sequence := Encode(ev, v.applicationCursor())
28 if len(sequence) == 0 {
29 return false
30 }
31
32 v.ScrollToBottom()
33 // A write that fails means the shell has gone, which the reading goroutine
34 // is about to report; there is nothing useful to do with the error here.
35 _, _ = v.session.Write(sequence)
36 return true
37}
38
39// handleScrollKey deals with the keys that read back through the history.
40func (v *View) handleScrollKey(ev *tcell.EventKey) bool {
41 if ev.Modifiers()&tcell.ModShift == 0 {
42 return false
43 }
44
45 page := max(v.Bounds().H-1, 1)
46 switch ev.Key() {
47 case tcell.KeyPgUp:
48 v.ScrollBy(page)
49 case tcell.KeyPgDn:
50 v.ScrollBy(-page)
51 default:
52 return false
53 }
54 return true
55}
56
57// applicationCursor reports the mode the program has asked for, under the lock.
58func (v *View) applicationCursor() bool {
59 v.mu.Lock()
60 defer v.mu.Unlock()
61 return v.parser.Screen().ApplicationCursor()
62}
63
64// HandleMouse scrolls back through the history with the wheel.
65//
66// Clicks are not forwarded: mouse reporting is a mode this emulator does not
67// implement, so a program has no reason to expect them.
68func (v *View) HandleMouse(ev *tcell.EventMouse) bool {
69 if !v.hitTest(ev) {
70 return false
71 }
72
73 switch ev.Buttons() {
74 case tcell.WheelUp:
75 v.ScrollBy(wheelStep)
76 case tcell.WheelDown:
77 v.ScrollBy(-wheelStep)
78 case tcell.Button1:
79 // A click inside the window is claimed so that it counts as focusing
80 // the window rather than falling through to the desktop.
81 default:
82 return false
83 }
84 return true
85}
86
87// hitTest reports whether a mouse event landed on the view.
88func (v *View) hitTest(ev *tcell.EventMouse) bool {
89 x, y := ev.Position()
90 return v.Bounds().Contains(x, y)
91}