Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
2ef9352b
Commit
2ef9352b
authored
Jan 05, 2024
by
Michael Yang
Browse files
fix(cmd): history in alt mode
parent
3a9f4471
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
26 deletions
+28
-26
readline/buffer.go
readline/buffer.go
+10
-20
readline/readline.go
readline/readline.go
+18
-6
No files found.
readline/buffer.go
View file @
2ef9352b
...
@@ -25,10 +25,7 @@ func NewBuffer(prompt *Prompt) (*Buffer, error) {
...
@@ -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() {
...
@@ -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
.
Prompt
Size
(
)))
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() {
...
@@ -109,7 +106,7 @@ func (b *Buffer) MoveToStart() {
fmt
.
Print
(
CursorUp
)
fmt
.
Print
(
CursorUp
)
}
}
}
}
fmt
.
Printf
(
CursorBOL
+
cursorRightN
(
b
.
Prompt
Size
(
)))
fmt
.
Printf
(
CursorBOL
+
cursorRightN
(
len
(
b
.
Prompt
.
prompt
()
)))
b
.
Pos
=
0
b
.
Pos
=
0
}
}
}
}
...
@@ -123,7 +120,7 @@ func (b *Buffer) MoveToEnd() {
...
@@ -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
.
Prompt
Size
(
)
+
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 {
...
@@ -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() {
...
@@ -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
.
P
rompt
)))
fmt
.
Printf
(
cursorUpN
(
remainingLines
+
1
)
+
cursorRightN
(
place
+
len
(
b
.
Prompt
.
p
rompt
()
)))
}
}
}
}
}
}
...
@@ -247,7 +237,7 @@ func (b *Buffer) Delete() {
...
@@ -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
.
P
rompt
)))
fmt
.
Printf
(
cursorUpN
(
remainingLines
)
+
cursorRightN
(
place
+
len
(
b
.
Prompt
.
p
rompt
()
)))
}
}
}
}
}
}
...
@@ -294,15 +284,15 @@ func (b *Buffer) DeleteWord() {
...
@@ -294,15 +284,15 @@ func (b *Buffer) DeleteWord() {
}
}
func
(
b
*
Buffer
)
ClearScreen
()
{
func
(
b
*
Buffer
)
ClearScreen
()
{
fmt
.
Printf
(
ClearScreen
+
CursorReset
+
b
.
Prompt
.
P
rompt
)
fmt
.
Printf
(
ClearScreen
+
CursorReset
+
b
.
Prompt
.
p
rompt
()
)
if
b
.
IsEmpty
()
{
if
b
.
IsEmpty
()
{
ph
:=
b
.
Prompt
.
P
laceholder
ph
:=
b
.
Prompt
.
p
laceholder
()
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
.
P
rompt
)))
fmt
.
Printf
(
CursorReset
+
cursorRightN
(
len
(
b
.
Prompt
.
p
rompt
()
)))
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 {
...
@@ -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
.
P
rompt
)
fmt
.
Printf
(
ClearLine
+
CursorBOL
+
b
.
Prompt
.
p
rompt
()
)
for
_
,
c
:=
range
r
{
for
_
,
c
:=
range
r
{
b
.
Add
(
c
)
b
.
Add
(
c
)
}
}
...
...
readline/readline.go
View file @
2ef9352b
...
@@ -16,6 +16,20 @@ type Prompt struct {
...
@@ -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) {
...
@@ -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) {
...
@@ -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
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment