138 lines
4.2 KiB
Go
138 lines
4.2 KiB
Go
package editor
|
|
|
|
import (
|
|
"git.gophernest.net/azpect/TextEditor/internal/core"
|
|
"git.gophernest.net/azpect/TextEditor/internal/input"
|
|
"git.gophernest.net/azpect/TextEditor/internal/style"
|
|
"github.com/alecthomas/chroma/v2/styles"
|
|
)
|
|
|
|
type ModelBuilder struct {
|
|
model Model
|
|
}
|
|
|
|
// NewModelBuilder: Builds and returns a new model, using the default color scheme (kanagawa-wave).
|
|
func NewModelBuilder() *ModelBuilder {
|
|
chromaStyle := styles.Get("kanagawa-wave")
|
|
|
|
return &ModelBuilder{
|
|
model: Model{
|
|
buffers: []*core.Buffer{},
|
|
windows: []*core.Window{},
|
|
activeWindowId: -1,
|
|
mode: core.NormalMode,
|
|
termWidth: 0,
|
|
termHeight: 0,
|
|
input: input.NewHandler(),
|
|
insertCount: 0,
|
|
insertKeys: []string{},
|
|
insertAction: nil,
|
|
command: "",
|
|
commandCursor: 0,
|
|
commandOutput: nil,
|
|
settings: core.NewDefaultSettings(),
|
|
registers: core.DefaultRegisters(),
|
|
styles: style.ChromaStyles(chromaStyle),
|
|
},
|
|
}
|
|
}
|
|
|
|
// ModelBuilder.WithBuffers: Set the buffers for the model. Buffers represent
|
|
// the in-memory text content of files being edited.
|
|
func (mb *ModelBuilder) WithBuffers(buffers []*core.Buffer) *ModelBuilder {
|
|
mb.model.buffers = buffers
|
|
return mb
|
|
}
|
|
|
|
// ModelBuilder.AddBuffer: Add a single buffer to the model's buffer list.
|
|
func (mb *ModelBuilder) AddBuffer(buffer *core.Buffer) *ModelBuilder {
|
|
mb.model.buffers = append(mb.model.buffers, buffer)
|
|
return mb
|
|
}
|
|
|
|
// ModelBuilder.WithWindows: Set the windows for the model. Windows are viewports
|
|
// that display buffer content with their own cursor position and scroll state.
|
|
func (mb *ModelBuilder) WithWindows(windows []*core.Window) *ModelBuilder {
|
|
mb.model.windows = windows
|
|
return mb
|
|
}
|
|
|
|
// ModelBuilder.AddWindow: Add a single window to the model's window list.
|
|
func (mb *ModelBuilder) AddWindow(window *core.Window) *ModelBuilder {
|
|
mb.model.windows = append(mb.model.windows, window)
|
|
return mb
|
|
}
|
|
|
|
// ModelBuilder.WithActiveWindowId: Set the ID of the currently active window.
|
|
// This determines which window receives input and displays the cursor.
|
|
func (mb *ModelBuilder) WithActiveWindowId(id int) *ModelBuilder {
|
|
mb.model.activeWindowId = id
|
|
return mb
|
|
}
|
|
|
|
// ModelBuilder.WithMode: Set the editor mode (Normal, Insert, Visual, etc).
|
|
func (mb *ModelBuilder) WithMode(mode core.Mode) *ModelBuilder {
|
|
mb.model.mode = mode
|
|
return mb
|
|
}
|
|
|
|
// ModelBuilder.WithTermSize: Set the terminal dimensions in columns and rows.
|
|
func (mb *ModelBuilder) WithTermSize(width, height int) *ModelBuilder {
|
|
mb.model.termWidth = width
|
|
mb.model.termHeight = height
|
|
return mb
|
|
}
|
|
|
|
// ModelBuilder.WithTermWidth: Set the terminal width in columns.
|
|
func (mb *ModelBuilder) WithTermWidth(width int) *ModelBuilder {
|
|
mb.model.termWidth = width
|
|
return mb
|
|
}
|
|
|
|
// ModelBuilder.WithTermHeight: Set the terminal height in rows.
|
|
func (mb *ModelBuilder) WithTermHeight(height int) *ModelBuilder {
|
|
mb.model.termHeight = height
|
|
return mb
|
|
}
|
|
|
|
// ModelBuilder.WithSettings: Set the editor settings (tabstop, scrolloff, etc).
|
|
func (mb *ModelBuilder) WithSettings(settings core.EditorSettings) *ModelBuilder {
|
|
mb.model.settings = settings
|
|
return mb
|
|
}
|
|
|
|
// ModelBuilder.WithRegisters: Set the register map for yank/delete/paste operations.
|
|
func (mb *ModelBuilder) WithRegisters(registers map[rune]core.Register) *ModelBuilder {
|
|
mb.model.registers = registers
|
|
return mb
|
|
}
|
|
|
|
// ModelBuilder.WithCommand: Set the command line text.
|
|
func (mb *ModelBuilder) WithCommand(command string) *ModelBuilder {
|
|
mb.model.command = command
|
|
return mb
|
|
}
|
|
|
|
// ModelBuilder.WithCommandCursor: Set the cursor position in the command line.
|
|
func (mb *ModelBuilder) WithCommandCursor(cursor int) *ModelBuilder {
|
|
mb.model.commandCursor = cursor
|
|
return mb
|
|
}
|
|
|
|
// ModelBuilder.WithCommandOutput: Set the command line output.
|
|
func (mb *ModelBuilder) WithCommandOutput(out *core.CommandOutput) *ModelBuilder {
|
|
mb.model.commandOutput = out
|
|
return mb
|
|
}
|
|
|
|
// ModelBuilder.WithStyles: Set the visual styling for the editor.
|
|
func (mb *ModelBuilder) WithStyles(styles style.Styles) *ModelBuilder {
|
|
mb.model.styles = styles
|
|
return mb
|
|
}
|
|
|
|
// ModelBuilder.Build: Build and return the configured Model instance.
|
|
func (mb *ModelBuilder) Build() *Model {
|
|
return &mb.model
|
|
}
|