Skip to content

Commit

Permalink
Add additional methods to *prompt.Prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
Verseth committed Jul 29, 2023
1 parent b4a75e8 commit e8b7384
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ 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.2] - 29.07.2023

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

### Added
- `func (*prompt.Prompt).DeleteBeforeCursor(count strings.GraphemeNumber) string`
- `func (*prompt.Prompt).DeleteBeforeCursorRunes(count strings.RuneNumber) string`
- `func (*prompt.Prompt).Delete(count strings.GraphemeNumber) string`
- `func (*prompt.Prompt).DeleteRunes(count strings.RuneNumber) string`
- `func (*prompt.Prompt).InsertText(text string, overwrite bool)`
- `func (*prompt.Prompt).InsertTextMoveCursor(text string, overwrite bool)`
- `func (*prompt.Prompt).UserInputColumns() strings.Width`

## [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)
Expand Down
2 changes: 1 addition & 1 deletion buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (b *Buffer) CursorDown(count int, columns istrings.Width, rows int) bool {
return b.recalculateStartLine(columns, rows)
}

// DeleteBeforeCursor delete specified number of graphemes before the cursor and returns the deleted text.
// Deletes the specified number of graphemes before the cursor and returns the deleted text.
func (b *Buffer) DeleteBeforeCursor(count istrings.GraphemeNumber, columns istrings.Width, rows int) string {
debug.Assert(count >= 0, "count should be positive")
if b.cursorPosition < 0 {
Expand Down
36 changes: 36 additions & 0 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ func (p *Prompt) IndentSize() int {
return p.renderer.indentSize
}

// Get the number of columns that are available
// for user input.
func (p *Prompt) UserInputColumns() istrings.Width {
return p.renderer.UserInputColumns()
}

// Returns the current amount of columns that the terminal can display.
func (p *Prompt) TerminalColumns() istrings.Width {
return p.renderer.col
Expand Down Expand Up @@ -621,6 +627,36 @@ func (p *Prompt) CursorDown(count int) bool {
return false
}

// Deletes the specified number of graphemes before the cursor and returns the deleted text.
func (p *Prompt) DeleteBeforeCursor(count istrings.GraphemeNumber) string {
return p.buffer.DeleteBeforeCursor(count, p.UserInputColumns(), p.renderer.row)
}

// Deletes the specified number of runes before the cursor and returns the deleted text.
func (p *Prompt) DeleteBeforeCursorRunes(count istrings.RuneNumber) string {
return p.buffer.DeleteBeforeCursorRunes(count, p.UserInputColumns(), p.renderer.row)
}

// Deletes the specified number of graphemes and returns the deleted text.
func (p *Prompt) Delete(count istrings.GraphemeNumber) string {
return p.buffer.Delete(count, p.UserInputColumns(), p.renderer.row)
}

// Deletes the specified number of runes and returns the deleted text.
func (p *Prompt) DeleteRunes(count istrings.RuneNumber) string {
return p.buffer.DeleteRunes(count, p.UserInputColumns(), p.renderer.row)
}

// Insert string into the buffer without moving the cursor.
func (p *Prompt) InsertText(text string, overwrite bool) {
p.buffer.InsertText(text, overwrite)
}

// Insert string into the buffer and move the cursor.
func (p *Prompt) InsertTextMoveCursor(text string, overwrite bool) {
p.buffer.InsertTextMoveCursor(text, p.UserInputColumns(), p.renderer.row, overwrite)
}

func (p *Prompt) Close() {
if !p.skipClose {
debug.AssertNoError(p.reader.Close())
Expand Down

0 comments on commit e8b7384

Please sign in to comment.