cwin

package
v0.0.0-...-59848b6 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DirUp = Dir(iota)
	DirUpRight
	DirRight
	DirDownRight
	DirDown
	DirDownLeft
	DirLeft
	DirUpLeft
	DirCount = int(DirUpLeft)
)
View Source
const (
	DirN  = DirUp
	DirNE = DirUpRight
	DirE  = DirRight
	DirSE = DirDownRight
	DirS  = DirDown
	DirSW = DirDownLeft
	DirW  = DirLeft
	DirNW = DirUpLeft
)
View Source
const (
	EventHandled = EventResponse(iota)
	EventNotHandled
	EventLoopStop
)
View Source
const (
	BorderRuneUL int = iota
	BorderRuneUR
	BorderRuneLR
	BorderRuneLL
	BorderRuneV
	BorderRuneH
	BorderRuneCount
)
View Source
const (
	RuneSpace rune = ' '
)

Variables

View Source
var (
	SingleLineBorderRunes = BorderRunes{'┏', '┓', '┛', '┗', '┃', '━'}
	DoubleLineBorderRunes = BorderRunes{'╔', '╗', '╝', '╚', '║', '═'}
)
View Source
var (
	DirOffSetXY = map[Dir]Point{
		DirUp:        {X: 0, Y: -1},
		DirUpRight:   {X: 1, Y: -1},
		DirRight:     {X: 1, Y: 0},
		DirDownRight: {X: 1, Y: 1},
		DirDown:      {X: 0, Y: 1},
		DirDownLeft:  {X: -1, Y: 1},
		DirLeft:      {X: -1, Y: 0},
		DirUpLeft:    {X: -1, Y: -1},
	}
)
View Source
var (
	DirRunes = map[Dir]rune{
		DirN:  '↑',
		DirNE: '↗',
		DirE:  '→',
		DirSE: '↘',
		DirS:  '↓',
		DirSW: '↙',
		DirW:  '←',
		DirNW: '↖',
	}
)

Functions

func ByteSizeStr

func ByteSizeStr(s int64) string

func FindKey

func FindKey(keys []cterm.Event, key cterm.Event) bool

func GenUID

func GenUID() int64

func Keys

func Keys(keys ...interface{}) []cterm.Event

func RunEventLoop

func RunEventLoop(s *Sys, evHandler EventHandler, f ...EventLoopSleepDurationFunc)

Types

type Align

type Align int
const (
	AlignLeft Align = iota
	AlignCenter
	AlignRight
)

type Attr

type Attr struct {
	Fg, Bg cterm.Attribute // cterm.ColorRed | ColorGreen | ...
}

type BorderRunes

type BorderRunes [BorderRuneCount]rune

type Chx

type Chx struct {
	Ch   rune
	Attr Attr
}

func TransparentChx

func TransparentChx() Chx

type Dir

type Dir int

func OffsetXYToDir

func OffsetXYToDir(dx, dy int) Dir

type EventHandler

type EventHandler func(ev cterm.Event) EventResponse

type EventLoopSleepDurationFunc

type EventLoopSleepDurationFunc func() time.Duration

type EventResponse

type EventResponse int

func FalseForEventSystemStop

func FalseForEventSystemStop(b bool) EventResponse

func NopHandledEventHandler

func NopHandledEventHandler(ev cterm.Event) EventResponse

func NopNotHandledEventHandler

func NopNotHandledEventHandler(ev cterm.Event) EventResponse

func TrueForEventSystemStop

func TrueForEventSystemStop(b bool) EventResponse

type Point

type Point struct {
	X, Y int
}

type Rect

type Rect struct {
	X, Y, W, H int
}

func TextDimension

func TextDimension(s string) Rect

func (Rect) Contain

func (r Rect) Contain(x, y int) bool

func (Rect) MoveDelta

func (r Rect) MoveDelta(dx, dy int) Rect

func (Rect) Overlap

func (r Rect) Overlap(other Rect) (Rect, bool)

func (Rect) Range

func (r Rect) Range(f func(x, y int))

func (Rect) String

func (r Rect) String() string

func (Rect) ToOrigin

func (r Rect) ToOrigin() Rect

type Sys

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

func Init

func Init(provider cterm.Provider) (*Sys, error)

func (*Sys) CenterBanner

func (s *Sys) CenterBanner(parent Win, title, format string, a ...interface{}) Win

func (*Sys) Close

func (s *Sys) Close()

func (*Sys) CreateWin

