edit

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LineUp         = "\x1b[1A"
	LineClear      = "\x1b[2K"
	NewLine        = '\n'
	ReturnCarriege = "\r"
	Backspace      = "\b"
)
View Source
const (
	PastingTimingThresholdInMicrosec = 250
	MacOSDeleteKey                   = 127
	Bell                             = "\a"
)
View Source
const (
	HistoryFileRigths = 0o644
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Content

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

func NewContent

func NewContent() *Content

NewContent returns a new instance of Content with empty text and position set to 0.

func (*Content) Clear

func (c *Content) Clear() string

Clear clears the content and returns the string representation of the cleared content. If the content is already empty, it returns an empty string.

func (*Content) GetLinesAfterPosition

func (c *Content) GetLinesAfterPosition(pos int) (startOfLine int, lines []string)

GetLinesAfterPosition returns the start index of the line containing the given position and a slice of strings representing the lines after the given position. If the position is before the first line, the start index is 0.

func (*Content) InsertSymbol

func (c *Content) InsertSymbol(symbol rune) string

InsertSymbol inserts a rune at the current position of the Content object. If the position is invalid, it returns an empty string. If the inserted symbol is not a newline and the next character is a newline, it returns the inserted symbol. If the inserted symbol is a newline, it returns the content of the lines affected by the insertion, with the cursor moved to the beginning of the next line.

func (*Content) MovePositionLeft

func (c *Content) MovePositionLeft() string

MovePositionLeft moves the cursor position one character to the left and returns the ANSI escape sequence required to move the cursor to the new position. If the cursor is already at the beginning of the line, it moves the cursor to the end of the previous line. If the cursor is already at the beginning of the content, it returns an empty string.

func (*Content) MovePositionRight

func (c *Content) MovePositionRight() string

MovePositionRight moves the position of the cursor to the right by one character in the content. If the position is already at the end of the content, it returns an empty string.

func (*Content) MoveToEnd

func (c *Content) MoveToEnd() string

MoveToEnd moves the cursor to the end of the content and returns the remaining text. If the cursor is already at the end, it returns an empty string.

func (*Content) PrevSymbol

func (c *Content) PrevSymbol() rune

PrevSymbol returns the symbol before the current position in the content text. If the current position is at the beginning of the text, it returns 0.

func (*Content) RemoveSymbol

func (c *Content) RemoveSymbol() string

RemoveSymbol removes the symbol at the current position of the Content object and returns the string representation of the changes made. If the current position is out of bounds, an empty string is returned. If the removed symbol is not a newline character, the function returns the string representation of the changes made, including clearing the current line and moving the cursor to the beginning of the line. If the removed symbol is a newline character, the function returns the string representation of the changes made, including moving the cursor up one line and clearing the current line.

func (*Content) ReplaceText

func (c *Content) ReplaceText(text string) string

ReplaceText replaces the current text with the given text and returns the resulting string.

func (*Content) String

func (c *Content) String() string

String returns the string representation of the Content.

func (*Content) ToRequest

func (c *Content) ToRequest() string

ToRequest returns the content as a trimmed string.

type Dictionary

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

func NewDictionary

func NewDictionary(words []string) *Dictionary

NewDictionary creates a new instance of Dictionary with the given list of words. The words are sorted in ascending order before being stored in the dictionary. It returns a pointer to the created Dictionary.

func (*Dictionary) Search

func (d *Dictionary) Search(prefix string) string

Search searches for words in the dictionary that have the given prefix. It performs a search to find all matching words. The function returns the longest common prefix among the matching words.

type Editor

type Editor struct {
	History *History

	Dictionary *Dictionary
	// contains filtered or unexported fields
}

func NewEditor

func NewEditor(output io.Writer, history *History, isSingleLine bool) *Editor

NewEditor creates a new instance of Editor struct. It takes an io.Writer to output the editor content, a *History to store the command history, a boolean value to indicate whether the editor should be single line or not. It returns a pointer to the created Editor struct.

func (*Editor) Close

func (ed *Editor) Close() error

Close saves the history to the history file.

func (*Editor) Edit

func (ed *Editor) Edit(keyStream <-chan keyboard.KeyEvent, initBuffer string) (string, error)

Edit reads input from the user via a keyboard stream and returns the resulting string. It takes a channel of keyboard events and an initial buffer string as input. It returns the resulting string and an error if any.

type History

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

func NewHistory

func NewHistory(fileName string, limit uint) *History

NewHistory creates a new History instance with the given file name and limit. The History instance stores a list of requests made by the user and loads them from the file if it exists. The limit parameter specifies the maximum number of requests to store in the history.

func (*History) AddRequest

func (h *History) AddRequest(request string)

AddRequest adds a request to the history. If the request is empty or the same as the last request, it will not be added.

func (*History) NextRequst

func (h *History) NextRequst() string

NextRequest returns the next request in the history. If there are no more requests, it returns an empty string.

func (*History) PrevRequst

func (h *History) PrevRequst() string

PrevRequst returns the previous request in the history. If there are no more previous requests, it returns an empty string.

func (*History) ResetPosition

func (h *History) ResetPosition()

ResetPosition resets the position of the history to the end. If the history is empty, it does nothing.

func (*History) SaveToFile

func (h *History) SaveToFile() error

SaveToFile saves the history to a file. It opens the file with the given filename and writes the history requests to it. If the file does not exist, it creates it. If the number of requests is greater than the limit, it writes only the last limit requests. It replaces newlines with the escape sequence "\\n". It returns an error if it fails to open the file or write to it.

Jump to

Keyboard shortcuts

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