opshell

package
v0.0.1-beta.5 Latest Latest
Warning

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

Go to latest
Published: May 4, 2024 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package opshell - Operator's interactive shell

Index

Constants

This section is empty.

Variables

View Source
var ErrOutputClosed = errors.New("output channel closed")

ErrOutputClosed is returned by Shell.Do when it returns because someone closed the output channel.

Functions

func ColorEC

func ColorEC(ec *term.EscapeCodes, color Color) []byte

ColorEC returns the escape code for the given color from ec. A non-nil slice will always be returned, even if ec is nil. As a special case, ColorNone returns an empty slice, as do unknown colors. The returned slice is a copy of the slice in ec or a newly-allocated slice; it may be modified at will.

Types

type CLine

type CLine struct {
	Color       Color
	Line        string
	Prompt      string
	NoTimestamp bool /* Don't print a timestamp. */
	Plain       bool /* No newline, color, timestamp, or anything else. */
}

CLine is a line and a color to print. If Prompt is not empty, it is set as the prompt before printing the line.

type Color

type Color int

Color represents a specific color.

const (
	ColorNone Color = iota // Equivalent to Terminal.Logf
	ColorBlack
	ColorRed
	ColorGreen
	ColorYellow
	ColorBlue
	ColorMagenta
	ColorCyan
	ColorWhite
	ColorReset
)

Options for colors in Terminal.ColorLogf

func (Color) String

func (i Color) String() string

type Shell

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

Shell is the shell used by an operator. It's a wrapper around golang.org/x/term.Terminal.

func New

func New(
	ich chan<- string,
	och <-chan CLine,
	prompt string,
	noTimestamps bool,
) (*Shell, func(), error)

New puts the controlly TTY in raw mode and returns a new Shell wrapping stdio. Call Shell.Do to start processing lines and handle resizing and call the returned function to restore the TTY's state and clean up other resources. ich will be closed before Shell.Do returns. IF noTimestamps is true, no timestamps will be printed.

func (*Shell) Do

func (s *Shell) Do(ctx context.Context) error

Do proxies between the channels with which the shell was made and stdio as well as watches for SIGWINCH to handle shell resizing. Do closes the input channel passed to New. Do returns ErrOutputClosed if the output channel passed to New is closed.

func (*Shell) Logf

func (s *Shell) Logf(
	color Color,
	noTS bool,
	format string,
	v ...any,
) (int, error)

Logf logs a line to the shell. It is similar to log.Printf but includes a color and only logs the time, not the date. Logf may be called from multiple goroutines simultaneously.

func (*Shell) SetPrompt

func (s *Shell) SetPrompt(prompt string)

SetPrompt sets the shell's prompt. This can also be done by sending it a CLine with CLine.Prompt set. Don't forget a trailing space. Use s.WrapIncolor to color the prompt.

func (*Shell) WrapInColor

func (s *Shell) WrapInColor(st string, color Color) string

WrapInColor returns s, wrapped on the left with the given color and on the right with ColorReset. As a special case, if color is ColorNone, s is returned unchanged.

Jump to

Keyboard shortcuts

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