editor

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2026 License: MIT Imports: 17 Imported by: 0

README

editor

import "github.com/lucasassuncao/yedit/editor"

Package editor provides the bubbletea TUI for editing a YAML file driven by a struct-based schema and a preset source.

Index

func Run

func Run(cfg Config) error

Run starts the editor TUI and blocks until the user quits. The Config must have Schema and Path set; everything else is optional.

Returns nil on a clean quit, or the underlying tea.Program error.

func RunAll

func RunAll(validators []Validator, raw []byte, blocks []document.Block) []string

RunAll executes all validators against raw/blocks and collects violations.

type Config

Config bundles everything the editor needs from the embedding application.

Schema must be a pointer to the Go type describing the YAML document's top level (e.g. &MyConfig{}). The editor introspects it through yedit/schema.

Presets is optional — when nil the editor opens fresh blocks with a minimal "<key>:\n" template and the preset picker is disabled.

Validators run before every save and on the explicit "validate" shortcut. Use editor.MutuallyExclusive and editor.RequiredWith for the common cases.

PreCheckedFields lists which sub-fields of a parent key start checked when the overlay opens. Keyed by top-level yaml name (e.g. "build" → ["dockerfile","context"]).

FieldSnippets provides the indented YAML chunk inserted when the user toggles a sub-field on (keyed by parent key → child yaml name → snippet). When a snippet is missing, the editor falls back to "<child>: \n".

FieldExamples provides a YAML snippet shown in the hint panel for each field (keyed by block yaml name → field yaml name → snippet). When absent the editor falls back to the "base" preset for that block, if one exists.

type Config struct {
    Path             string
    Schema           any
    Title            string
    Presets          presets.Source
    Validators       []Validator
    PreCheckedFields map[string][]string
    FieldSnippets    map[string]map[string]string
    FieldExamples    map[string]map[string]string
    Hidden           []string // additional top-level keys to omit from the UI
}

type Validator

Validator is a pluggable rule executed at validate/save time. It returns human-readable messages for every violation it finds. Returning an empty slice (or nil) means "all good".

type Validator interface {
    Validate(raw []byte, blocks []document.Block) []string
}

func MutuallyExclusive
func MutuallyExclusive(keys ...string) Validator

MutuallyExclusive reports a violation when more than one of the listed keys is present in the document.

func RequiredWith
func RequiredWith(key, parent string) Validator

RequiredWith reports a violation when key is present but parent is not.

type ValidatorFunc

ValidatorFunc adapts a plain function to the Validator interface, letting callers register inline validators without defining a named type:

editor.Run(editor.Config{
    Validators: []editor.Validator{
        editor.ValidatorFunc(func(raw []byte, blocks []document.Block) []string {
            // custom rule ...
            return nil
        }),
    },
})
type ValidatorFunc func(raw []byte, blocks []document.Block) []string

func (ValidatorFunc) Validate
func (f ValidatorFunc) Validate(raw []byte, blocks []document.Block) []string

Validate calls f.

Generated by gomarkdoc

Documentation

Overview

Package editor provides the bubbletea TUI for editing a YAML file driven by a struct-based schema and a preset source.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(cfg Config) error

Run starts the editor TUI and blocks until the user quits. The Config must have Schema and Path set; everything else is optional.

Returns nil on a clean quit, or the underlying tea.Program error.

func RunAll added in v0.2.1

func RunAll(validators []Validator, raw []byte, blocks []document.Block) []string

RunAll executes all validators against raw/blocks and collects violations.

Types

type Config

type Config struct {
	Path             string
	Schema           any
	Title            string
	Presets          presets.Source
	Validators       []Validator
	PreCheckedFields map[string][]string
	FieldSnippets    map[string]map[string]string
	FieldExamples    map[string]map[string]string
	Hidden           []string // additional top-level keys to omit from the UI
}

Config bundles everything the editor needs from the embedding application.

Schema must be a pointer to the Go type describing the YAML document's top level (e.g. &MyConfig{}). The editor introspects it through yedit/schema.

Presets is optional — when nil the editor opens fresh blocks with a minimal "<key>:\n" template and the preset picker is disabled.

Validators run before every save and on the explicit "validate" shortcut. Use editor.MutuallyExclusive and editor.RequiredWith for the common cases.

PreCheckedFields lists which sub-fields of a parent key start checked when the overlay opens. Keyed by top-level yaml name (e.g. "build" → ["dockerfile","context"]).

FieldSnippets provides the indented YAML chunk inserted when the user toggles a sub-field on (keyed by parent key → child yaml name → snippet). When a snippet is missing, the editor falls back to "<child>: \n".

FieldExamples provides a YAML snippet shown in the hint panel for each field (keyed by block yaml name → field yaml name → snippet). When absent the editor falls back to the "base" preset for that block, if one exists.

type Validator

type Validator interface {
	Validate(raw []byte, blocks []document.Block) []string
}

Validator is a pluggable rule executed at validate/save time. It returns human-readable messages for every violation it finds. Returning an empty slice (or nil) means "all good".

func MutuallyExclusive

func MutuallyExclusive(keys ...string) Validator

MutuallyExclusive reports a violation when more than one of the listed keys is present in the document.

func RequiredWith

func RequiredWith(key, parent string) Validator

RequiredWith reports a violation when key is present but parent is not.

type ValidatorFunc

type ValidatorFunc func(raw []byte, blocks []document.Block) []string

ValidatorFunc adapts a plain function to the Validator interface, letting callers register inline validators without defining a named type:

editor.Run(editor.Config{
    Validators: []editor.Validator{
        editor.ValidatorFunc(func(raw []byte, blocks []document.Block) []string {
            // custom rule ...
            return nil
        }),
    },
})

func (ValidatorFunc) Validate

func (f ValidatorFunc) Validate(raw []byte, blocks []document.Block) []string

Validate calls f.

Jump to

Keyboard shortcuts

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