Implement rprompt

This commit is contained in:
Cheer Xiao 2014-01-04 22:45:35 +08:00
parent e0c78f08c4
commit 6c5ab5ced6
4 changed files with 28 additions and 3 deletions

View File

@ -8,6 +8,7 @@ var (
attrForCurrentCompletion = "7"
attrForCompleted = ";4"
attrForMode = "1;7;33"
attrForRprompt = "7"
)
var attrForType = map[parse.ItemType]string{

View File

@ -23,7 +23,7 @@ const (
type bufferState struct {
// States used during ReadLine.
tokens []parse.Item
prompt, line, tip string
prompt, rprompt, line, tip string
mode bufferMode
completion *completion
dot int
@ -35,6 +35,8 @@ func (bs *bufferState) finish() {
bs.mode = ModeInsert
bs.completion = nil
bs.dot = len(bs.line)
// TODO Perhaps make it optional to NOT clear the rprompt
bs.rprompt = ""
}
// Editor keeps the status of the line editor.
@ -185,8 +187,9 @@ func (ed *Editor) acceptCompletion() {
}
// ReadLine reads a line interactively.
func (ed *Editor) ReadLine(prompt string) (lr LineRead) {
func (ed *Editor) ReadLine(prompt string, rprompt string) (lr LineRead) {
ed.prompt = prompt
ed.rprompt = rprompt
ed.line = ""
ed.mode = ModeInsert
ed.tip = ""

View File

@ -220,6 +220,15 @@ func (w *writer) refresh(bs *bufferState) error {
}
}
// Write rprompt
padding := w.width - 1 - w.cursor.col - wcwidths(bs.rprompt)
if padding >= 1 {
w.writes(strings.Repeat(" ", padding))
w.currentAttr = attrForRprompt
w.writes(bs.rprompt)
w.currentAttr = ""
}
w.indent = 0
if bs.mode != ModeInsert {

View File

@ -3,6 +3,7 @@ package main
import (
"os"
"fmt"
"os/user"
"../parse"
"../edit"
"../eval"
@ -18,6 +19,17 @@ func main() {
ev := eval.NewEvaluator(os.Environ())
cmd_no := 0
username := "???"
user, err := user.Current()
if err == nil {
username = user.Username
}
hostname, err := os.Hostname()
if err != nil {
hostname = "???"
}
rprompt := username + "@" + hostname
for {
cmd_no++
name := fmt.Sprintf("<tty %d>", cmd_no)
@ -28,7 +40,7 @@ func main() {
}
prompt := util.Getwd() + "> "
lr := ed.ReadLine(prompt)
lr := ed.ReadLine(prompt, rprompt)
err = ed.Cleanup()
if err != nil {
panic(err)