uiadapter

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Overview

package uiadapter converts UI interface to model interface

Index

Constants

View Source
const DefaultMaxWaitDuration = 365 * (24 * time.Hour) // 1 year

Waiting functions without timeout feature, such as Wait(), Command() and so on, use this timeout threshold which is enough for most cases.

Variables

View Source
var ErrorPipelineClosed = errors.New("pipeline is closed")

Error notifying XXXBuffer is Closed.

Functions

func RegisterAllRequestObserver added in v0.5.0

func RegisterAllRequestObserver(sender Sender, o RequestObserver)

Helper function for register RequestObserver for all of InputRequestType. The RequestObservers already registered are overwritten.

func UnregisterAllRequestObserver added in v0.5.0

func UnregisterAllRequestObserver(sender Sender)

Helper function for unregister RequestObserver for all of InputRequestType.

Types

type InputRequestType

type InputRequestType int8

indicates requesting of current input.

const (
	// request is none.
	InputRequestNone InputRequestType = iota

	// request command which is confirmed by user.
	InputRequestCommand

	// request just input which is empty command by user confirming.
	InputRequestInput

	// request raw inputting such as pressed key by user.
	InputRequestRawInput
)

type Layouter

type Layouter interface {
	// set new layout acording to attribute.LayoutData.
	// it may return error if LayoutData is invalid.
	//
	// More details for LayoutData structure is in erago/attribute package.
	SetLayout(layout *attribute.LayoutData) error

	// set default output view by view name.
	// Printer's functions will output to a default view.
	// it may return error if vname is not found.
	SetCurrentView(vname string) error

	// return default output view name.
	GetCurrentViewName() string

	// return existing views name in multiple layout.
	GetViewNames() []string
}

Layouting interface. it should be implemented to build multiple window user interface. These functions are called asynchronously.

type Printer

type Printer interface {
	Syncer // implements Syncer interface

	// Print text to screen.
	// It should implement moving to next line by "\n".
	Print(s string) error

	// Print label text to screen.
	// It should not be separated in wrapping text.
	PrintLabel(s string) error

	// Print Clickable button text. it shows caption on screen and emits command
	// when it is selected. It is no separatable in wrapping text.
	PrintButton(caption, command string) error

	// Print Line using sym.
	// given sym #, output line is: ############...
	PrintLine(sym string) error

	// Print image from file path.
	// Image is exceptional case, which may draw image region exceed over 1 line.
	PrintImage(file string, widthInRW, heightInLC int) error

	// Measure Image size in text scale, width in rune-width and height in line-count.
	// This is useful when PrintImage will call with either widthInRW or heightInLC is zero,
	// the drawn image size shall be auto determined but client want to know determined size
	// before calling PrintImage.
	MeasureImageSize(file string, widthInRW, heightInLC int) (width, height int, err error)

	// Set and Get Color using 0xRRGGBB for 24bit color
	SetColor(color uint32) error
	GetColor() (color uint32, err error)
	ResetColor() error

	// Set and Get Alignment
	SetAlignment(attribute.Alignment) error
	GetAlignment() (attribute.Alignment, error)

	// skip current lines to display none.
	// TODO: is it needed?
	NewPage() error

	// Clear lines specified number.
	ClearLine(nline int) error

	// Clear all lines containing historys.
	ClearLineAll() error

	// rune width to fill the window width.
	WindowRuneWidth() (int, error)

	// line count to fill the window height.
	WindowLineCount() (int, error)

	// current rune width in the editting line.
	CurrentRuneWidth() (int, error)

	// line count as it increases at outputting new line.
	LineCount() (int, error)
}

output interface. note that these functions may be called asynchronously.

type RequestObserver

type RequestObserver interface {
	// OnRequestChanged is called when user changes input request by calling
	// input APIs such as WaitXXX, CommandXXX and RawInputXXX.
	// This function is called on same context as input APIs.
	OnRequestChanged(InputRequestType)
}

