editor

package
v0.0.0-...-acee8aa Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoKeyAssociated = FemtoError{
		Message:  "no key associated with pressed key",
		LogLevel: slog.LevelWarn,
	}
	ErrNoCommandFound = FemtoError{
		Message:  "no command found",
		LogLevel: slog.LevelError,
	}
	ErrArgumentMissing = FemtoError{
		Message:  "argument missing",
		LogLevel: slog.LevelError,
	}
	ErrKeyUnparsable = FemtoError{
		Message:  "Cannot parse key",
		LogLevel: slog.LevelError,
	}
	ErrNoWindowFoundForId = FemtoError{
		Message:  "command not found",
		LogLevel: slog.LevelError,
	}
)
View Source
var Commands = map[string]Command{
	"noop": {
		Name: "no operation",
		Func: func(e *Editor, args ...string) error {
			return nil
		},
	},
	"normal": {
		Name: "Normal mode",
		Func: func(e *Editor, args ...string) error {
			e.Win().Mode = "normal"
			return nil
		},
	},
	"quit": {
		Name: "quit editor",
		Func: func(e *Editor, args ...string) error {
			e.Screen.Fini()
			os.Exit(0)
			return nil
		},
	},
	"q": Alias("quit"),
}

Functions

func GracefulPanic

func GracefulPanic(e *Editor)

Panicking without finalizing the screen causes really weird behaviour, So defer this at the top of every main loop function (not for plugins)

Types

type Alignment

type Alignment uint8
const (
	AlignmentLeft   Alignment = 0
	AlignmentRight  Alignment = 1
	AlignmentTop    Alignment = 2
	AlignmentBottom Alignment = 3
	AlignmentCenter Alignment = 4
)

type CharInsertedEvent

type CharInsertedEvent struct {
	Rune rune
	// contains filtered or unexported fields
}

func (*CharInsertedEvent) When

func (c *CharInsertedEvent) When() time.Time

type Command

type Command struct {
	Name        string
	Description string // if empty, takes the Name as Description
	Public      bool   // if public, can be executed in command mode
	Func        CommandFunc
}

func Alias

func Alias(cmd string) Command

type CommandBarEvent

type CommandBarEvent struct {
	Msg   string
	Style tcell.Style
	Time  time.Time
}

func (*CommandBarEvent) When

func (c *CommandBarEvent) When() time.Time

type CommandFunc

type CommandFunc func(e *Editor, args ...string) error

type DumbPlugin

type DumbPlugin struct {
	Info     PluginInfo
	Commands map[string]Command // if it's a third party plugin, please prefix Commands with your plugin id
	Keymap   humankey.HumanKeymap
	Themes   map[string]Theme
}

A plugin that only has a Startup function, and can contribute Commands and Keymap

ideally only use **this**, unless you REALLY need to access the main loop

func (*DumbPlugin) Draw

func (p *DumbPlugin) Draw(e *Editor) error

func (*DumbPlugin) GetInfo

func (p *DumbPlugin) GetInfo() PluginInfo

func (*DumbPlugin) Startup

func (p *DumbPlugin) Startup(e *Editor) error

func (*DumbPlugin) Update

func (p *DumbPlugin) Update(e *Editor, event tcell.Event) tcell.Event

type Editor

type Editor struct {
	Tabs  []Tab
	TabId int

	Keymap   humankey.InternalKeymap
	Commands map[string]Command
	Plugins  []Plugin

	Themes map[string]Theme
	Theme  Theme // for simplicity's (and performance) sake, since we won't change theme often, we don't save the id but just the theme itself here

	Screen             tcell.Screen
	Windows            []Window
	FocusedWindowIndex int // if set to something that isn't -1, overrides the Tab's CurrentWindowId
}

func (*Editor) Buf

func (e *Editor) Buf() buffer.Buffer

func (*Editor) Draw

func (e *Editor) Draw() error

func (*Editor) FocusWindow

func (e *Editor) FocusWindow(id string) error

