summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2017-07-11 16:33:22 +0200
committerStefan Majewsky <majewsky@gmx.net>2017-07-11 16:33:22 +0200
commit888095449e80b33c1d5925e3164956567e43e86e (patch)
tree27436f3835ba18bb6b4b3b54e6dbb1de788e6120
parente8b31652a0a0fa1767d5e5d86fa9f7016057262c (diff)
downloadgofu-888095449e80b33c1d5925e3164956567e43e86e.tar.gz
prompt: draw separator line
-rw-r--r--pkg/cli/query.go11
-rw-r--r--pkg/prompt/main.go30
2 files changed, 33 insertions, 8 deletions
diff --git a/pkg/cli/query.go b/pkg/cli/query.go
index 2c53cfb..add170c 100644
--- a/pkg/cli/query.go
+++ b/pkg/cli/query.go
@@ -186,9 +186,7 @@ func displayChoices(out io.Writer, choices []Choice, selectedIndex int) {
}
}
-//AnsiEscapeRx is a regexp that matches full ANSI escape sequences that start
-//with the CSI.
-var AnsiEscapeRx = regexp.MustCompile(`^\x1B\[[\x20-\x3F]*[\x40-\x7E]`)
+var ansiEscapeRx = regexp.MustCompile(`^\x1B\[[\x20-\x3F]*[\x40-\x7E]`)
type buffer struct {
Input io.Reader
@@ -206,7 +204,7 @@ func (b *buffer) getNextInput() []byte {
}
//do we have a full ANSI escape sequence?
- match := AnsiEscapeRx.Find(b.buf[0:b.fill])
+ match := ansiEscapeRx.Find(b.buf[0:b.fill])
if match != nil {
result := append([]byte(nil), match...)
copy(b.buf[0:], b.buf[len(match):])
@@ -237,10 +235,11 @@ type pipeTUI struct {
i *Implementation
}
-var ansiColorCodeRx = regexp.MustCompile("\x1B" + `\[[0-9;]*m`)
+//AnsiColorCodeRx is a regexp that matches ANSI escape sequences of the type SGR.
+var AnsiColorCodeRx = regexp.MustCompile("\x1B" + `\[[0-9;]*m`)
func (t *pipeTUI) Print(w io.Writer, msg string) {
- w.Write([]byte(ansiColorCodeRx.ReplaceAllString(msg, "")))
+ w.Write([]byte(AnsiColorCodeRx.ReplaceAllString(msg, "")))
}
func (t *pipeTUI) ReadLine(prompt string) (string, error) {
diff --git a/pkg/prompt/main.go b/pkg/prompt/main.go
index ff51f0c..8ff56c9 100644
--- a/pkg/prompt/main.go
+++ b/pkg/prompt/main.go
@@ -19,9 +19,12 @@
package prompt
import (
+ "fmt"
"os"
"strings"
+ "golang.org/x/crypto/ssh/terminal"
+
"github.com/majewsky/gofu/pkg/cli"
)
@@ -32,6 +35,24 @@ func Exec() int {
getLoginField(),
}
line := strings.Join(fields, " ")
+ lineWidth := getPrintableLength(line)
+
+ //add dashes to expand `line` to fill the terminal's width
+ termWidth, _, err := terminal.GetSize(0)
+ if err != nil {
+ termWidth = 80
+ }
+ if termWidth > lineWidth {
+ line += " "
+ lineWidth++
+ }
+ if termWidth > lineWidth {
+ dashes := make([]byte, termWidth-lineWidth)
+ for idx := range dashes {
+ dashes[idx] = '-'
+ }
+ line += string(dashes)
+ }
os.Stdout.Write([]byte(line + "\n"))
return 0
@@ -52,7 +73,7 @@ func handleError(err error) {
}
func getPrintableLength(text string) int {
- return len(cli.AnsiEscapeRx.ReplaceAllString(text, ""))
+ return len(cli.AnsiColorCodeRx.ReplaceAllString(text, ""))
}
//withColor adds ANSI escape sequences to the string to display it with a
@@ -63,5 +84,10 @@ func withColor(color, text string) string {
if color == "0" {
return text
}
- return "\x1B[" + color + "m" + text + "\x1B[0m"
+ return fmt.Sprintf("\x1B[%sm%s\x1B[0m", color, text)
+}
+
+//withType adds a type annotation with a standardized format to the text.
+func withType(typeStr, text string) string {
+ return fmt.Sprintf("\x1B[37m%s:\x1B[0m%s", typeStr, text)
}