func (s *Sys) CreateWin(parent Win, cfg WinCfg) Win

func (*Sys) CreateWinEx

func (s *Sys) CreateWinEx(parent Win, createWin func() Win) Win

func (*Sys) Dump

func (s *Sys) Dump() string

func (*Sys) FPS

func (s *Sys) FPS() int64

func (*Sys) Find

func (s *Sys) Find(w Win) Win

func (*Sys) GetEvent

func (s *Sys) GetEvent() cterm.Event

This is a blocking call

func (*Sys) GetFocused

func (s *Sys) GetFocused() Win

func (*Sys) MessageBox

func (s *Sys) MessageBox(parent Win, title, format string, a ...interface{}) bool

MessageBox is mostly similar to MessageBoxEx but only with 2 expected keys: Enter or ESC It returns true if Enter is pressed or false if ESC is pressed.

func (*Sys) MessageBoxEx

func (s *Sys) MessageBoxEx(
	parent Win, keys []cterm.Event, title, format string, a ...interface{}) cterm.Event

MessageBoxEx displays a blue-background message window centered in the client area of parent window, and synchronously waiting for a set of user specified keys, and return if any of the keys are pressed.

func (*Sys) Refresh

func (s *Sys) Refresh()

func (*Sys) RemoveWin

func (s *Sys) RemoveWin(w Win)

func (*Sys) Run

func (s *Sys) Run(fallbackHandler EventHandler, f ...EventLoopSleepDurationFunc)

func (*Sys) SetFocus

func (s *Sys) SetFocus(w Win)

func (*Sys) SyncExpectKey

func (s *Sys) SyncExpectKey(f func(cterm.Key, rune) bool)

if f == nil, SyncExpectKey waits for any single key and then returns if f != nil, SyncExpectKey repeatedly waits for a key & has it processed by f, if f returns false

func (*Sys) SysWin

func (s *Sys) SysWin() Win

func (*Sys) TotalChxRendered

func (s *Sys) TotalChxRendered() int64

func (*Sys) TryFind

func (s *Sys) TryFind(w Win) (Win, bool)

func (*Sys) TryGetEvent

func (s *Sys) TryGetEvent() cterm.Event

This is a non-blocking call

func (*Sys) Update

func (s *Sys) Update()

type Win

type Win interface {
	Cfg() WinCfg

	Sys() *Sys

	// Base returns the embedded WinBase pointer which can be used for accessing
	// the library built-in WinBase functionalities, as well as serving as a unique
	// identifier for the window.
	Base() *WinBase
	// Same tells if this Win is the same instance of other Win.
	Same(other Win) bool
	// This returns the actual object that implements Win interface that is registered
	// with Sys. Because WinBase implements Win interface, sometimes we go into situation
	// where a WinBase pointer is getting passed around but eventually when deverloper
	// wants to cast back to their own object (which embeds WinBase) they get type assertion
	// failure. As long as the object (that implements Win) passed into Sys.CreateWin is
	// the "top-level" object, This() will always return that registered object. Note if
	// calling This() on a non Sys managed (i.e. not from Sys.CreateWin) object, it will
	// panic.
	This() Win
	Parent() Win
	Prev() Win
	Next() Win
	ChildFirst() Win
	ChildLast() Win

	// Returns the Rect of the window relative to its parent window's client region.
	Rect() Rect
	// Returns the client Rect of the window, relative to this window.
	ClientRect() Rect

	// Moves the window position (relative to its parent's client region) to (x, y)
	SetPosAbs(x, y int)
	// Moves the window position (relative to its parent's client region) by (dx, dy)
	SetPosRel(dx, dy int)
	// Sets the event handler for this window.
	SetEventHandler(evHandler EventHandler)
	// Sets the window's title.
	SetTitle(format string, a ...interface{})
	// Sets a text, multi-line allowed, to the client region of the window
	SetText(format string, a ...interface{})
	// Sets a single line text, to the client region of the window
	SetLine(cy int, attr Attr, format string, a ...interface{})

	// Set the window to be in or out of focus. This method should only conduct focus related
	// changes to this window only. To actually set a window in focus system-wise needs to do
	// more things like set up message handler, changing z-order, etc, which requires a calling
	// to Sys.SetFocus.
	SetFocus(focused bool)

	// Sets the window to the bottom position among all the child windows of its parent window.
	SendToBottom(recursive bool)
	// Sets the window to the top position among all the child windows of its parent window.
	SendToTop(recursive bool)

	// Gets the Chx from the window.
	Get(x, y int) Chx
	// Gets the Chx from the client region of the window. Note cx/cy are client coordinate,
	// relative to this window's ClientRect.
	GetClient(cx, cy int) Chx
	// Puts a Chx to the client region of the window. Note cx/cy are client coordinate,
	// relative to this window's ClientRect.
	PutClient(cx, cy int, chx Chx)
	// Puts a rune to the client region of the window with default color attributes. Note cx/cy
	// are client coordinate, relative to this window's ClientRect.
	PutClientCh(cx, cy int, ch rune)
	// Fills a region inside the client region of the window. Note cr is the region Rect, relative
	// to this window's ClientRect.
	FillClient(cr Rect, chx Chx)

	fmt.Stringer
}

