edit

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LineUp         = "\x1b[1A"
	LineClear      = "\x1b[2K"
	NewLine        = '\n'
	ReturnCarriage = "\r"
	Backspace      = "\b"
)
View Source
const (
	PastingTimingThresholdInMicrosec = 250
	MacOSDeleteKey                   = 127
	Bell                             = "\a"
)
View Source
const (
	HideCursor = "\x1b[?25l"
	ShowCursor = "\x1b[?25h"
)

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) DeleteToNextWord

func (c *Content) DeleteToNextWord() string

DeleteToNextWord deletes characters from the current position to the beginning of the next word in the content.

func (*Content) DeleteToPrevWord

func (c *Content) DeleteToPrevWord() string

DeleteToPrevWord deletes characters from the current position to the beginning of current or previous word in the content.

func (*Content) GetCurrentWord added in v0.7.0

func (c *Content) GetCurrentWord() string

GetCurrentWord retrieves the word currently under the cursor position. It returns a string representing the word or an empty string if the position is at the start.

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) GetPosition added in v0.7.0

func (c *Content) GetPosition() int

GetPosition retrieves the current position value of the Content. It takes no parameters. It returns an int representing the current position.

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) MoveToNextWord

func (c *Content) MoveToNextWord() string

MoveToNextWord moves the cursor to the beginning of the next word in the content and returns the word.

func (*Content) MoveToPosition added in v0.7.0

func (c *Content) MoveToPosition(pos int) string

MoveToPosition moves the current position to the specified index and returns the resultant output string. It takes pos of type int, which is the target position relative to the content length. It returns a string detailing the changes made while adjusting the position. If pos is less than 0, it defaults to 0. If pos exceeds the content length, it defaults to the content's length.

func (*Content) MoveToPrevWord

func (c *Content) MoveToPrevWord() string

MoveToPrevWord moves the cursor to the beginning of the previous word in the content and returns the word.

func (*Content) MoveToRowEnd

func (c *Content) MoveToRowEnd() string

MoveToRowEnd moves the position within the text to the end of the current row. It returns the substring from the initial position to the end of the row. If the current position is already at or beyond the end of the text, it returns an empty string.

func (*Content) MoveToRowStart

func (c *Content) MoveToRowStart() string

MoveToRowStart moves the cursor position to the start of the current row. It updates the cursor position and returns a string of backspace characters needed to move the cursor to the start of the row. It takes no parameters and returns a string. If the cursor is already at the start of the row (position 0), 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) RemoveNextSymbol

func (c *Content) RemoveNextSymbol() string

RemoveNextSymbol removes the symbol at the current position in the content's text and returns a string representing the updated text or necessary control characters for display. It takes no parameters. It returns a string which is the updated text or control characters for display. If the current position is out of bounds, it returns an empty string. If the symbol at the current position is a newline, it handles the line break and returns the appropriate control characters to update the display.

func (*Content) RemovePrevSymbol

func (c *Content) RemovePrevSymbol() string

RemovePrevSymbol removes the previous symbol from the content's text at the current position. It adjusts the position and updates the text accordingly. It returns a string representing the changes to be displayed. If the current position is out of bounds, it returns an empty string. If the removed symbol is not a newline, it returns a string to clear and update the current line. If the removed symbol is a newline, it returns a string to clear and update the previous line(s).

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 Editor

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

func NewEditor

func NewEditor(output io.Writer, history HistoryRepo, isSingleLine bool, opts ...Option) *Editor

NewEditor initializes a new instance of Editor for text editing tasks. It takes output of type io.Writer for writing, history of type HistoryRepo for request history, and isSingleLine of type bool to specify single-line mode. It returns a pointer to an initialized Editor structure.

func (*Editor) Edit

func (ed *Editor) Edit(ctx context.Context, initBuffer string) (res string, err error)

Edit processes keyboard input to manipulate and return the edited content. It takes a context ctx of type context.Context for cancellation and an initial buffer initBuffer of type string. It returns the final edited string content or an error if input is unavailable, keyboard stream is closed, or an interrupt occurs.

func (*Editor) SetInput

func (ed *Editor) SetInput(input <-chan core.KeyEvent)

SetInput sets the input channel for the Editor instance to process keyboard events. It takes a single parameter input of type <-chan core.KeyEvent. This method does not return any value.

type HistoryRepo

type HistoryRepo interface {
	AddRequest(req string)
	PrevRequest() string
	NextRequest() string
	ResetPosition()
	Search(prefix string) string
}

type MockHistoryRepo

type MockHistoryRepo struct {
	mock.Mock
}

MockHistoryRepo is an autogenerated mock type for the HistoryRepo type

func NewMockHistoryRepo

func NewMockHistoryRepo(t interface {
	mock.TestingT
	Cleanup(func())
}) *MockHistoryRepo

NewMockHistoryRepo creates a new instance of MockHistoryRepo. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockHistoryRepo) AddRequest

func (_m *MockHistoryRepo) AddRequest(req string)

AddRequest provides a mock function with given fields: req

func (*MockHistoryRepo) EXPECT

func (*MockHistoryRepo) NextRequest

