jsoneditor

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: Apache-2.0 Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package jsoneditor is a small bubbletea component that edits a JSON document and shows a syntax-highlighted preview before committing. It is shared by the `cinc <noun> edit` commands (via Run, which drives it as a standalone program) and by the `cinc explore` TUI (which embeds the Model as a screen).

The editor is JSON-aware: by default it presents a structural cursor (Model in model.go) that selects and edits keys, scalar values, and whole {}/[] blocks, with a raw free-text mode a Tab away.

cinc ships its own editor rather than shelling out to $VISUAL / $EDITOR so the experience is consistent across systems and works in minimal environments without an editor on PATH.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoTerminal = errors.New("we couldn't open the editor because there's no terminal here. " +
	"Pass --file with the updated JSON instead")

ErrNoTerminal is returned by Run (and the node-edit form) when there is no terminal to draw the editor on, as in a script or a CI job. Every command that opens the editor also takes --file, so the error says so.

View Source
var HaveTerminal = func() bool {
	if term.IsTerminal(int(os.Stdin.Fd())) {
		return true
	}
	name := "/dev/tty"
	if runtime.GOOS == "windows" {
		name = "CONIN$"
	}
	f, err := os.Open(name)
	if err != nil {
		return false
	}
	_ = f.Close()
	return true
}

HaveTerminal reports whether an interactive editor can run: stdin is a terminal, or the process has a controlling terminal it can open, which is where bubbletea reads keys from when stdin is redirected. It is a package variable so tests can pretend either way.

Functions

func Run

func Run(initial []byte, validate func([]byte) error) ([]byte, error)

Run drives the editor as a standalone full-screen program and returns the committed JSON. It is used by the `cinc <noun> edit` commands.

Types

type Model

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

Model is the editor state. It is an embeddable bubbletea component: call New, then forward Update/View to it, and check Finished after each Update to learn when the user has committed or aborted. It does not quit the surrounding program — embedding code decides what to do when Finished reports true.

By default it opens in structural mode: a JSON-aware cursor selects a key, a scalar value, or a whole object/array block (highlighted as a unit) and edits it in place. Tab drops to the raw free-text textarea for bulk edits, and Tab returns to structural mode.

func New

func New(initial []byte, validate func([]byte) error) Model

New returns an editor seeded with initial. validate is invoked on each save attempt; returning a non-nil error keeps the user editing with the message shown inline (no work lost). If initial is not valid JSON the editor opens in raw mode so the user can repair it.

func (Model) Aborted

func (m Model) Aborted() bool

Aborted reports whether the user abandoned the edit.

func (Model) Committed

func (m Model) Committed() []byte

Committed returns the canonical JSON the user confirmed, or nil if the edit was aborted or is still in progress.

func (Model) ContentView

func (m Model) ContentView() string

ContentView renders just the editor body for the current state, without the header line. Embedding code (e.g. the node-edit form) uses it to place the editor under its own label instead of the standalone header.

func (Model) Editing

func (m Model) Editing() bool

Editing reports whether the editor is in a transient sub-edit (editing a key/scalar inline or a whole block). Embedding code uses this to decide whether Esc should cancel that sub-edit or back out of the editor.

func (Model) Finished

func (m Model) Finished() bool

Finished reports whether the user has committed or aborted.

func (Model) Init

func (m Model) Init() tea.Cmd

func (*Model) SetSize

func (m *Model) SetSize(width, height int)

SetSize resizes the editor to fit the given terminal dimensions, reserving a few lines for the header.

func (Model) Update

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)

Update advances the editor. It returns the concrete Model (not tea.Model) so embedding code can call Finished/Committed without a type assertion.

func (Model) Value

func (m Model) Value() []byte

Value returns the document's current canonical JSON, reflecting in-progress edits without requiring a commit. It lets embedding code (e.g. a larger form) read the live document at any time. In raw mode it canonicalizes the textarea contents, falling back to the raw bytes if they are not yet valid JSON.

func (Model) View

func (m Model) View() string

Jump to

Keyboard shortcuts

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