Win represents a window

type WinBase

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

func NewWinBase

func NewWinBase(sys *Sys, parent Win, c WinCfg) *WinBase

func (*WinBase) Base

func (wb *WinBase) Base() *WinBase

func (*WinBase) Cfg

func (wb *WinBase) Cfg() WinCfg

func (*WinBase) ChildFirst

func (wb *WinBase) ChildFirst() Win

func (*WinBase) ChildLast

func (wb *WinBase) ChildLast() Win

func (*WinBase) ClientRect

func (wb *WinBase) ClientRect() Rect

func (*WinBase) FillClient

func (wb *WinBase) FillClient(cr Rect, chx Chx)

func (*WinBase) Get

func (wb *WinBase) Get(x, y int) Chx

func (*WinBase) GetClient

func (wb *WinBase) GetClient(cx, cy int) Chx

func (*WinBase) Next

func (wb *WinBase) Next() Win

func (*WinBase) Parent

func (wb *WinBase) Parent() Win

func (*WinBase) Prev

func (wb *WinBase) Prev() Win

func (*WinBase) PutClient

func (wb *WinBase) PutClient(cx, cy int, chx Chx)

func (*WinBase) PutClientCh

func (wb *WinBase) PutClientCh(cx, cy int, ch rune)

func (*WinBase) Rect

func (wb *WinBase) Rect() Rect

func (*WinBase) Same

func (wb *WinBase) Same(other Win) bool

func (*WinBase) SendToBottom

func (wb *WinBase) SendToBottom(recursive bool)

func (*WinBase) SendToTop

func (wb *WinBase) SendToTop(recursive bool)

func (*WinBase) SetEventHandler

func (wb *WinBase) SetEventHandler(evHandler EventHandler)

func (*WinBase) SetFocus

func (wb *WinBase) SetFocus(focused bool)

func (*WinBase) SetLine

func (wb *WinBase) SetLine(cy int, attr Attr, format string, a ...interface{})

func (*WinBase) SetPosAbs

func (wb *WinBase) SetPosAbs(x, y int)

func (*WinBase) SetPosRel

func (wb *WinBase) SetPosRel(dx, dy int)

func (*WinBase) SetText

func (wb *WinBase) SetText(format string, a ...interface{})

func (*WinBase) SetTitle

func (wb *WinBase) SetTitle(format string, a ...interface{})

func (*WinBase) String

func (wb *WinBase) String() string

func (*WinBase) Sys

func (wb *WinBase) Sys() *Sys

func (*WinBase) This

func (wb *WinBase) This() Win

type WinCfg

type WinCfg struct {
	// Required.
	// For root window, R.X/Y specify the absolute console x/y of the root window; W/H for its size
	// For regular window (that is direct/indirect descendant of root window), R.X/Y is the relative
	// position from its direct parent window. W/H for its size.
	R Rect
	// All the following are optional.
	EventHandler       EventHandler
	Name               string // also used as title (unless NoTitle or NoBorder is true)
	NoBorder           bool
	BorderRunes        *BorderRunes // if NoBorder && BorderRunes==nil, default to 1-line border
	InFocusBorderRunes *BorderRunes // Used for border rendering when window is in focus. If nil, default to 2-line border
	BorderAttr         Attr
	InFocusBorderAttr  *Attr // Used for border rendering when window is in focus. If nil, default Fg=ColorLightYellow
	ClientAttr         Attr
	NoTitle            bool // in case user sets Name (for debug purpose) and border, but doesn't want Title
	NoPaddingTitle     bool // most cases, have a one-space padding on each side of title looks nice
	NoPaddingText      bool // most cases, have a one-space padding on each side of text block looks nice
	TitleAlign         Align
	TextAlign          Align
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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