edit

package
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 12 Imported by: 0 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 edit is how a gist is changed, shared by the CLI and the TUI: it holds the gist's lock, knows the revision a change was based on, keeps an edit as a draft until it is sent, and checks the gist did not move underneath it.

What can be promised is limited by the API, which takes no condition on a PATCH. A change made elsewhere between the start of an edit and the check just before sending is always found. One made between that check and the PATCH is not prevented, but it is found afterwards and reported, and the revision it made stays on GitHub. Changing just the description makes no revision, so it is compared by its text, and one overwritten in that last moment is lost.

Index

Constants

This section is empty.

Variables

View Source
var ErrDraftsLeft = errors.New("the drafts of the deleted gist cannot be removed")

ErrDraftsLeft says a gist was deleted but its drafts could not be.

View Source
var ErrNoFile = errors.New("no such file in the gist")

ErrNoFile says the gist has no such file.

Functions

func Command

func Command(configured string, paths ...string) []string

Command is the program to edit paths with, and its arguments: the editor set in the config, or $VISUAL, $EDITOR, then vi. It is split on spaces, so `code -w` works.

Types

type API

type API interface {
	Get(ctx context.Context, id string) (model.Gist, error)
	Raw(ctx context.Context, f model.File) (string, error)
	Update(ctx context.Context, id string, ch model.Change) (model.Gist, error)
	Delete(ctx context.Context, id string) error
}

API is what editing asks of GitHub.

type ChangedError

type ChangedError struct {
	// Current is the gist as it is now.
	Current model.Gist
	// What names what moved: "revision" or "description".
	What string
}

ChangedError says the gist on GitHub is not what the change was based on.

func (*ChangedError) Error

func (e *ChangedError) Error() string

type Editor

type Editor struct {
	API   API
	State *state.Store
	// Now is the time, for tests; nil uses the clock.
	Now func() time.Time
}

Editor changes gists.

func (*Editor) Apply

func (e *Editor) Apply(ctx context.Context, r Request) (Result, error)

Apply makes a change under the gist's lock.

func (*Editor) Begin

func (e *Editor) Begin(ctx context.Context, gist string, files ...string) (*Session, error)

Begin starts editing files, each from its draft if it has one. The gist's lock is held until Close: ErrLocked says another process has it.

func (*Editor) Delete added in v2.1.0

func (e *Editor) Delete(ctx context.Context, gist string) (err error)

Delete deletes a gist under its lock, and its drafts with it once GitHub has deleted it. Any answer but that keeps them, not found among them: a token without the permission gets that for a gist that is still there, and a draft can be the only copy of an edit.

func (*Editor) Discard

func (e *Editor) Discard(gist, file string) error

Discard removes the draft of a file, under the gist's lock: ErrLocked says another process is editing the gist.

func (*Editor) Draft

func (e *Editor) Draft(gist, file string) (state.Draft, bool, error)

Draft reads the draft kept for a file.

type FileEdit

type FileEdit struct {
	// Name is the file's name.
	Name string
	// Revision and Original are the base: what the edit started from. A
	// refresh of the list does not change them.
	Revision string
	Original string
	// Content is what to edit: the original, or the draft of an earlier edit.
	Content string
	// FromDraft says Content came from a draft.
	FromDraft bool
	// contains filtered or unexported fields
}

FileEdit is a file being edited.

func (*FileEdit) Changed

func (f *FileEdit) Changed() bool

Changed says the edit differs from what it started from.

type KeptAtError

type KeptAtError struct {
	Dir string
	Err error
}

KeptAtError says an edit could not be kept as a draft and its files are left on disk, so it is not lost.

func (*KeptAtError) Error

func (e *KeptAtError) Error() string

func (*KeptAtError) Unwrap

func (e *KeptAtError) Unwrap() error

type Request

type Request struct {
	Gist string
	// Base, when not "", is the revision the change must be based on.
	Base string
	// BaseDescription, when not nil, is the description it must be based on.
	BaseDescription *string
	// Build makes the change from the gist as it is. An empty one sends
	// nothing.
	Build func(ctx context.Context, g model.Gist) (model.Change, error)
}

Request is a change made without an editor.

type Result

type Result struct {
	// Gist is the gist after the change.
	Gist model.Gist
	// Sent says something was sent; not when there was nothing to change.
	Sent bool
	// Overwritten is the revision of a change made elsewhere that this one
	// went over, found only after sending, or "".
	Overwritten string
}

Result is what a change did.

type Scratch

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

Scratch is a file for an editor to write what has no gist yet.

func NewScratch

func NewScratch(name, content string) (*Scratch, error)

NewScratch writes content to a file of this name, in a directory of its own that only the user can read.

func (*Scratch) Path

func (s *Scratch) Path() string

Path is the file.

func (*Scratch) Read

func (s *Scratch) Read() (string, error)

Read reads what the editor left, and removes it unless that could not be read. That it could not be removed is no reason to lose what was read.

func (*Scratch) Remove

func (s *Scratch) Remove() error

Remove removes it unread.

type Session

type Session struct {
	// Files are the files being edited.
	Files []*FileEdit
	// contains filtered or unexported fields
}

Session is the editing of a gist's files: from taking the lock until they are sent or given up. Close lets the lock go.

func (*Session) Changed

func (s *Session) Changed() []*FileEdit

Changed are the files that differ from what they started from.

func (*Session) Close

func (s *Session) Close() error

Close lets the lock go. Drafts stay.

func (*Session) DropDrafts

func (s *Session) DropDrafts(ctx context.Context) error

DropDrafts throws away the drafts the session started from, and starts those files again from GitHub.

func (*Session) File

func (s *Session) File(name string) *FileEdit

File is the file being edited with this name, or nil.

func (*Session) Keep

func (s *Session) Keep(name, content string) (bool, error)

Keep records content as the draft of a file, if it differs from the base, and returns whether it does. When it does not, there is nothing to send of it and no draft is left. Comparing with the base rather than the draft means a draft opened and closed unchanged still goes on to be sent.

func (*Session) Push

func (s *Session) Push(ctx context.Context, overwrite bool) (Result, error)

Push sends the content kept of the files changed, in one change, unless the gist has moved on from what one of them is based on, which ChangedError says. With overwrite it goes over what is there. Their drafts go once it is sent.

func (*Session) Reload

func (s *Session) Reload(ctx context.Context) (bool, error)

Reload takes the gist as it is now as the new base of every file, keeping the edits, and returns whether any still differs from it. It may not: the same change may have been sent from elsewhere.

func (*Session) Workspace

func (s *Session) Workspace() (*Workspace, error)

Workspace writes the content of the files to a directory of their own, which only the user can read. Names that differ only in case are one file on the disks of macOS and Windows, so all but the first of them go a directory down.

type Workspace

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

Workspace is the files of a session on disk, for an editor: each under its real name, so the editor highlights it.

func (*Workspace) Paths

func (w *Workspace) Paths() []string

Paths are the files, in the order of the session.

func (*Workspace) ReadBack

func (w *Workspace) ReadBack(s *Session) (bool, error)

ReadBack reads the files the editor left, whatever way it ended, and keeps each as a draft. Only then are they removed: until the edit is in a draft these files are its only copy. It returns whether anything differs from the base. An editor that removed a file leaves the edit of that file as it was.

Jump to

Keyboard shortcuts

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