micro/internal/action/termpane.go

121 lines
2.6 KiB
Go
Raw Normal View History

2019-01-11 05:26:58 +03:00
package action
import (
2019-12-22 21:43:29 +03:00
"runtime"
2019-01-11 22:49:22 +03:00
2019-01-11 05:26:58 +03:00
"github.com/zyedidia/clipboard"
2019-02-04 07:17:24 +03:00
"github.com/zyedidia/micro/internal/display"
"github.com/zyedidia/micro/internal/screen"
"github.com/zyedidia/micro/internal/shell"
2020-01-01 05:50:26 +03:00
"github.com/zyedidia/tcell"
2019-01-11 05:26:58 +03:00
"github.com/zyedidia/terminal"
)
2019-01-19 23:37:59 +03:00
type TermPane struct {
2019-01-11 05:26:58 +03:00
*shell.Terminal
display.Window
mouseReleased bool
2019-01-11 22:49:22 +03:00
id uint64
}
2019-01-19 23:37:59 +03:00
func NewTermPane(x, y, w, h int, t *shell.Terminal, id uint64) *TermPane {
th := new(TermPane)
2019-01-11 22:49:22 +03:00
th.Terminal = t
th.id = id
2019-01-15 08:24:53 +03:00
th.mouseReleased = true
2019-01-11 22:49:22 +03:00
th.Window = display.NewTermWindow(x, y, w, h, t)
return th
}
2019-01-19 23:37:59 +03:00
func (t *TermPane) ID() uint64 {
2019-01-11 22:49:22 +03:00
return t.id
}
2019-01-19 23:37:59 +03:00
func (t *TermPane) SetID(i uint64) {
t.id = i
}
func (t *TermPane) Close() {}
2019-01-14 05:06:58 +03:00
2019-01-19 23:37:59 +03:00
func (t *TermPane) Quit() {
2019-01-14 05:06:58 +03:00
t.Close()
2019-01-11 22:49:22 +03:00
if len(MainTab().Panes) > 1 {
t.Unsplit()
} else if len(Tabs.List) > 1 {
Tabs.RemoveTab(t.id)
} else {
screen.Screen.Fini()
InfoBar.Close()
2019-12-22 21:43:29 +03:00
runtime.Goexit()
2019-01-11 22:49:22 +03:00
}
}
2019-01-19 23:37:59 +03:00
func (t *TermPane) Unsplit() {
2019-01-11 22:49:22 +03:00
n := MainTab().GetNode(t.id)
n.Unsplit()
MainTab().RemovePane(MainTab().GetPane(t.id))
MainTab().Resize()
MainTab().SetActive(len(MainTab().Panes) - 1)
2019-01-11 05:26:58 +03:00
}
// HandleEvent handles a tcell event by forwarding it to the terminal emulator
// If the event is a mouse event and the program running in the emulator
// does not have mouse support, the emulator will support selections and
// copy-paste
2019-01-19 23:37:59 +03:00
func (t *TermPane) HandleEvent(event tcell.Event) {
2019-01-11 05:26:58 +03:00
if e, ok := event.(*tcell.EventKey); ok {
if t.Status == shell.TTDone {
switch e.Key() {
case tcell.KeyEscape, tcell.KeyCtrlQ, tcell.KeyEnter:
t.Close()
2019-01-11 22:49:22 +03:00
t.Quit()
2019-01-11 05:26:58 +03:00
default:
}
}
if e.Key() == tcell.KeyCtrlC && t.HasSelection() {
clipboard.WriteAll(t.GetSelection(t.GetView().Width), "clipboard")
InfoBar.Message("Copied selection to clipboard")
} else if t.Status != shell.TTDone {
2020-01-01 05:50:26 +03:00
t.WriteString(event.EscSeq())
2019-01-11 05:26:58 +03:00
}
2019-08-26 21:47:27 +03:00
} else if e, ok := event.(*tcell.EventMouse); e != nil && (!ok || t.State.Mode(terminal.ModeMouseMask)) {
// t.WriteString(event.EscSeq())
2019-08-26 21:47:27 +03:00
} else if e != nil {
2019-01-11 05:26:58 +03:00
x, y := e.Position()
v := t.GetView()
x -= v.X
2019-01-15 08:24:53 +03:00
y -= v.Y
2019-01-11 05:26:58 +03:00
if e.Buttons() == tcell.Button1 {
if !t.mouseReleased {
// drag
t.Selection[1].X = x
t.Selection[1].Y = y
} else {
t.Selection[0].X = x
t.Selection[0].Y = y
t.Selection[1].X = x
t.Selection[1].Y = y
}
t.mouseReleased = false
} else if e.Buttons() == tcell.ButtonNone {
if !t.mouseReleased {
t.Selection[1].X = x
t.Selection[1].Y = y
}
t.mouseReleased = true
}
}
2019-08-26 21:47:27 +03:00
if t.Status == shell.TTClose {
t.Quit()
}
2019-01-11 05:26:58 +03:00
}
2019-01-11 22:49:22 +03:00
2019-01-19 23:37:59 +03:00
func (t *TermPane) HandleCommand(input string) {
2019-01-11 22:49:22 +03:00
InfoBar.Error("Commands are unsupported in term for now")
}