aboutsummaryrefslogtreecommitdiff
path: root/pkg/prompt/main.go
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 /pkg/prompt/main.go
parente8b31652a0a0fa1767d5e5d86fa9f7016057262c (diff)
downloadgofu-888095449e80b33c1d5925e3164956567e43e86e.tar.gz
prompt: draw separator line
Diffstat (limited to 'pkg/prompt/main.go')
-rw-r--r--pkg/prompt/main.go30
1 files changed, 28 insertions, 2 deletions
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)
}