RequestObserver observes changing input state.

type RequestObserverFunc added in v0.5.0

type RequestObserverFunc func(InputRequestType)

func (RequestObserverFunc) OnRequestChanged added in v0.5.0

func (fn RequestObserverFunc) OnRequestChanged(typ InputRequestType)

implements RequestObserver interface.

type Sender

type Sender interface {
	// send input event to app.
	Send(ev input.Event)
	// register listener for changing input request type.
	RegisterRequestObserver(InputRequestType, RequestObserver)
	// unregister listener for changing input request type.
	UnregisterRequestObserver(InputRequestType)
	// short hand for Send(QuitEvent).
	Quit()
}

Input Port interface.

type SingleUI

type SingleUI struct {
	Printer
}

SingleUI implements partial UI interface, Layouter. Printer interface is injected by user to build complete UI interface.

Thus, you can implement only Printer interface for complete UI interface:

UI = SingleUI{implements_only_printer}

func (SingleUI) GetCurrentViewName

func (ui SingleUI) GetCurrentViewName() string

func (SingleUI) GetViewNames

func (ui SingleUI) GetViewNames() []string

func (SingleUI) SetCurrentView

func (ui SingleUI) SetCurrentView(vname string) error

func (SingleUI) SetLayout

func (ui SingleUI) SetLayout(*attribute.LayoutData) error

type Syncer

type Syncer interface {
	// Sync flushes any pending output result, PrintXXX or ClearLine,
	// at UI implementor. It can also use rate limitting for PrintXXX functions.
	Sync() error
}

Syncer is a interface for synchronizing output and display state.

type UI

type UI interface {
	Printer
	Layouter
}

Interfaces for the presentaion layer.

type UIAdapter

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

UIAdapter converts input and output data in cannonical manner.

func New

func New(ui UI) *UIAdapter

func (UIAdapter) Command

func (port UIAdapter) Command() (string, error)

wait for string command.

func (UIAdapter) CommandNumber

func (port UIAdapter) CommandNumber() (int, error)

wait for integer command.

func (UIAdapter) CommandNumberRange

func (port UIAdapter) CommandNumberRange(ctx context.Context, min, max int) (int, error)

wait for number command that mathes in range [min : max]

func (UIAdapter) CommandNumberSelect

func (port UIAdapter) CommandNumberSelect(ctx context.Context, nums ...int) (int, error)

wait for number command that matches given nums.

func (UIAdapter) CommandNumberWithTimeout

func (port UIAdapter) CommandNumberWithTimeout(ctx context.Context, timeout time.Duration) (int, error)

wait for integer command.

func (UIAdapter) CommandWithTimeout

func (port UIAdapter) CommandWithTimeout(ctx context.Context, timeout time.Duration) (string, error)

wait for string command with context. it can cancel by cancelation for context. it returns command string and error which is uiadapter.ErrorPipelineClosed, context.DeadLineExceeded or context.Canceled.

func (UIAdapter) GetInputPort

func (ad UIAdapter) GetInputPort() Sender

get input interface

func (UIAdapter) Print

func (p UIAdapter) Print(s string) error

func (UIAdapter) PrintBar

func (p UIAdapter) PrintBar(now, max int64, w int, fg, bg string) error

print text bar with current value now, maximum value max, bar's width w, bar's symbol fg, and background symbol bg. For example, now=3, max=10, w=5, fg='#', and bg='.' then prints "[#..]".

func (UIAdapter) PrintC

func (p UIAdapter) PrintC(s string, w int) error

print text with padding space to fill at least having the width. e.g. text "AAA" with width 5 is "AAA ". But width of multibyte character is counted as 2, while that of single byte character is 1. If the text expresses button pattern, the entire text is teasted as Button. The text after "\n" is ignored.

func (UIAdapter) PrintL

func (p UIAdapter) PrintL(s string) error

func (UIAdapter) PrintPlain

func (p UIAdapter) PrintPlain(s string) error