func (_m *MockHistoryRepo) NextRequest() string

NextRequest provides a mock function with no fields

func (*MockHistoryRepo) PrevRequest

func (_m *MockHistoryRepo) PrevRequest() string

PrevRequest provides a mock function with no fields

func (*MockHistoryRepo) ResetPosition

func (_m *MockHistoryRepo) ResetPosition()

ResetPosition provides a mock function with no fields

func (*MockHistoryRepo) Search added in v0.7.0

func (_m *MockHistoryRepo) Search(prefix string) string

Search provides a mock function with given fields: prefix

type MockHistoryRepo_AddRequest_Call

type MockHistoryRepo_AddRequest_Call struct {
	*mock.Call
}

MockHistoryRepo_AddRequest_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'AddRequest'

func (*MockHistoryRepo_AddRequest_Call) Return

func (*MockHistoryRepo_AddRequest_Call) Run

func (*MockHistoryRepo_AddRequest_Call) RunAndReturn

type MockHistoryRepo_Expecter

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

func (*MockHistoryRepo_Expecter) AddRequest

func (_e *MockHistoryRepo_Expecter) AddRequest(req interface{}) *MockHistoryRepo_AddRequest_Call

AddRequest is a helper method to define mock.On call

  • req string

func (*MockHistoryRepo_Expecter) NextRequest

NextRequest is a helper method to define mock.On call

func (*MockHistoryRepo_Expecter) PrevRequest

PrevRequest is a helper method to define mock.On call

func (*MockHistoryRepo_Expecter) ResetPosition

ResetPosition is a helper method to define mock.On call

func (*MockHistoryRepo_Expecter) Search added in v0.7.0

func (_e *MockHistoryRepo_Expecter) Search(prefix interface{}) *MockHistoryRepo_Search_Call

Search is a helper method to define mock.On call

  • prefix string

type MockHistoryRepo_NextRequest_Call

type MockHistoryRepo_NextRequest_Call struct {
	*mock.Call
}

MockHistoryRepo_NextRequest_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'NextRequest'

func (*MockHistoryRepo_NextRequest_Call) Return

func (*MockHistoryRepo_NextRequest_Call) Run

func (*MockHistoryRepo_NextRequest_Call) RunAndReturn

type MockHistoryRepo_PrevRequest_Call

type MockHistoryRepo_PrevRequest_Call struct {
	*mock.Call
}

MockHistoryRepo_PrevRequest_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'PrevRequest'

func (*MockHistoryRepo_PrevRequest_Call) Return

func (*MockHistoryRepo_PrevRequest_Call) Run

func (*MockHistoryRepo_PrevRequest_Call) RunAndReturn

type MockHistoryRepo_ResetPosition_Call

type MockHistoryRepo_ResetPosition_Call struct {
	*mock.Call
}

MockHistoryRepo_ResetPosition_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ResetPosition'

func (*MockHistoryRepo_ResetPosition_Call) Return

func (*MockHistoryRepo_ResetPosition_Call) Run

func (*MockHistoryRepo_ResetPosition_Call) RunAndReturn

type MockHistoryRepo_Search_Call added in v0.7.0

type MockHistoryRepo_Search_Call struct {
	*mock.Call
}

MockHistoryRepo_Search_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Search'

func (*MockHistoryRepo_Search_Call) Return added in v0.7.0

func (*MockHistoryRepo_Search_Call) Run added in v0.7.0

func (*MockHistoryRepo_Search_Call) RunAndReturn added in v0.7.0

type MultiMode

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

func NewMultiMode

func NewMultiMode(output io.Writer, reqHistory, cmdHistory HistoryRepo) *MultiMode

NewMultiMode initializes a new MultiMode structure with separate editors for command and standard input modes. It takes an io.Writer, two HistoryRepo instances for request and command histories, and an optional Dictionary. It returns a pointer to the created MultiMode, setting up command and edit modes appropriately.

func (*MultiMode) CommandMode

func (m *MultiMode) CommandMode(ctx context.Context, initBuffer string) (string, error)

CommandMode activates the command mode, reading user input from keyStream with an initial buffer initBuffer. It returns the resulting command string or an error if any issue occurs.

func (*MultiMode) Edit

func (m *MultiMode) Edit(ctx context.Context, initBuffer string) (string, error)

Edit switches the editor to edit mode, processing user input from keyStream with an initial buffer. It returns the final string after editing or an error if an issue occurs.

func (*MultiMode) SetInput

func (m *MultiMode) SetInput(input <-chan core.KeyEvent)

SetInput sets the input channel for both command and edit modes.

type Option added in v0.7.0

type Option func(*Editor)

func WithCloseHook added in v0.7.0

func WithCloseHook(hook func(io.Writer) error) Option

WithCloseHook sets the onClose function for the Editor instance. It takes a function hook of type func(io.Writer) error. It returns an Option function to set the onClose function.

func WithOpenHook added in v0.7.0

func WithOpenHook(hook func(io.Writer) error) Option

WithOpenHook sets the onOpen function for the Editor instance. It takes a function hook of type func(io.Writer) error. It returns an Option function to set the onOpen function.

Jump to

Keyboard shortcuts

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