func (*Editor) GetWindow

func (e *Editor) GetWindow(id string) *Window

func (*Editor) RegisterCommandMap

func (e *Editor) RegisterCommandMap(cmds map[string]Command)

func (*Editor) RegisterKeymap

func (e *Editor) RegisterKeymap(keymap humankey.HumanKeymap) error

func (*Editor) RegisterThemeMap

func (e *Editor) RegisterThemeMap(themes map[string]Theme)

func (*Editor) RegisterWindow

func (e *Editor) RegisterWindow(w Window)

func (*Editor) RunCommand

func (e *Editor) RunCommand(id string, args ...string) error

func (*Editor) Setup

func (e *Editor) Setup()

func (*Editor) Tab

func (e *Editor) Tab() *Tab

func (*Editor) Update

func (e *Editor) Update() error

func (*Editor) Win

func (e *Editor) Win() *Window

type EventCaught

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

func (*EventCaught) When

func (c *EventCaught) When() time.Time

type FemtoError

type FemtoError struct {
	Message  string
	LogLevel slog.Level
}

func (FemtoError) Context

func (f FemtoError) Context(msg string) FemtoError

func (FemtoError) Error

func (f FemtoError) Error() string

type Plugin

type Plugin interface {
	GetInfo() PluginInfo
	Startup(e *Editor) error
	Update(e *Editor, event tcell.Event) tcell.Event // Plugins can hijack the event by returning a new one. Only do this for errors or for catching the event
	Draw(e *Editor) error
}

type PluginInfo

type PluginInfo struct {
	Id          string
	Author      string
	Name        string
	Description string
}

type StyleSection

type StyleSection struct {
	Y      int
	StartX int
	EndX   int
	Style  tcell.Style
}

type Tab

type Tab struct {
	Windows            []Window
	FocusedWindowIndex int
}

func (*Tab) FocusWindow

func (t *Tab) FocusWindow(e *Editor, id string) error

Side effect: this also sets editor's FocusedWindowIndex to -1 if a window is found

func (*Tab) GetWindow

func (t *Tab) GetWindow(id string) *Window

func (*Tab) RegisterWindow

func (t *Tab) RegisterWindow(w Window)

type Theme

type Theme struct {
	Name string

	Default     tcell.Style
	SelectionBG tcell.Color
	Borders     tcell.Color

	Error tcell.Style
	Warn  tcell.Style

	Red       tcell.Color
	Green     tcell.Color
	Yellow    tcell.Color
	Pink      tcell.Color
	Blue      tcell.Color
	LightBlue tcell.Color
	Purple    tcell.Color

	NormalModeAccent tcell.Color
	InsertModeAccent tcell.Color
	VisualModeAccent tcell.Color
}

type Window

type Window struct {
	Id string

	Alignment Alignment
	Size      int
	Priority  int

	Shown bool
	Flags WindowFlags

	// buffer stuff
	Buffer    buffer.Buffer // to implement interactivity, you just need to make a type InteractiveBuffer and runtime check if its that typ
	Selection femath.Range2
	Title     string // used so scratchpads can have a name in the statusbar
	FilePath  string // if left empty, will treat buffer as scratchpad
	Mode      string
	Sequence  []humankey.InternalKey

	StyleSections []StyleSection
	BorderStyle   tcell.Style

	Keymap   humankey.HumanKeymap // keymaps that only work here. override editor global keymap
	Commands map[string]Command   // commands that only work here. overrides editor global commands
}

func (*Window) Draw

func (w *Window) Draw(e *Editor, startX int, startY int, boundX int, boundY int, isFocused bool) error

type WindowFlags

type WindowFlags uint8
const (
	WindowFlagReadonly    WindowFlags = 1
	WindowFlagInteractive WindowFlags = 2
	WindowFlagHasBorder   WindowFlags = 4
	WindowFlagUnfocusable WindowFlags = 8
)

Jump to

Keyboard shortcuts

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