Skip to content

Commit

Permalink
Change the signature of ExecuteOnEnterCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Verseth committed Jul 28, 2023
1 parent 890b7d4 commit b4a75e8
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 92 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.1] - 28.07.2023

[Diff](https://github.com/elk-language/go-prompt/compare/v1.1.0...elk-language:go-prompt:v1.1.1)

### Added
- `func (*prompt.Prompt).TerminalColumns() strings.Width`
- `func (*prompt.Prompt).TerminalRows() int`

### Changed
- Change signatures:
- `prompt.ExecuteOnEnterCallback`
- from `func(buffer *prompt.Buffer, indentSize int) (indent int, execute bool)`
- to `func(p *prompt.Prompt, indentSize int) (indent int, execute bool)`
- Change `(*Prompt).Buffer` from a public field to a public getter method


## [1.1.0] - 28.07.2023

[Diff](https://github.com/elk-language/go-prompt/compare/v1.0.3...elk-language:go-prompt:v1.1.0)
Expand Down
4 changes: 2 additions & 2 deletions _example/automatic-indenter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func main() {
p.Run()
}

func ExecuteOnEnter(buffer *prompt.Buffer, indentSize int) (int, bool) {
input := buffer.Text()
func ExecuteOnEnter(p *prompt.Prompt, indentSize int) (int, bool) {
input := p.Buffer().Text()
lines := strings.SplitAfter(input, "\n")
var spaces int
if len(lines) > 0 {
Expand Down
4 changes: 2 additions & 2 deletions _example/bang-executor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func main() {
p.Run()
}

func ExecuteOnEnter(buffer *prompt.Buffer, indentSize int) (int, bool) {
input := buffer.Text()
func ExecuteOnEnter(p *prompt.Prompt, indentSize int) (int, bool) {
input := p.Buffer().Text()
char, _ := utf8.DecodeLastRuneInString(input)
return 0, char == '!'
}
Expand Down
1 change: 1 addition & 0 deletions _example/http-prompt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func executor(in string) {

func completer(in prompt.Document) ([]prompt.Suggest, istrings.RuneNumber, istrings.RuneNumber) {
endIndex := in.CurrentRuneIndex()
in.

Check failure on line 163 in _example/http-prompt/main.go

View workflow job for this annotation

GitHub Actions / Build examples

non-name in.w on left side of :=
w := in.GetWordBeforeCursor()

Check failure on line 164 in _example/http-prompt/main.go

View workflow job for this annotation

GitHub Actions / Build examples

in.w undefined (type prompt.Document has no field or method w)
if w == "" {

Check failure on line 165 in _example/http-prompt/main.go

View workflow job for this annotation

GitHub Actions / Build examples

undefined: w
return []prompt.Suggest{}, 0, 0
Expand Down
6 changes: 3 additions & 3 deletions constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func WithPrefix(prefix string) Option {
// WithInitialText can be used to set the initial buffer text.
func WithInitialText(text string) Option {
return func(p *Prompt) error {
p.Buffer.InsertTextMoveCursor(text, p.renderer.col, int(p.renderer.row), true)
p.buffer.InsertTextMoveCursor(text, p.renderer.col, int(p.renderer.row), true)
return nil
}
}
Expand Down Expand Up @@ -286,7 +286,7 @@ func WithExitChecker(fn ExitChecker) Option {
}
}

func DefaultExecuteOnEnterCallback(buffer *Buffer, indentSize int) (int, bool) {
func DefaultExecuteOnEnterCallback(p *Prompt, indentSize int) (int, bool) {
return 0, true
}

Expand All @@ -299,7 +299,7 @@ func New(executor Executor, opts ...Option) *Prompt {
pt := &Prompt{
reader: NewStdinReader(),
renderer: NewRenderer(),
Buffer: NewBuffer(),
buffer: NewBuffer(),
executor: executor,
history: NewHistory(),
completion: NewCompletionManager(6),
Expand Down
22 changes: 11 additions & 11 deletions emacs.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var emacsKeyBindings = []KeyBind{
Key: ControlE,
Fn: func(p *Prompt) bool {
return p.CursorRightRunes(
istrings.RuneCountInString(p.Buffer.Document().CurrentLineAfterCursor()),
istrings.RuneCountInString(p.buffer.Document().CurrentLineAfterCursor()),
)
},
},
Expand All @@ -56,16 +56,16 @@ var emacsKeyBindings = []KeyBind{
Key: ControlA,
Fn: func(p *Prompt) bool {
return p.CursorLeftRunes(
p.Buffer.Document().FindStartOfFirstWordOfLine(),
p.buffer.Document().FindStartOfFirstWordOfLine(),
)
},
},
// Cut the Line after the cursor
{
Key: ControlK,
Fn: func(p *Prompt) bool {
p.Buffer.DeleteRunes(
istrings.RuneCountInString(p.Buffer.Document().CurrentLineAfterCursor()),
p.buffer.DeleteRunes(
istrings.RuneCountInString(p.buffer.Document().CurrentLineAfterCursor()),
p.renderer.col,
p.renderer.row,
)
Expand All @@ -76,8 +76,8 @@ var emacsKeyBindings = []KeyBind{
{
Key: ControlU,
Fn: func(p *Prompt) bool {
p.Buffer.DeleteBeforeCursorRunes(
istrings.RuneCountInString(p.Buffer.Document().CurrentLineBeforeCursor()),
p.buffer.DeleteBeforeCursorRunes(
istrings.RuneCountInString(p.buffer.Document().CurrentLineBeforeCursor()),
p.renderer.col,
p.renderer.row,
)
Expand All @@ -88,8 +88,8 @@ var emacsKeyBindings = []KeyBind{
{
Key: ControlD,
Fn: func(p *Prompt) bool {
if p.Buffer.Text() != "" {
p.Buffer.Delete(1, p.renderer.col, p.renderer.row)
if p.buffer.Text() != "" {
p.buffer.Delete(1, p.renderer.col, p.renderer.row)
return true
}
return false
Expand All @@ -99,7 +99,7 @@ var emacsKeyBindings = []KeyBind{
{
Key: ControlH,
Fn: func(p *Prompt) bool {
p.Buffer.DeleteBeforeCursor(1, p.renderer.col, p.renderer.row)
p.buffer.DeleteBeforeCursor(1, p.renderer.col, p.renderer.row)
return true
},
},
Expand All @@ -115,7 +115,7 @@ var emacsKeyBindings = []KeyBind{
Key: AltRight,
Fn: func(p *Prompt) bool {
return p.CursorRightRunes(
p.Buffer.Document().FindRuneNumberUntilEndOfCurrentWord(),
p.buffer.Document().FindRuneNumberUntilEndOfCurrentWord(),
)
},
},
Expand All @@ -131,7 +131,7 @@ var emacsKeyBindings = []KeyBind{
Key: AltLeft,
Fn: func(p *Prompt) bool {
return p.CursorLeftRunes(
p.Buffer.Document().FindRuneNumberUntilStartOfPreviousWord(),
p.buffer.Document().FindRuneNumberUntilStartOfPreviousWord(),
)
},
},
Expand Down
2 changes: 1 addition & 1 deletion emacs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestEmacsKeyBindings(t *testing.T) {
p := New(NoopExecutor)
buf := p.Buffer
buf := p.buffer
buf.InsertTextMoveCursor("abcde", DefColCount, DefRowCount, false)
if buf.cursorPosition != istrings.RuneNumber(len("abcde")) {
t.Errorf("Want %d, but got %d", len("abcde"), buf.cursorPosition)
Expand Down
12 changes: 6 additions & 6 deletions key_bind_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import (

// GoLineEnd Go to the End of the line
func GoLineEnd(p *Prompt) bool {
x := []rune(p.Buffer.Document().TextAfterCursor())
x := []rune(p.buffer.Document().TextAfterCursor())
return p.CursorRightRunes(istrings.RuneNumber(len(x)))
}

// GoLineBeginning Go to the beginning of the line
func GoLineBeginning(p *Prompt) bool {
x := []rune(p.Buffer.Document().TextBeforeCursor())
x := []rune(p.buffer.Document().TextBeforeCursor())
return p.CursorLeftRunes(istrings.RuneNumber(len(x)))
}

// DeleteChar Delete character under the cursor
func DeleteChar(p *Prompt) bool {
p.Buffer.Delete(1, p.renderer.col, p.renderer.row)
p.buffer.Delete(1, p.renderer.col, p.renderer.row)
return true
}

// DeleteBeforeChar Go to Backspace
func DeleteBeforeChar(p *Prompt) bool {
p.Buffer.DeleteBeforeCursor(1, p.renderer.col, p.renderer.row)
p.buffer.DeleteBeforeCursor(1, p.renderer.col, p.renderer.row)
return true
}

Expand All @@ -39,8 +39,8 @@ func GoLeftChar(p *Prompt) bool {
}

func DeleteWordBeforeCursor(p *Prompt) bool {
p.Buffer.DeleteBeforeCursorRunes(
istrings.RuneCountInString(p.Buffer.Document().GetWordBeforeCursorWithSpace()),
p.buffer.DeleteBeforeCursorRunes(
istrings.RuneCountInString(p.buffer.Document().GetWordBeforeCursorWithSpace()),
p.renderer.col,
p.renderer.row,
)
Expand Down
Loading

0 comments on commit b4a75e8

Please sign in to comment.