aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pkg/cli/query.go6
-rw-r--r--pkg/prompt/buffer.go66
-rw-r--r--pkg/prompt/login.go12
-rw-r--r--pkg/prompt/main.go31
4 files changed, 36 insertions, 79 deletions
diff --git a/pkg/cli/query.go b/pkg/cli/query.go
index 65cb2a5..2c53cfb 100644
--- a/pkg/cli/query.go
+++ b/pkg/cli/query.go
@@ -186,7 +186,9 @@ func displayChoices(out io.Writer, choices []Choice, selectedIndex int) {
}
}
-var ansiEscapeRx = regexp.MustCompile(`^\x1B\[[\x20-\x3F]*[\x40-\x7E]`)
+//AnsiEscapeRx is a regexp that matches full ANSI escape sequences that start
+//with the CSI.
+var AnsiEscapeRx = regexp.MustCompile(`^\x1B\[[\x20-\x3F]*[\x40-\x7E]`)
type buffer struct {
Input io.Reader
@@ -204,7 +206,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):])
diff --git a/pkg/prompt/buffer.go b/pkg/prompt/buffer.go
deleted file mode 100644
index 03aca43..0000000
--- a/pkg/prompt/buffer.go
+++ /dev/null
@@ -1,66 +0,0 @@
-/*******************************************************************************
-*
-* Copyright 2017 Stefan Majewsky <majewsky@gmx.net>
-*
-* This program is free software: you can redistribute it and/or modify it under
-* the terms of the GNU General Public License as published by the Free Software
-* Foundation, either version 3 of the License, or (at your option) any later
-* version.
-*
-* This program is distributed in the hope that it will be useful, but WITHOUT ANY
-* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License along with
-* this program. If not, see <http://www.gnu.org/licenses/>.
-*
-*******************************************************************************/
-
-package prompt
-
-import "bytes"
-
-//LineBuffer wraps bytes.Buffer that measures the length of the displayed text
-//(minus ANSI escape sequences).
-type LineBuffer struct {
- buffer bytes.Buffer
- length int
-}
-
-//Bytes returns the complete string (including non-printable characters) in the
-//buffer.
-func (b *LineBuffer) Bytes() []byte {
- return b.buffer.Bytes()
-}
-
-//Length returns the number of printable characters in the buffer.
-func (b *LineBuffer) Length() int {
- return b.length
-}
-
-//Write implements the io.Writer interface. Use this only for printable
-//characters.
-func (b *LineBuffer) Write(buf []byte) (n int, err error) {
- n, err = b.buffer.Write(buf)
- b.length += n
- return
-}
-
-//WriteNonprintable works like Write, but bytes written do not count towards
-//Length().
-func (b *LineBuffer) WriteNonprintable(buf []byte) (int, error) {
- return b.buffer.Write(buf)
-}
-
-//WriteWithColor writes a colored printable string. The color is given as the
-//semicolon-separated list of arguments to the ANSI escape sequence SGR, e.g.
-//"1;41" for bold with red background.
-func (b *LineBuffer) WriteWithColor(text, color string) {
- if color == "0" {
- b.Write([]byte(text))
- } else {
- b.WriteNonprintable([]byte("\x1B[" + color + "m"))
- b.Write([]byte(text))
- b.WriteNonprintable([]byte("\x1B[0m"))
- }
-}
diff --git a/pkg/prompt/login.go b/pkg/prompt/login.go
index b5fcebd..6e7d8f7 100644
--- a/pkg/prompt/login.go
+++ b/pkg/prompt/login.go
@@ -24,7 +24,9 @@ import "os"
//#include <pwd.h>
import "C"
-func addLogin(buf *LineBuffer) {
+func getLoginField() string {
+ var result string
+
//show user name
userName := getUserName()
commonUser := getenvOrDefault("PRETTYPROMPT_COMMONUSER", "stefan")
@@ -33,8 +35,7 @@ func addLogin(buf *LineBuffer) {
if userName == "root" {
color = "1;41"
}
- buf.WriteWithColor(userName, color)
- buf.Write([]byte("@"))
+ result = withColor(color, userName) + "@"
}
//show hostname
@@ -43,11 +44,10 @@ func addLogin(buf *LineBuffer) {
handleError(err)
hostname = "<unknown>"
}
- buf.WriteWithColor(
+ return result + withColor(
getenvOrDefault("PRETTYPROMPT_HOSTCOLOR", "0;33"),
hostname,
- )
- buf.Write([]byte(" "))
+ ) + " "
}
func getUserName() string {
diff --git a/pkg/prompt/main.go b/pkg/prompt/main.go
index 6b4764a..ff51f0c 100644
--- a/pkg/prompt/main.go
+++ b/pkg/prompt/main.go
@@ -18,16 +18,22 @@
package prompt
-import "os"
+import (
+ "os"
+ "strings"
+
+ "github.com/majewsky/gofu/pkg/cli"
+)
//Exec executes the prettyprompt applet and returns an exit code (0 for
//success, >0 for error).
func Exec() int {
- var buf LineBuffer
- addLogin(&buf)
+ fields := []string{
+ getLoginField(),
+ }
+ line := strings.Join(fields, " ")
- os.Stdout.Write(buf.Bytes())
- os.Stdout.Write([]byte("\n"))
+ os.Stdout.Write([]byte(line + "\n"))
return 0
}
@@ -44,3 +50,18 @@ func handleError(err error) {
os.Stderr.Write([]byte("\x1B[1;31mPrompt error: " + err.Error() + "\x1B[0m\n"))
}
}
+
+func getPrintableLength(text string) int {
+ return len(cli.AnsiEscapeRx.ReplaceAllString(text, ""))
+}
+
+//withColor adds ANSI escape sequences to the string to display it with a
+//certain color. The color is given as the semicolon-separated list of
+//arguments to the ANSI escape sequence SGR, e.g. "1;41" for bold with red
+//background.
+func withColor(color, text string) string {
+ if color == "0" {
+ return text
+ }
+ return "\x1B[" + color + "m" + text + "\x1B[0m"
+}