buffer

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer emulates the console buffer.

func NewBuffer

func NewBuffer() (b *Buffer)

NewBuffer is constructor of Buffer struct.

func (*Buffer) CursorDown

func (b *Buffer) CursorDown(count int)

CursorDown move cursor to the next line. (for multi-line edit).

func (*Buffer) CursorLeft

func (b *Buffer) CursorLeft(count int)

CursorLeft move to left on the current line.

func (*Buffer) CursorRight

func (b *Buffer) CursorRight(count int)

CursorRight move to right on the current line.

func (*Buffer) CursorUp

func (b *Buffer) CursorUp(count int)

CursorUp move cursor to the previous line. (for multi-line edit).

func (*Buffer) Delete

func (b *Buffer) Delete(count int) (deleted string)

Delete specified number of characters and Return the deleted text.

func (*Buffer) DeleteBeforeCursor

func (b *Buffer) DeleteBeforeCursor(count int) (deleted string)

DeleteBeforeCursor delete specified number of characters before cursor and return the deleted text.

func (*Buffer) DisplayCursorPosition

func (b *Buffer) DisplayCursorPosition() int

DisplayCursorPosition returns the cursor position on rendered text on terminal emulators. So if Document is "日本(cursor)語", DisplayedCursorPosition returns 4 because '日' and '本' are double width characters.

func (*Buffer) Document

func (b *Buffer) Document() (d *Document)

Document method to return document instance from the current text and cursor position.

func (*Buffer) InsertText

func (b *Buffer) InsertText(v string, overwrite bool, moveCursor bool)

InsertText insert string from current line.

func (*Buffer) JoinNextLine

func (b *Buffer) JoinNextLine(separator string)

JoinNextLine joins the next line to the current one by deleting the line ending after the current line.

func (*Buffer) NewLine

func (b *Buffer) NewLine(copyMargin bool)

NewLine means CR.

func (*Buffer) Reset

func (b *Buffer) Reset()

func (*Buffer) SwapCharactersBeforeCursor

func (b *Buffer) SwapCharactersBeforeCursor()

SwapCharactersBeforeCursor swaps the last two characters before the cursor.

func (*Buffer) Text

func (b *Buffer) Text() string

Text returns string of the current line.

type Document

type Document struct {
	Text string
	// contains filtered or unexported fields
}

Document has text displayed in terminal and cursor position.

func NewDocument

func NewDocument() *Document

NewDocument return the new empty document.

func (*Document) CurrentLine

func (d *Document) CurrentLine() string