print text without parsing button pattern.

func (UIAdapter) PrintW

func (ad UIAdapter) PrintW(s string) error

print text then wait any input.

func (UIAdapter) Quit

func (port UIAdapter) Quit()

send quit event signal to terminate RunFilter.

func (UIAdapter) RawInput

func (port UIAdapter) RawInput() (string, error)

wait for raw input without user confirming, hit enter key.

func (UIAdapter) RawInputWithTimeout

func (port UIAdapter) RawInputWithTimeout(ctx context.Context, timeout time.Duration) (string, error)

wait for raw input with timeout. raw input does not need user confirming, hit enter key. It returns command string and error which is uiadapter.ErrorPipelineClosed or context.DeadLineExceeded.

func (UIAdapter) RegisterRequestObserver added in v0.5.0

func (port UIAdapter) RegisterRequestObserver(typ InputRequestType, o RequestObserver)

It can not use concurrently.

func (UIAdapter) RunFilter

func (p UIAdapter) RunFilter(ctx context.Context) error

starting filtering input Event. It blocks until context is canceled, you can use go statement to run other thread. After canceling, inputPort is closed and can not be used.

It returns error which indicates what context is canceled by.

func (UIAdapter) Send

func (port UIAdapter) Send(ev input.Event)

send input event to input port.

func (UIAdapter) SetHorizontalLayout

func (p UIAdapter) SetHorizontalLayout(vname1, vname2 string, rate float64) error

func (UIAdapter) SetSingleLayout

func (p UIAdapter) SetSingleLayout(vname string) error

func (UIAdapter) SetVerticalLayout

func (p UIAdapter) SetVerticalLayout(vname1, vname2 string, rate float64) error

func (UIAdapter) TextBar

func (p UIAdapter) TextBar(now, max int64, w int, fg, bg string) (string, error)

return text represented bar as string. it is same as printed text by PrintBar().

func (UIAdapter) UnregisterRequestObserver added in v0.5.0

func (port UIAdapter) UnregisterRequestObserver(typ InputRequestType)

It can not use concurrently.

func (UIAdapter) VClearLine

func (p UIAdapter) VClearLine(vname string, nline int) error

func (UIAdapter) VClearLineAll

func (p UIAdapter) VClearLineAll(vname string) error

func (UIAdapter) VNewPage

func (p UIAdapter) VNewPage(vname string) error

func (UIAdapter) VPrint

func (p UIAdapter) VPrint(vname, s string) error

print string and parse button automatically

func (UIAdapter) VPrintBar

func (p UIAdapter) VPrintBar(vname string, now, max int64, width int, fg, bg string) error

func (UIAdapter) VPrintButton

func (p UIAdapter) VPrintButton(vname, caption, cmd string) error

func (UIAdapter) VPrintC

func (p UIAdapter) VPrintC(vname, s string, width int) error

print string. it also parses button automatically.

func (UIAdapter) VPrintL

func (p UIAdapter) VPrintL(vname, s string) error

print string + "\n" and parse button automatically

func (UIAdapter) VPrintLine

func (p UIAdapter) VPrintLine(vname string, sym string) error

func (UIAdapter) VPrintPlain

func (p UIAdapter) VPrintPlain(vname, s string) error

print plain text. no parse button

func (UIAdapter) VPrintW

func (ad UIAdapter) VPrintW(vname, s string) error

print text to view spcfied by name then wait any input.

func (UIAdapter) Wait

func (port UIAdapter) Wait() error

wait for any input. it will never return until getting any input.

func (UIAdapter) WaitWithTimeout

func (port UIAdapter) WaitWithTimeout(ctx context.Context, timeout time.Duration) error

wait for any input with context. it can cancel by cancelation for context. it returns error which is uiadapter.ErrorPipelineClosed, context.DeadLineExceeded or context.Canceled.

Directories

Path Synopsis
event
macro syntax is that 1.
macro syntax is that 1.

Jump to

Keyboard shortcuts

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