text

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package text provides a text field widget.

Package text provides a text field widget.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateTopMiddleBottom

func CalculateTopMiddleBottom(w IWidget, size gowid.IRenderSize) (int, int, int)

CalculateTopMiddleBottom will, for a given size, calculate three indices: - the index of the line of text that should be at the top of the rendered area - the number of lines to display - the number of lines occluded from the bottom

func GetCoordsFromCursorPos

func GetCoordsFromCursorPos(cursorPos int, maxCol int, layout *TextLayout, at IChrAt) (x int, y int)

zero-based

func GetCursorPosFromCoords

func GetCursorPosFromCoords(ccol int, crow int, layout *TextLayout, at IChrAt) int

GetCursorPosFromCoords translates a (col, row) coord to a cursor position. It looks up the layout structure at the right line, then adds the col to the segment start offset.

func IsBreakableSpace

func IsBreakableSpace(chr rune) bool

func Render

func Render(w IWidget, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas

If rendered Fixed, then rows==1 and cols==len(text)

Types

type Content

type Content []StyledRune

Content is an array of AttributedRune and implements IContent.

func NewContent

func NewContent(content []ContentSegment) *Content

NewContent constructs Content suitable for initializing a text Widget.

func (*Content) AddAt

func (h *Content) AddAt(idx int, content ContentSegment)

AddAt will insert the supplied ContentSegment at index idx.

func (Content) ChrAt

func (h Content) ChrAt(idx int) rune

ChrAt will return the unstyled rune at index idx.

func (*Content) Clone

func (h *Content) Clone() IContent

func (*Content) DeleteAt

func (h *Content) DeleteAt(idx int, length int)

DeleteAt will remove a segment of content of the provided length starting at index idx.

func (Content) Length

func (h Content) Length() int

Length will return the length of the content i.e. the number of runes it comprises.

func (Content) RangeOver

func (h Content) RangeOver(start, end int, attrs gowid.IRenderContext, proc gowid.ICellProcessor)

RangeOver will call the supplied ICellProcessor for each element of the content between start and end, having first transformed that content element into an AttributedRune by using the accompanying ICellStyler and the IRenderContext. You can use this to build up an array of Cells, for example, in the process of converting a text widget to something that can be rendered in a canvas.

func (Content) String

func (h Content) String() string

String implements fmt.Stringer.

func (Content) Width

func (h Content) Width() int

Width returns the number of screen cells the content takes. Different from Length if >1-width runes are used.

type ContentCB

type ContentCB struct{}

For callback registration

type ContentSegment

type ContentSegment struct {
	Style gowid.ICellStyler
	Text  string
}

ContentSegment represents some text each character of which is styled the same way.

func StringContent

func StringContent(s string) ContentSegment

StringContent makes a ContentSegment from a simple string.

func StyledContent

func StyledContent(text string, style gowid.ICellStyler) ContentSegment

StyledContent makes a ContentSegment from a string and an ICellStyler.

type ContentToCellArray

type ContentToCellArray struct {
	Cells []gowid.Cell
	Cur   int
}

ContentToCellArray is a helper type; it can be used to construct a Cell array by passing it to a RangeOver() function.

func (*ContentToCellArray) ProcessCell

func (m *ContentToCellArray) ProcessCell(cell gowid.Cell) gowid.Cell

type CopyableWidget

type CopyableWidget struct {
	*Widget
	gowid.IIdentity
	gowid.IClipboardSelected
}

func NewCopyable

func NewCopyable(text string, id gowid.IIdentity, cs gowid.IClipboardSelected, opts ...Options) *CopyableWidget

func (*CopyableWidget) Clips

func (w *CopyableWidget) Clips(app gowid.IApp) []gowid.ICopyResult

func (*CopyableWidget) Render

func (w *CopyableWidget) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas

func (*CopyableWidget) UserInput

func (w *CopyableWidget) UserInput(ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool

type IChrAt

type IChrAt interface {
	ChrAt(i int) rune
}

type ICloneContent

type ICloneContent interface {
	Clone() IContent
}

type IContent

type IContent interface {
	Length() int
	Width() int
	ChrAt(idx int) rune
	RangeOver(start, end int, attrs gowid.IRenderContext, proc gowid.ICellProcessor)
	AddAt(idx int, content ContentSegment)
	DeleteAt(idx, length int)
	fmt.Stringer
}

IContent represents a styled range of text. Different sections of the text can have different styles. Behind the scenes, this is just implemented as an array of (rune, ICellStyler) pairs - maybe nothing more complicated would ever be needed in practise. See also TextContent.

type ICursor

type ICursor interface {
	CursorEnabled() bool
	SetCursorDisabled()
	CursorPos() int
	SetCursorPos(pos int, app gowid.IApp)
}

type ISimple

type ISimple interface {
	gowid.IWidget
	Text() string
	SetText(text string, app gowid.IApp)
}

ISimple is a gowid Widget that supports getting and setting plain unstyled text. It is used by edit.Widget, for example. This package's Widget type implements it.

type IWidget

type IWidget interface {
	gowid.IWidget
	// Content returns an interface that provides access to the text and styling used.
	Content() IContent
	// Wrap determines whether the text is clipped, if too long, or flows onto the next line.
	Wrap() WrapType
	// Align can be used to keep each line of text left, right or center aligned.
	Align() gowid.IHAlignment
	// LinesFromTop is used to track how many widget lines are not in view off the top
	// given the current render. The widget tries to keep this the same when the widget
	// is re-rendered at a different size (e.g. the terminal is resized).
	LinesFromTop() int
	ClipIndicator() string
}

IWidget is a gowid IWidget with the following extra APIs.

type LineLayout

type LineLayout struct {
	StartLength int
	StartWidth  int
	EndWidth    int
	EndLength   int
	Clipped     bool
}

type Options

type Options struct {
	Wrap          WrapType
	ClipIndicator string
	Align         gowid.IHAlignment
}

Options is used to provide arguments to the various New initialization functions.

type SimpleCursor

type SimpleCursor struct {
	Pos int
}

func (*SimpleCursor) CursorEnabled

func (c *SimpleCursor) CursorEnabled() bool

func (*SimpleCursor) CursorPos

func (c *SimpleCursor) CursorPos() int

func (*SimpleCursor) SetCursorDisabled

func (c *SimpleCursor) SetCursorDisabled()

func (*SimpleCursor) SetCursorPos

func (c *SimpleCursor) SetCursorPos(pos int, app gowid.IApp)

type StyledRune

type StyledRune struct {
	Chr  rune
	Attr gowid.ICellStyler
}

StyledRune is a styled rune.

func MakeAttributedRunes

func MakeAttributedRunes(m ContentSegment) []StyledRune

MakeAttributedRunes converts a ContentSegment into an array of AttributeRune, which is used to build a Content implementing IContent.

type TextLayout

type TextLayout struct {
	Lines []LineLayout
}

func MakeTextLayout

func MakeTextLayout(content IContent, width int, wrap WrapType, align gowid.IHAlignment) *TextLayout

MakeTextLayout builds an array of line layouts from an IContent object. It applies the provided text wrapping and alignment options. The line layouts can then be used to index the IContent in order to build a canvas for rendering.

type Widget

type Widget struct {
	Callbacks *gowid.Callbacks
	gowid.RejectUserInput
	gowid.NotSelectable
	// contains filtered or unexported fields
}

Widget can be used to display text on the screen, with optional styling for specified regions of the text.

func New

func New(text string, opts ...Options) *Widget

New initializes a text widget with a string and some extra arguments e.g. to align the text within each line, and to determine whether or not it's clipped.

func NewFromContent

func NewFromContent(content IContent) *Widget

NewFromContent initializes a text widget with IContent, which can be built from a set of content segments. This is a way of making a text widget with styling.

func NewFromContentExt

func NewFromContentExt(content IContent, opts Options) *Widget

NewFromContentExt initialized a text widget with IContent and some extra options such as wrapping, alignment, etc.

func (*Widget) Align

func (w *Widget) Align() gowid.IHAlignment

func (*Widget) ClipIndicator

func (w *Widget) ClipIndicator() string

func (*Widget) Content

func (w *Widget) Content() IContent

func (*Widget) LinesFromTop

func (w *Widget) LinesFromTop() int

func (*Widget) OnContentSet

func (w *Widget) OnContentSet(cb gowid.IWidgetChangedCallback)

func (*Widget) Read

func (w *Widget) Read(p []byte) (n int, err error)

Read makes Widget implement io.Reader.

func (*Widget) RemoveOnContentSet

func (w *Widget) RemoveOnContentSet(cb gowid.IIdentity)

func (*Widget) Render

func (w *Widget) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas

func (*Widget) RenderSize

func (w *Widget) RenderSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox

func (*Widget) SetAlign

func (w *Widget) SetAlign(align gowid.IHAlignment, app gowid.IApp)

func (*Widget) SetContent

func (w *Widget) SetContent(app gowid.IApp, content IContent)

func (*Widget) SetLinesFromTop

func (w *Widget) SetLinesFromTop(l int, app gowid.IApp)

func (*Widget) SetText

func (w *Widget) SetText(text string, app gowid.IApp)

func (*Widget) SetWrap

func (w *Widget) SetWrap(wrap WrapType, app gowid.IApp)

func (*Widget) String

func (w *Widget) String() string

func (*Widget) Wrap

func (w *Widget) Wrap() WrapType

type Widget1

type Widget1 struct {
	I int
}

func (*Widget1) Render

func (w *Widget1) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas

func (*Widget1) RenderSize

func (w *Widget1) RenderSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox

func (*Widget1) Selectable

func (w *Widget1) Selectable() bool

func (*Widget1) UserInput

func (w *Widget1) UserInput(ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool

type WidgetWithCursor

type WidgetWithCursor struct {
	*Widget
	*SimpleCursor
}

This meets both IText and ICursor, and allows me to make a canvas from a text widget and a separately specified cursor position

func (*WidgetWithCursor) CalculateTopMiddleBottom

func (w *WidgetWithCursor) CalculateTopMiddleBottom(size gowid.IRenderSize) (int, int, int)

func (*WidgetWithCursor) Render

func (w *WidgetWithCursor) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas

type WrapType

type WrapType int
const (
	WrapAny WrapType = iota
	WrapClip
)

type Writer

type Writer struct {
	*Widget
	gowid.IApp
}

Writer is a wrapper around a text Widget which, by including the app, can be used to implement io.Writer.

func (*Writer) Write

func (w *Writer) Write(p []byte) (n int, err error)

Write implements io.Writer. The app is required because the content setting API requires the app because callbacks might be invoked which themselves require the app.

Jump to

Keyboard shortcuts

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