Documentation ¶
Overview ¶
package uiadapter converts UI interface to model interface
Index ¶
- Constants
- Variables
- func RegisterAllRequestObserver(sender Sender, o RequestObserver)
- func UnregisterAllRequestObserver(sender Sender)
- type InputRequestType
- type Layouter
- type Printer
- type RequestObserver
- type RequestObserverFunc
- type Sender
- type SingleUI
- type Syncer
- type UI
- type UIAdapter
- func (port UIAdapter) Command() (string, error)
- func (port UIAdapter) CommandNumber() (int, error)
- func (port UIAdapter) CommandNumberRange(ctx context.Context, min, max int) (int, error)
- func (port UIAdapter) CommandNumberSelect(ctx context.Context, nums ...int) (int, error)
- func (port UIAdapter) CommandNumberWithTimeout(ctx context.Context, timeout time.Duration) (int, error)
- func (port UIAdapter) CommandWithTimeout(ctx context.Context, timeout time.Duration) (string, error)
- func (ad UIAdapter) GetInputPort() Sender
- func (p UIAdapter) Print(s string) error
- func (p UIAdapter) PrintBar(now, max int64, w int, fg, bg string) error
- func (p UIAdapter) PrintC(s string, w int) error
- func (p UIAdapter) PrintL(s string) error
- func (p UIAdapter) PrintPlain(s string) error
- func (ad UIAdapter) PrintW(s string) error
- func (port UIAdapter) Quit()
- func (port UIAdapter) RawInput() (string, error)
- func (port UIAdapter) RawInputWithTimeout(ctx context.Context, timeout time.Duration) (string, error)
- func (port UIAdapter) RegisterRequestObserver(typ InputRequestType, o RequestObserver)
- func (p UIAdapter) RunFilter(ctx context.Context) error
- func (port UIAdapter) Send(ev input.Event)
- func (p UIAdapter) SetHorizontalLayout(vname1, vname2 string, rate float64) error
- func (p UIAdapter) SetSingleLayout(vname string) error
- func (p UIAdapter) SetVerticalLayout(vname1, vname2 string, rate float64) error
- func (p UIAdapter) TextBar(now, max int64, w int, fg, bg string) (string, error)
- func (port UIAdapter) UnregisterRequestObserver(typ InputRequestType)
- func (p UIAdapter) VClearLine(vname string, nline int) error
- func (p UIAdapter) VClearLineAll(vname string) error
- func (p UIAdapter) VNewPage(vname string) error
- func (p UIAdapter) VPrint(vname, s string) error
- func (p UIAdapter) VPrintBar(vname string, now, max int64, width int, fg, bg string) error
- func (p UIAdapter) VPrintButton(vname, caption, cmd string) error
- func (p UIAdapter) VPrintC(vname, s string, width int) error
- func (p UIAdapter) VPrintL(vname, s string) error
- func (p UIAdapter) VPrintLine(vname string, sym string) error
- func (p UIAdapter) VPrintPlain(vname, s string) error
- func (ad UIAdapter) VPrintW(vname, s string) error
- func (port UIAdapter) Wait() error
- func (port UIAdapter) WaitWithTimeout(ctx context.Context, timeout time.Duration) error
Constants ¶
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 ¶
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) // Print blank space sized with rune-width. PrintSpace(widthInRW int) 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. If UI is already terminated and Printer no longer available, Printer's methods should return uiadapater.ErrorPipelineClosed to shutdown application without any problem.
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 (SingleUI) GetViewNames ¶
func (SingleUI) SetCurrentView ¶
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 UIAdapter ¶
type UIAdapter struct {
// contains filtered or unexported fields
}
UIAdapter converts input and output data in cannonical manner.
func (UIAdapter) CommandNumber ¶
wait for integer command.
func (UIAdapter) CommandNumberRange ¶
wait for number command that mathes in range [min : max]
func (UIAdapter) CommandNumberSelect ¶
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) PrintBar ¶
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 ¶
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) PrintPlain ¶
print text without parsing button pattern.
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 ¶
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) SetHorizontalLayout ¶
func (UIAdapter) SetSingleLayout ¶
func (UIAdapter) SetVerticalLayout ¶
func (UIAdapter) TextBar ¶
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 (UIAdapter) VClearLineAll ¶
func (UIAdapter) VPrintButton ¶
func (UIAdapter) VPrintLine ¶
func (UIAdapter) VPrintPlain ¶
print plain text. no parse button
func (UIAdapter) Wait ¶
func (port UIAdapter) Wait() error
wait for any input. it will never return until getting any input.
func (UIAdapter) WaitWithTimeout ¶
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.