CurrentLine return the text on the line where the cursor is. (when the input consists of just one line, it equals `text`.

Example
d := &Document{
	Text: `Hello! my name is c-bata.
This is a example of Document component.
This component has texts displayed in terminal and cursor position.
`,
	cursorPosition: len(`Hello! my name is c-bata.
This is a exam`),
}
fmt.Println(d.CurrentLine())
Output:

This is a example of Document component.

func (*Document) CurrentLineAfterCursor

func (d *Document) CurrentLineAfterCursor() string

CurrentLineAfterCursor returns the text from the cursor until the end of the line.

Example
d := &Document{
	Text: `Hello! my name is c-bata.
This is a example of Document component.
This component has texts displayed in terminal and cursor position.
`,
	cursorPosition: len(`Hello! my name is c-bata.
This is a exam`),
}
fmt.Println(d.CurrentLineAfterCursor())
Output:

ple of Document component.

func (*Document) CurrentLineBeforeCursor

func (d *Document) CurrentLineBeforeCursor() string

CurrentLineBeforeCursor returns the text from the start of the line until the cursor.

Example
d := &Document{
	Text: `Hello! my name is c-bata.
This is a example of Document component.
This component has texts displayed in terminal and cursor position.
`,
	cursorPosition: len(`Hello! my name is c-bata.
This is a exam`),
}
fmt.Println(d.CurrentLineBeforeCursor())
Output:

This is a exam

func (*Document) CursorPositionCol

func (d *Document) CursorPositionCol() (col int)

CursorPositionCol returns the current column. (0-based.)

Example
d := &Document{
	Text: `Hello! my name is c-bata.
This is a example of Document component.
This component has texts displayed in terminal and cursor position.
`,
	cursorPosition: len(`Hello! my name is c-bata.
This is a exam`),
}
fmt.Println("CursorPositionCol", d.CursorPositionCol())
Output:

CursorPositionCol 14

func (*Document) CursorPositionRow

func (d *Document) CursorPositionRow() (row int)

CursorPositionRow returns the current row. (0-based.)

Example
d := &Document{
	Text: `Hello! my name is c-bata.
This is a example of Document component.
This component has texts displayed in terminal and cursor position.
`,
	cursorPosition: len(`Hello! my name is c-bata.
This is a exam`),
}
fmt.Println("CursorPositionRow", d.CursorPositionRow())
Output:

CursorPositionRow 1

func (*Document) DisplayCursorPosition

func (d *Document) DisplayCursorPosition() int

DisplayCursorPosition returns the cursor position on rendered text on terminal emulators. So if Document is "日本(cursor)語", DisplayedCursorPosition returns 4 because '日' and '本' are double width characters.

Example
d := &Document{
	Text:           `Hello! my name is c-bata.`,
	cursorPosition: len(`Hello`),
}
fmt.Println("DisplayCursorPosition", d.DisplayCursorPosition())
Output:

DisplayCursorPosition 5
Example (WithJapanese)
d := &Document{
	Text:           `こんにちは、芝田 将です。`,
	cursorPosition: 3,
}
fmt.Println("DisplayCursorPosition", d.DisplayCursorPosition())
Output:

DisplayCursorPosition 6

func (*Document) FindEndOfCurrentWord

func (d *Document) FindEndOfCurrentWord() int

FindEndOfCurrentWord returns an index relative to the cursor position. pointing to the end of the current word. Return 0 if nothing was found.

func (*Document) FindEndOfCurrentWordUntilSeparator

func (d *Document) FindEndOfCurrentWordUntilSeparator(sep string) int

FindEndOfCurrentWordUntilSeparator is almost the same as FindEndOfCurrentWord. But this can specify Separator. Return 0 if nothing was found.

func (*Document) FindEndOfCurrentWordUntilSeparatorIgnoreNextToCursor

func (d *Document) FindEndOfCurrentWordUntilSeparatorIgnoreNextToCursor(sep string) int

FindEndOfCurrentWordUntilSeparatorIgnoreNextToCursor is almost the same as FindEndOfCurrentWordWithSpace. But this can specify Separator. Return 0 if nothing was found.

func (*Document) FindEndOfCurrentWordWithSpace

func (d *Document) FindEndOfCurrentWordWithSpace() int

FindEndOfCurrentWordWithSpace is almost the same as FindEndOfCurrentWord. The only difference is to ignore contiguous spaces.

func (*Document) FindStartOfPreviousWord

func (d *Document) FindStartOfPreviousWord() int

FindStartOfPreviousWord returns an index relative to the cursor position pointing to the start of the previous word. Return 0 if nothing was found.

func (*Document) FindStartOfPreviousWordUntilSeparator

func (d *Document) FindStartOfPreviousWordUntilSeparator(sep string) int

FindStartOfPreviousWordUntilSeparator is almost the same as FindStartOfPreviousWord. But this can specify Separator. Return 0 if nothing was found.

func (*Document) FindStartOfPreviousWordUntilSeparatorIgnoreNextToCursor

func (d *Document) FindStartOfPreviousWordUntilSeparatorIgnoreNextToCursor(sep string) int

FindStartOfPreviousWordUntilSeparatorIgnoreNextToCursor is almost the same as FindStartOfPreviousWordWithSpace. But this can specify Separator. Return 0 if nothing was found.

func (*Document) FindStartOfPreviousWordWithSpace

func (d *Document) FindStartOfPreviousWordWithSpace() int

FindStartOfPreviousWordWithSpace is almost the same as FindStartOfPreviousWord. The only difference is to ignore contiguous spaces.

func (*Document) GetCharRelativeToCursor

func (d *Document) GetCharRelativeToCursor(offset int) (r rune)

GetCharRelativeToCursor return character relative to cursor position, or empty string

func (*Document) GetCursorDownPosition

func (d *Document) GetCursorDownPosition(count int, preferredColumn int) int

GetCursorDownPosition return the relative cursor position (character index) where we would be if the user pressed the arrow-down button.

func (*Document) GetCursorLeftPosition

func (d *Document) GetCursorLeftPosition(count int) int

GetCursorLeftPosition returns the relative position for cursor left.

func (*Document) GetCursorRightPosition

func (d *Document) GetCursorRightPosition(count int) int

GetCursorRightPosition returns relative position for cursor right.

func (*Document) GetCursorUpPosition

func (d *Document) GetCursorUpPosition(count int, preferredColumn int) int

GetCursorUpPosition return the relative cursor position (character index) where we would be if the user pressed the arrow-up button.

func (*Document) GetEndOfLinePosition

func (d *Document) GetEndOfLinePosition() int

GetEndOfLinePosition returns relative position for the end of this line.

func (*Document) GetWordAfterCursor

func (d *Document) GetWordAfterCursor() string

GetWordAfterCursor returns the word after the cursor. If we have whitespace after the cursor this returns an empty string.

Example
d := &Document{
	Text: `Hello! my name is c-bata.
This is a example of Document component.
`,
	cursorPosition: len(`Hello! my name is c-bata.
This is a exam`),
}
fmt.Println(d.GetWordAfterCursor())
Output:

ple

func (*Document) GetWordAfterCursorUntilSeparator

func (d *Document) GetWordAfterCursorUntilSeparator(sep string) string

GetWordAfterCursorUntilSeparator returns the text after the cursor until next separator.

Example
d := &Document{
	Text:           `hello,i am c-bata,thank you for using go-prompt`,
	cursorPosition: len(`hello,i a`),
}
fmt.Println(d.GetWordAfterCursorUntilSeparator(","))
Output:

m c-bata

func (*Document) GetWordAfterCursorUntilSeparatorIgnoreNextToCursor

func (d *Document) GetWordAfterCursorUntilSeparatorIgnoreNextToCursor(sep string) string

GetWordAfterCursorUntilSeparatorIgnoreNextToCursor returns the word after the cursor. Unlike GetWordAfterCursor, it returns string containing space

Example
d := &Document{
	Text:           `hello,i am c-bata,thank you for using go-prompt`,
	cursorPosition: len(`hello`),
}
fmt.Println(d.GetWordAfterCursorUntilSeparatorIgnoreNextToCursor(","))
Output:

,i am c-bata

func (*Document) GetWordAfterCursorWithSpace

func (d *Document) GetWordAfterCursorWithSpace() string

GetWordAfterCursorWithSpace returns the word after the cursor. Unlike GetWordAfterCursor, it returns string containing space

Example
d := &Document{
	Text: `Hello! my name is c-bata.
This is a example of Document component.
`,
	cursorPosition: len(`Hello! my name is c-bata.
This is a`),
}
fmt.Println(d.GetWordAfterCursorWithSpace())
Output:

 example

func (*Document) GetWordBeforeCursor

func (d *Document) GetWordBeforeCursor() string

GetWordBeforeCursor returns the word before the cursor. If we have whitespace before the cursor this returns an empty string.

Example
d := &Document{
	Text: `Hello! my name is c-bata.
This is a example of Document component.
`,
	cursorPosition: len(`Hello! my name is c-bata.
This is a exam`),
}
fmt.Println(d.GetWordBeforeCursor())
Output:

exam

func (*Document) GetWordBeforeCursorUntilSeparator

func (d *Document) GetWordBeforeCursorUntilSeparator(sep string) string

GetWordBeforeCursorUntilSeparator returns the text before the cursor until next separator.

Example
d := &Document{
	Text:           `hello,i am c-bata`,
	cursorPosition: len(`hello,i am c`),
}
fmt.Println(d.GetWordBeforeCursorUntilSeparator(","))
Output:

i am c

func (*Document) GetWordBeforeCursorUntilSeparatorIgnoreNextToCursor

func (d *Document) GetWordBeforeCursorUntilSeparatorIgnoreNextToCursor(sep string) string

GetWordBeforeCursorUntilSeparatorIgnoreNextToCursor returns the word before the cursor. Unlike GetWordBeforeCursor, it returns string containing space

Example
d := &Document{
	Text:           `hello,i am c-bata,thank you for using go-prompt`,
	cursorPosition: len(`hello,i am c-bata,`),
}
fmt.Println(d.GetWordBeforeCursorUntilSeparatorIgnoreNextToCursor(","))
Output:

i am c-bata,

func (*Document) GetWordBeforeCursorWithSpace

func (d *Document) GetWordBeforeCursorWithSpace() string

GetWordBeforeCursorWithSpace returns the word before the cursor. Unlike GetWordBeforeCursor, it returns string containing space

Example
d := &Document{
	Text: `Hello! my name is c-bata.
This is a example of Document component.
`,
	cursorPosition: len(`Hello! my name is c-bata.
This is a example `),
}
fmt.Println(d.GetWordBeforeCursorWithSpace())
Output:

example

func (*Document) LineCount

func (d *Document) LineCount() int

LineCount return the number of lines in this document. If the document ends with a trailing \n, that counts as the beginning of a new line.

func (*Document) Lines

func (d *Document) Lines() []string

Lines returns the array of all the lines.

func (*Document) OnLastLine

func (d *Document) OnLastLine() bool

OnLastLine returns true when we are at the last line.

func (*Document) TextAfterCursor

func (d *Document) TextAfterCursor() string

TextAfterCursor returns the text after the cursor.

Example
d := &Document{
	Text: `Hello! my name is c-bata.
This is a example of Document component.
This component has texts displayed in terminal and cursor position.
`,
	cursorPosition: len(`Hello! my name is c-bata.
This is a exam`),
}
fmt.Println(d.TextAfterCursor())
Output:

ple of Document component.
This component has texts displayed in terminal and cursor position.

func (*Document) TextBeforeCursor

func (d *Document) TextBeforeCursor() string

TextBeforeCursor returns the text before the cursor.

Example
d := &Document{
	Text: `Hello! my name is c-bata.
This is a example of Document component.
This component has texts displayed in terminal and cursor position.
`,
	cursorPosition: len(`Hello! my name is c-bata.
This is a exam`),
}
fmt.Println(d.TextBeforeCursor())
Output:

Hello! my name is c-bata.
This is a exam

func (*Document) TranslateIndexToPosition

func (d *Document) TranslateIndexToPosition(index int) (row int, col int)

TranslateIndexToPosition given an index for the text, return the corresponding (row, col) tuple. (0-based. Returns (0, 0) for index=0.)

func (*Document) TranslateRowColToIndex

func (d *Document) TranslateRowColToIndex(row int, column int) (index int)

TranslateRowColToIndex given a (row, col), return the corresponding index. (Row and col params are 0-based.)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL