diff options
author | Petter Rodhelind <petter.rodhelind@gmail.com> | 2021-03-03 15:21:17 +0100 |
---|---|---|
committer | Petter Rodhelind <petter.rodhelind@gmail.com> | 2021-03-03 15:21:17 +0100 |
commit | 80571e64fa78ad90a563987cffc856b897e6bd8b (patch) | |
tree | 92e62ee0c8d0d3f452a48e021f7144db44bf48a3 /ui/tcell | |
parent | 89eb5668d877d61aeb8a8f559a5c110e54089644 (diff) | |
download | poe-80571e64fa78ad90a563987cffc856b897e6bd8b.tar.gz poe-80571e64fa78ad90a563987cffc856b897e6bd8b.tar.bz2 poe-80571e64fa78ad90a563987cffc856b897e6bd8b.zip |
Refactor mouse ButtonSecondary and ButtonMiddle.
Diffstat (limited to 'ui/tcell')
-rw-r--r-- | ui/tcell/view.go | 181 |
1 files changed, 64 insertions, 117 deletions
diff --git a/ui/tcell/view.go b/ui/tcell/view.go index b04b96a..ecd5aa4 100644 --- a/ui/tcell/view.go +++ b/ui/tcell/view.go @@ -2,6 +2,7 @@ package uitcell import ( "io" + "os" "path/filepath" "strings" "time" @@ -392,54 +393,13 @@ func (v *View) HandleEvent(ev tcell.Event) { v.mpressed = true v.mclickpos = pos - if ev.Modifiers()&tcell.ModAlt != 0 { // identic code to Btn2 - pos := v.XYToOffset(mx, my) - // if we clicked inside a current selection, run that one - q0, q1 := v.text.Dot() - if pos >= q0 && pos <= q1 && q0 != q1 { - output := Cmd(v.text.ReadDot()) - if output != "" { - printMsg(output) - } - return - } - - // otherwise, select non-space chars under mouse and run that - p := pos - v.text.PrevSpace(pos) - n := pos + v.text.NextSpace(pos) - v.text.SetDot(p, n) - str := strings.Trim(v.text.ReadDot(), "\n\t ") - v.text.SetDot(q0, q1) - output := Cmd(str) - if output != "" { - printMsg(output) - } + if ev.Modifiers()&tcell.ModAlt != 0 { // identic to ButtonMiddle + ButtonMiddle(v, mx, my) return } - if ev.Modifiers()&tcell.ModShift != 0 { // identic code to Btn3 - pos := v.XYToOffset(mx, my) - // if we clicked inside a current selection, open that one - q0, q1 := v.text.Dot() - if pos >= q0 && pos <= q1 && q0 != q1 { - CmdOpen(v.text.ReadDot()) - return - } - - // otherwise, select everything inside surround spaces and open that - p := pos - v.text.PrevSpace(pos) - n := pos + v.text.NextSpace(pos) - v.text.SetDot(p, n) - fn := strings.Trim(v.text.ReadDot(), "\n\t ") - v.text.SetDot(q0, q1) - if fn == "" { // if it is still blank, abort - return - } - if fn != "" && fn[0] != filepath.Separator { - fn = CurWin.Dir() + string(filepath.Separator) + fn - fn = filepath.Clean(fn) - } - CmdOpen(fn) + if ev.Modifiers()&tcell.ModShift != 0 { // identic to ButtonSecondary + ButtonSecondary(v, mx, my) return } @@ -459,45 +419,10 @@ func (v *View) HandleEvent(ev tcell.Event) { case tcell.WheelDown: // scrolldown v.Scroll(1) case tcell.ButtonMiddle: // middle click - pos := v.XYToOffset(mx, my) - // if we clicked inside a current selection, run that one - q0, q1 := v.text.Dot() - if pos >= q0 && pos <= q1 && q0 != q1 { - Cmd(v.text.ReadDot()) - return - } - - // otherwise, select non-space chars under mouse and run that - p := pos - v.text.PrevSpace(pos) - n := pos + v.text.NextSpace(pos) - v.text.SetDot(p, n) - fn := strings.Trim(v.text.ReadDot(), "\n\t ") - v.text.SetDot(q0, q1) - Cmd(fn) + ButtonMiddle(v, mx, my) return case tcell.ButtonSecondary: // right click - pos := v.XYToOffset(mx, my) - // if we clicked inside a current selection, open that one - q0, q1 := v.text.Dot() - if pos >= q0 && pos <= q1 && q0 != q1 { - CmdOpen(v.text.ReadDot()) - return - } - - // otherwise, select everything inside surround spaces and open that - p := pos - v.text.PrevSpace(pos) - n := pos + v.text.NextSpace(pos) - v.text.SetDot(p, n) - fn := strings.Trim(v.text.ReadDot(), "\n\t ") - v.text.SetDot(q0, q1) - if fn == "" { // if it is still blank, abort - return - } - if fn != "" && fn[0] != filepath.Separator { - fn = CurWin.Dir() + string(filepath.Separator) + fn - fn = filepath.Clean(fn) - } - CmdOpen(fn) + ButtonSecondary(v, mx, my) return default: printMsg("%#v", btn) @@ -587,44 +512,9 @@ func (v *View) HandleEvent(ev tcell.Event) { ed.WorkDir(), CurWin.Dir(), CurWin.Name(), CurWin.w, CurWin.h, sh, sw) return - case tcell.KeyCtrlO: // open file/dir - fn := v.text.ReadDot() - if fn == "" { // select all non-space characters - curpos := v.Cursor() - p := curpos - v.text.PrevSpace(curpos) - n := curpos + v.text.NextSpace(curpos) - v.text.SetDot(p, n) - fn = strings.Trim(v.text.ReadDot(), "\n\t ") - v.SetCursor(curpos, io.SeekStart) - if fn == "" { // if it is still blank, abort - return - } - } - if fn != "" && fn[0] != filepath.Separator { - fn = CurWin.Dir() + string(filepath.Separator) + fn - fn = filepath.Clean(fn) - } - CmdOpen(fn) - return case tcell.KeyCtrlN: // new column CmdNewcol() return - case tcell.KeyCtrlR: // run command in dot - cmd := v.text.ReadDot() - if cmd == "" { // select all non-space characters - curpos := v.Cursor() - p := curpos - v.text.PrevSpace(curpos) - n := curpos + v.text.NextSpace(curpos) - v.text.SetDot(p, n) - cmd = strings.Trim(v.text.ReadDot(), "\n\t ") - v.SetCursor(curpos, io.SeekStart) - if cmd == "" { // if it is still blank, abort - return - } - } - res := Cmd(cmd) - printMsg("%s\n", res) - return case tcell.KeyCtrlC: // copy to clipboard str := v.text.ReadDot() if str == "" { @@ -665,3 +555,60 @@ func RuneWidth(r rune) int { } return rw } + +func ButtonSecondary(v *View, mx, my int) { + pos := v.XYToOffset(mx, my) + // if we clicked inside a current selection, open that one + q0, q1 := v.text.Dot() + if pos < q0 || pos > q1 || q0 == q1 { + // otherwise, select everything inside surround spaces and open that + p := pos - v.text.PrevSpace(pos) + n := pos + v.text.NextSpace(pos) + v.text.SetDot(p, n) + } + + // read our (changed) dot and then reset it to whatever the user had (or had not) selected + fn := strings.Trim(v.text.ReadDot(), "\n\t ") + v.text.SetDot(q0, q1) + + if fn == "" { // if it is still blank, abort + return + } + if fn != "" && fn[0] != filepath.Separator { + fn = CurWin.Dir() + string(filepath.Separator) + fn + fn = filepath.Clean(fn) + } + + _, err := os.Stat(fn) + if err != nil { + // if the file exists, print why we could not open it + // otherwise just close silently + if os.IsExist(err) { + printMsg("%s\n", err) + return + } + return + } + + CmdOpen(fn) + return +} + +func ButtonMiddle(v *View, mx, my int) { + pos := v.XYToOffset(mx, my) + // if we clicked inside a current selection, run that one + q0, q1 := v.text.Dot() + if pos >= q0 && pos <= q1 && q0 != q1 { + Cmd(v.text.ReadDot()) + return + } + + // otherwise, select non-space chars under mouse and run that + p := pos - v.text.PrevSpace(pos) + n := pos + v.text.NextSpace(pos) + v.text.SetDot(p, n) + fn := strings.Trim(v.text.ReadDot(), "\n\t ") + v.text.SetDot(q0, q1) + Cmd(fn) + return +} |