fix(cmd): history in alt mode

This commit is contained in:
Michael Yang 2024-01-05 16:19:37 -08:00
parent 3a9f447141
commit 2ef9352b94
2 changed files with 28 additions and 26 deletions

View file

@ -25,10 +25,7 @@ func NewBuffer(prompt *Prompt) (*Buffer, error) {
return nil, err return nil, err
} }
lwidth := width - len(prompt.Prompt) lwidth := width - len(prompt.prompt())
if prompt.UseAlt {
lwidth = width - len(prompt.AltPrompt)
}
b := &Buffer{ b := &Buffer{
Pos: 0, Pos: 0,
@ -78,7 +75,7 @@ func (b *Buffer) MoveRight() {
if b.Pos < b.Size() { if b.Pos < b.Size() {
b.Pos += 1 b.Pos += 1
if b.Pos%b.LineWidth == 0 { if b.Pos%b.LineWidth == 0 {
fmt.Printf(CursorDown + CursorBOL + cursorRightN(b.PromptSize())) fmt.Printf(CursorDown + CursorBOL + cursorRightN(len(b.Prompt.prompt())))
} else { } else {
fmt.Print(CursorRight) fmt.Print(CursorRight)
} }
@ -109,7 +106,7 @@ func (b *Buffer) MoveToStart() {
fmt.Print(CursorUp) fmt.Print(CursorUp)
} }
} }
fmt.Printf(CursorBOL + cursorRightN(b.PromptSize())) fmt.Printf(CursorBOL + cursorRightN(len(b.Prompt.prompt())))
b.Pos = 0 b.Pos = 0
} }
} }
@ -123,7 +120,7 @@ func (b *Buffer) MoveToEnd() {
fmt.Print(CursorDown) fmt.Print(CursorDown)
} }
remainder := b.Size() % b.LineWidth remainder := b.Size() % b.LineWidth
fmt.Printf(CursorBOL + cursorRightN(b.PromptSize()+remainder)) fmt.Printf(CursorBOL + cursorRightN(len(b.Prompt.prompt())+remainder))
} else { } else {
fmt.Print(cursorRightN(b.Size() - b.Pos)) fmt.Print(cursorRightN(b.Size() - b.Pos))
} }
@ -143,13 +140,6 @@ func min(n, m int) int {
return n return n
} }
func (b *Buffer) PromptSize() int {
if b.Prompt.UseAlt {
return len(b.Prompt.AltPrompt)
}
return len(b.Prompt.Prompt)
}
func (b *Buffer) Add(r rune) { func (b *Buffer) Add(r rune) {
if b.Pos == b.Buf.Size() { if b.Pos == b.Buf.Size() {
fmt.Printf("%c", r) fmt.Printf("%c", r)
@ -232,7 +222,7 @@ func (b *Buffer) Remove() {
remainingLines := (b.Size() - b.Pos) / b.LineWidth remainingLines := (b.Size() - b.Pos) / b.LineWidth
fmt.Printf(cursorDownN(remainingLines+1) + CursorBOL + ClearToEOL) fmt.Printf(cursorDownN(remainingLines+1) + CursorBOL + ClearToEOL)
place := b.Pos % b.LineWidth place := b.Pos % b.LineWidth
fmt.Printf(cursorUpN(remainingLines+1) + cursorRightN(place+len(b.Prompt.Prompt))) fmt.Printf(cursorUpN(remainingLines+1) + cursorRightN(place+len(b.Prompt.prompt())))
} }
} }
} }
@ -247,7 +237,7 @@ func (b *Buffer) Delete() {
remainingLines := (b.Size() - b.Pos) / b.LineWidth remainingLines := (b.Size() - b.Pos) / b.LineWidth
fmt.Printf(cursorDownN(remainingLines) + CursorBOL + ClearToEOL) fmt.Printf(cursorDownN(remainingLines) + CursorBOL + ClearToEOL)
place := b.Pos % b.LineWidth place := b.Pos % b.LineWidth
fmt.Printf(cursorUpN(remainingLines) + cursorRightN(place+len(b.Prompt.Prompt))) fmt.Printf(cursorUpN(remainingLines) + cursorRightN(place+len(b.Prompt.prompt())))
} }
} }
} }
@ -294,15 +284,15 @@ func (b *Buffer) DeleteWord() {
} }
func (b *Buffer) ClearScreen() { func (b *Buffer) ClearScreen() {
fmt.Printf(ClearScreen + CursorReset + b.Prompt.Prompt) fmt.Printf(ClearScreen + CursorReset + b.Prompt.prompt())
if b.IsEmpty() { if b.IsEmpty() {
ph := b.Prompt.Placeholder ph := b.Prompt.placeholder()
fmt.Printf(ColorGrey + ph + cursorLeftN(len(ph)) + ColorDefault) fmt.Printf(ColorGrey + ph + cursorLeftN(len(ph)) + ColorDefault)
} else { } else {
currPos := b.Pos currPos := b.Pos
b.Pos = 0 b.Pos = 0
b.drawRemaining() b.drawRemaining()
fmt.Printf(CursorReset + cursorRightN(len(b.Prompt.Prompt))) fmt.Printf(CursorReset + cursorRightN(len(b.Prompt.prompt())))
if currPos > 0 { if currPos > 0 {
targetLine := currPos / b.LineWidth targetLine := currPos / b.LineWidth
if targetLine > 0 { if targetLine > 0 {
@ -329,7 +319,7 @@ func (b *Buffer) IsEmpty() bool {
func (b *Buffer) Replace(r []rune) { func (b *Buffer) Replace(r []rune) {
b.Pos = 0 b.Pos = 0
b.Buf.Clear() b.Buf.Clear()
fmt.Printf(ClearLine + CursorBOL + b.Prompt.Prompt) fmt.Printf(ClearLine + CursorBOL + b.Prompt.prompt())
for _, c := range r { for _, c := range r {
b.Add(c) b.Add(c)
} }

View file

@ -16,6 +16,20 @@ type Prompt struct {
UseAlt bool UseAlt bool
} }
func (p *Prompt) prompt() string {
if p.UseAlt {
return p.AltPrompt
}
return p.Prompt
}
func (p *Prompt) placeholder() string {
if p.UseAlt {
return p.AltPlaceholder
}
return p.Placeholder
}
type Terminal struct { type Terminal struct {
outchan chan rune outchan chan rune
} }
@ -46,8 +60,9 @@ func New(prompt Prompt) (*Instance, error) {
} }
func (i *Instance) Readline() (string, error) { func (i *Instance) Readline() (string, error) {
prompt := i.Prompt.Prompt prompt := i.Prompt.prompt()
if i.Prompt.UseAlt || i.Pasting { if i.Pasting {
// force alt prompt when pasting
prompt = i.Prompt.AltPrompt prompt = i.Prompt.AltPrompt
} }
fmt.Print(prompt) fmt.Print(prompt)
@ -71,10 +86,7 @@ func (i *Instance) Readline() (string, error) {
// don't show placeholder when pasting unless we're in multiline mode // don't show placeholder when pasting unless we're in multiline mode
showPlaceholder := !i.Pasting || i.Prompt.UseAlt showPlaceholder := !i.Pasting || i.Prompt.UseAlt
if buf.IsEmpty() && showPlaceholder { if buf.IsEmpty() && showPlaceholder {
ph := i.Prompt.Placeholder ph := i.Prompt.placeholder()
if i.Prompt.UseAlt {
ph = i.Prompt.AltPlaceholder
}
fmt.Printf(ColorGrey + ph + fmt.Sprintf(CursorLeftN, len(ph)) + ColorDefault) fmt.Printf(ColorGrey + ph + fmt.Sprintf(CursorLeftN, len(ph)) + ColorDefault)
} }