From 8ef481706897209c66e41fa3ee5b20a50d965b6a Mon Sep 17 00:00:00 2001 From: Shingo Kawamura Date: Wed, 17 Jan 2018 00:09:57 +0900 Subject: [PATCH] Add SetCursor --- README.md | 2 +- lib/isac.go | 23 +++++++++++++---------- lib/row/row.go | 29 +++++++++++++++++++++-------- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 2fb6324..e73744d 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ $ isac |Name|Description| |-|-| -|ESC, C-c|exit| +|C-c|exit| |Arrow Up, C-p|move current row up| |Arrow Down, C-n|move current row down| |C-u|power on current row's server| diff --git a/lib/isac.go b/lib/isac.go index 20d3fa9..6aab13e 100644 --- a/lib/isac.go +++ b/lib/isac.go @@ -84,7 +84,7 @@ MAINLOOP: switch ev.Type { case termbox.EventKey: switch ev.Key { - case termbox.KeyEsc, termbox.KeyCtrlC: + case termbox.KeyCtrlC: break MAINLOOP case termbox.KeyArrowUp, termbox.KeyCtrlP: i.currentRowUp() @@ -140,21 +140,22 @@ func (i *Isac) draw(message string) { lines := []string{ "Quick reference for isac keybindings:", "", - ", exit", - ", move current row up", - ", move current row down", - " power on current row's server", - " refresh rows", - ", delete a filter character", - " sort rows", - " show help", - " show current row's detail", + " exit", + ", move current row up", + ", move current row down", + " power on current row's server", + " refresh rows", + ", C-b>, delete a filter character", + " sort rows", + " show help", + " show current row's detail", } for index, line := range lines { i.setLine(index, line) } + termbox.HideCursor() termbox.Flush() return } @@ -180,6 +181,7 @@ func (i *Isac) draw(message string) { i.setLine(index, line) } + termbox.HideCursor() termbox.Flush() return } @@ -215,6 +217,7 @@ func (i *Isac) draw(message string) { } headers := i.row.Headers(i.message, strings.Join(i.zones, ", "), len(servers), i.currentNo(), i.filter) + termbox.SetCursor(i.row.CursorX, i.row.CursorY) for index, header := range headers { i.setLine(index, header) diff --git a/lib/row/row.go b/lib/row/row.go index 8a53c5c..d084b4a 100644 --- a/lib/row/row.go +++ b/lib/row/row.go @@ -1,10 +1,16 @@ package row -import "fmt" +import ( + "fmt" + + runewidth "github.com/mattn/go-runewidth" +) type Row struct { MovableBottom int Current int + CursorX int + CursorY int } func New() *Row { @@ -15,14 +21,21 @@ func New() *Row { func (r *Row) Headers(message string, zones string, totalServers int, currentNo int, filter string) (headers []string) { id := fmt.Sprintf("%-12v", "ID") - headers = []string{ - fmt.Sprintf("isac Message: %v", message), - fmt.Sprintf("Selected Zones: %v", zones), - fmt.Sprintf("Total Servers: %v, Current No.: %v", totalServers, currentNo), - fmt.Sprintf("Filter: %v", filter), - fmt.Sprintf(""), - fmt.Sprintf("Zone %v Status Name", id), + headers = append(headers, fmt.Sprintf("isac Message: %v", message)) + headers = append(headers, fmt.Sprintf("Selected Zones: %v", zones)) + headers = append(headers, fmt.Sprintf("Total Servers: %v, Current No.: %v", totalServers, currentNo)) + headers = append(headers, fmt.Sprintf("Filter: %v", filter)) + + r.CursorY = len(headers) - 1 + runes := []rune(headers[r.CursorY]) + x := 0 + for _, r := range runes { + x += runewidth.RuneWidth(r) } + r.CursorX = x + + headers = append(headers, fmt.Sprintf("")) + headers = append(headers, fmt.Sprintf("Zone %v Status Name", id)) return headers }