pencere

package module
v0.0.0-...-b6868f3 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2018 License: AGPL-3.0 Imports: 14 Imported by: 0

README

pencere

IT IS JUST A PROTOTYPE FOR NOW AND SUBJECT TO HEAVYLY CHANGE

A new Terminal UI kit for go

What is new
Full rendering with windows (rectangles)

Every ui element is a window (pencere) and ui is built by compositing new windows adding as child window. Every window can focus, receive mouse events, keyboard events, drag, drop, render and so on.

Pencere takes and dispatches terminal events to windows and renders windows based on Zindexes and window attributes like Visible and HasBorder

Layout

Every window has it's Layout event to change layout of itself or childs. Also every window has LayoutOrder attribute to take precedence on layout event order. So we can easly implement window slider where slider manages side windows layouts

Drag and Drop

Windows support Draggable,Droppable attributes to be come eligible for Drag and Drop events. OnDragBegin, OnDragEnd, OnDragging, OnDragEnter, OnDragOver, OnDragLeave, CanDrop and OnDrop events are supported.

Focusing

Windows can receive keyboard events when they are focused. Managed by CanFocus attribute.

Mouse events

Windows suport mouse events and events canbe received if relavent eventhandler is implemented, if not event may be handled by parent windows

New Event system (as message bus)

New event system will be implemented as message bus to support inter window communication

Sidebars

Windows has left right sidebar , top and bottom toolbars as window (pencere) means can be implemented as menu, toolbar or sticked toolboxes

Menu system

Menu systems and menu events ( in progress)

Because usage of window ui is very elastic and mature concept, many many widgets and features can be implemented.

Used libraries

Inspired and used some code from these libraries. Basicly tried to be like and compatible to reuse of widgets and other stuff

Documentation

Index

Constants

View Source
const (
	TOP_RIGHT       = '┐'
	VERTICAL_LINE   = '│'
	HORIZONTAL_LINE = '─'
	TOP_LEFT        = '┌'
	BOTTOM_RIGHT    = '┘'
	BOTTOM_LEFT     = '└'
	VERTICAL_LEFT   = '┤'
	VERTICAL_RIGHT  = '├'
	HORIZONTAL_DOWN = '┬'
	HORIZONTAL_UP   = '┴'
	QUOTA_LEFT      = '«'
	QUOTA_RIGHT     = '»'

	FOCUSED_TOP_LEFT = '╒'
	FOCUSED_TOP_RIGT = '╕'
	FOCUSED_TOP      = '═'

	//SCROLL_BASE = '░'
	//SCROLL_BASE   = '█'
	SCROLL_BASE   = ' '
	SCROLL_BUTTON = '█'
)
View Source
const (
	KeyBackspace  = KeyBS
	KeyTab        = KeyTAB
	KeyEsc        = KeyESC
	KeyEscape     = KeyESC
	KeyEnter      = KeyCR
	KeyBackspace2 = KeyDEL
)

These are aliases for other keys.

View Source
const (
	BORDER = 1 << iota
	SCROLLABLE
	ENABLED
	VISIBLE
	CANFOCUS
)
View Source
const ColorDefault = -1

ColorDefault = clear

View Source
const DOTS = '…'

Variables

View Source
var Bus = NewEventBus()
View Source
var DefaultTheme = &Theme{
	styles: map[string]Style{
		"default":             {Fg: 100, Bg: 230},
		"border":              {Fg: 230, Bg: 0},
		"label":               {Fg: 230, Bg: 0},
		"menubar":             {Fg: 12, Bg: 123},
		"scrollbar":           {Fg: 12, Bg: 123, Texture: '░'},
		"textbox":             {Fg: 12, Bg: 50, Texture: '░'},
		"list.item.selected":  {Reverse: DecorationOn},
		"table.cell.selected": {Reverse: DecorationOn},
		"button.focused":      {Reverse: DecorationOn},
	},
}

DefaultTheme is a theme with reasonable defaults.

Functions

func Clear

func Clear()

Clear clears the screen with the default Bg color.

func Close

func Close()

Close finalizes termui library. It should be called after successful initialization when termui's functionality isn't required anymore.

func Decorate

func Decorate(this *Pencere, buf *Buffer)

func Error

func Error(issue, diagnostics string)

func Init

func Init() error

Init initializes termui library. This function should be called before any others. After initialization, the library must be finalized by 'Close' function.

func Loop

func Loop()

Loop gets events from termbox and passes them off to handleEvent. Stops when StopLoop is called.

func MaxString

func MaxString(s string, l int) string

MaxString trims a string and adds dots if the string is longer than a give length.

func NextTo

func NextTo(p *Pencere, n *Pencere) func() error

func Render

func Render() error

func RenderOLD

func RenderOLD(bs ...Bufferer)

Render renders all Bufferers in the given order to termbox, then asks termbox to print the screen.

func Round

func Round(f float64) float64

func SetFocus

func SetFocus(p *Pencere)

func StopLoop

func StopLoop()

StopLoop stops the event loop.

Types

type Buffer

type Buffer struct {
	Area    image.Rectangle // selected drawing area
	CellMap map[image.Point]Cell
}

Buffer is a renderable rectangle cell data container.

func NewBuffer

func NewBuffer() *Buffer

NewBuffer returns a new empty Buffer.

func NewFilledBuffer

func NewFilledBuffer(x0, y0, x1, y1 int, c Cell) *Buffer

NewFilledBuffer returns a new Buffer filled with the given Cell.

func (*Buffer) At

func (self *Buffer) At(x, y int) Cell

At returns the cell at (x,y).

func (*Buffer) Fill

func (self *Buffer) Fill(c Cell)

Fill fills the Buffer with a Cell.

func (*Buffer) Merge

func (self *Buffer) Merge(bs ...*Buffer)

Merge merges the given buffers onto the current Buffer.

func (*Buffer) MergeChildArea

func (self *Buffer) MergeChildArea(buf *Buffer, left, top, width, height int)

func (*Buffer) MergeWithOffset

func (self *Buffer) MergeWithOffset(buf *Buffer, xOffset, yOffset int)

MergeWithOffset merges a Buffer onto another with an offset.

func (*Buffer) SetArea

func (self *Buffer) SetArea(r image.Rectangle)

SetArea assigns a new rect area to self.

func (*Buffer) SetAreaXY

func (self *Buffer) SetAreaXY(x, y int)

SetAreaXY sets the Buffer bounds from (0,0) to (x,y).

func (*Buffer) SetCell

func (self *Buffer) SetCell(x, y int, c Cell)

SetCell assigns a Cell to (x,y).

func (*Buffer) SetString

func (self *Buffer) SetString(x, y int, s string, fg, bg Color)

SetString assigns a string to a Buffer starting at (x,y).

type Bufferer

type Bufferer interface {
	Buffer() *Buffer
	GetXOffset() int
	GetYOffset() int
}

Bufferer should be implemented by all renderable components.

type Cell

type Cell struct {
	Ch rune
	Fg Color
	Bg Color
}

Cell is a rune with assigned Fg and Bg.

func NewCell

func NewCell(ch rune, Fg, Bg Color) Cell

NewCell returne a new Cell given all necessary fields.

type Color

type Color int

Color is an integer in the range -1 to 255. -1 is clear, while 0-255 are xterm 256 colors.

const (
	AttrBold Color = 1 << (iota + 9)
	AttrUnderline
	AttrReverse
)

Copied from termbox. Attributes that can be bitwise OR'ed with a color.

type Colorscheme

type Colorscheme struct {
	Fg Color
	Bg Color

	LabelFg  Color
	LabelBg  Color
	BorderFg Color
	BorderBg Color

	Sparkline   Color
	LineGraph   Color
	TableCursor Color
	GaugeColor  Color
}

A Colorscheme represents the current look-and-feel of the dashboard.

type Control

type Control interface {
	Window() *Pencere
	AddWindow(c Control)
}

type Decoration

type Decoration int

Decoration represents a bold/underline/etc. state

const (
	DecorationInherit Decoration = iota
	DecorationOn
	DecorationOff
)

Decoration modes: Inherit from parent widget, explicitly on, or explicitly off.

type DragBeginEvent

type DragBeginEvent struct {
	Target           *Pencere
	GlobalX, GlobalY int
	X, Y             int
	Type             int
}

type DragContext

type DragContext struct {
	Properties   Properties
	DraggingIcon *Pencere
	IconOffsetX  int
	IconOffsetY  int
}

func NewDragContext

func NewDragContext(source *Pencere) *DragContext

type DragEndEvent

type DragEndEvent struct {
	DragContext      *DragContext
	DropedPencere    *Pencere
	Target           *Pencere
	GlobalX, GlobalY int
	X, Y             int
	Type             int
}

type DragEnterEvent

type DragEnterEvent struct {
	DragContext      *DragContext
	Target           *Pencere
	GlobalX, GlobalY int
	X, Y             int
	Type             int
}

type DragLeaveEvent

type DragLeaveEvent struct {
	DragContext      *DragContext
	Target           *Pencere
	GlobalX, GlobalY int
	X, Y             int
	Type             int
}

type DragOverEvent

type DragOverEvent struct {
	DragContext      *DragContext
	Target           *Pencere
	GlobalX, GlobalY int
	X, Y             int
	Type             int
}

type DraggingEvent

type DraggingEvent struct {
	DragContext      *DragContext
	Target           *Pencere
	GlobalX, GlobalY int
	X, Y             int
	Type             int
}

type DropEvent

type DropEvent struct {
	DragContext      *DragContext
	Target           *Pencere
	GlobalX, GlobalY int
	X, Y             int
	Type             int
}

type EventBus

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

func NewEventBus

func NewEventBus() *EventBus

func (*EventBus) On

func (this *EventBus) On(ctx context.Context, subject string, callback EventCallback)

func (*EventBus) PushEvent

func (this *EventBus) PushEvent(ctx context.Context, subject string, event interface{}) error

type EventCallback

type EventCallback func(ctx context.Context, event interface{}) error

type EventData

type EventData struct {
	Subject string
	Event   interface{}
}

type EventStream

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

func (EventStream) HandleEvent

func (this EventStream) HandleEvent(e tb.Event)

type Key

type Key uint16
const (
	KeyRune Key = iota + 256
	KeyUp
	KeyDown
	KeyRight
	KeyLeft
	KeyUpLeft
	KeyUpRight
	KeyDownLeft
	KeyDownRight
	KeyCenter
	KeyPgUp
	KeyPgDn
	KeyHome
	KeyEnd
	KeyInsert
	KeyDelete
	KeyHelp
	KeyExit
	KeyClear
	KeyCancel
	KeyPrint
	KeyPause
	KeyBacktab
	KeyF1
	KeyF2
	KeyF3
	KeyF4
	KeyF5
	KeyF6
	KeyF7
	KeyF8
	KeyF9
	KeyF10
	KeyF11
	KeyF12
	KeyF13
	KeyF14
	KeyF15
	KeyF16
	KeyF17
	KeyF18
	KeyF19
	KeyF20
	KeyF21
	KeyF22
	KeyF23
	KeyF24
	KeyF25
	KeyF26
	KeyF27
	KeyF28
	KeyF29
	KeyF30
	KeyF31
	KeyF32
	KeyF33
	KeyF34
	KeyF35
	KeyF36
	KeyF37
	KeyF38
	KeyF39
	KeyF40
	KeyF41
	KeyF42
	KeyF43
	KeyF44
	KeyF45
	KeyF46
	KeyF47
	KeyF48
	KeyF49
	KeyF50
	KeyF51
	KeyF52
	KeyF53
	KeyF54
	KeyF55
	KeyF56
	KeyF57
	KeyF58
	KeyF59
	KeyF60
	KeyF61
	KeyF62
	KeyF63
	KeyF64
)

These are named keys that can be handled.

const (
	KeyCtrlSpace Key = iota
	KeyCtrlA
	KeyCtrlB
	KeyCtrlC
	KeyCtrlD
	KeyCtrlE
	KeyCtrlF
	KeyCtrlG
	KeyCtrlH
	KeyCtrlI
	KeyCtrlJ
	KeyCtrlK
	KeyCtrlL
	KeyCtrlM
	KeyCtrlN
	KeyCtrlO
	KeyCtrlP
	KeyCtrlQ
	KeyCtrlR
	KeyCtrlS
	KeyCtrlT
	KeyCtrlU
	KeyCtrlV
	KeyCtrlW
	KeyCtrlX
	KeyCtrlY
	KeyCtrlZ
	KeyCtrlLeftSq // Escape
	KeyCtrlBackslash
	KeyCtrlRightSq
	KeyCtrlCarat
	KeyCtrlUnderscore
)

These are the supported control keys.

const (
	KeyNUL Key = iota
	KeySOH
	KeySTX
	KeyETX
	KeyEOT
	KeyENQ
	KeyACK
	KeyBEL
	KeyBS
	KeyTAB
	KeyLF
	KeyVT
	KeyFF
	KeyCR
	KeySO
	KeySI
	KeyDLE
	KeyDC1
	KeyDC2
	KeyDC3
	KeyDC4
	KeyNAK
	KeySYN
	KeyETB
	KeyCAN
	KeyEM
	KeySUB
	KeyESC
	KeyFS
	KeyGS
	KeyRS
	KeyUS
	KeyDEL Key = 0x7F
)

These are the defined ASCII values for key codes.

type KeyEvent

type KeyEvent struct {
	Target *Pencere
	Mod    tb.Modifier // one of Mod* constants or 0
	Key    tb.Key      // one of Key* constants, invalid if 'Ch' is not 0
	Rune   rune
}
type Menu struct {
	Title     string `json:"title"`
	MenuItems []*MenuItem
}
type MenuItem struct {
	Title string `json:"title"`
}
type MenuList struct {
	Menus []*Menu
}

type Modifier

type Modifier int16

Modifier is a mask of modifier keys.

type MouseEvent

type MouseEvent struct {
	Target           *Pencere
	GlobalX, GlobalY int
	X, Y             int
	Type             int
}

type MultiLineTextBox

type MultiLineTextBox struct {
	*Pencere
	// contains filtered or unexported fields
}

func NewMultiLineTextBox

func NewMultiLineTextBox(options ...Option) (*MultiLineTextBox, error)

func (*MultiLineTextBox) OnKeyEvent

func (this *MultiLineTextBox) OnKeyEvent(ev KeyEvent) error

func (*MultiLineTextBox) Render

func (this *MultiLineTextBox) Render(buf *Buffer) error

type Option

type Option func(*Pencere) error

Option ...

func LayoutFill

func LayoutFill() Option

func Position

func Position(left, top, width, height int) Option

func ResetFlag

func ResetFlag(flags int) Option

func SetFlag

func SetFlag(flags int) Option

type Pencere

type Pencere struct {
	Label   string
	LabelFg Color
	LabelBg Color

	Text string

	Inner image.Rectangle

	Fg      Color
	Bg      Color
	Texture rune

	Left, Top     int
	Width, Height int
	CanFocus      bool
	HasFocus      bool
	HasBorder     bool

	Scrollable bool
	ContentX   int
	ContentY   int

	BorderFg Color
	BorderBg Color

	Render  func(buffer *Buffer) error
	Enabled bool
	Visible bool

	Layout func() error

	Id   string
	Kind string
	Tag  string

	OnMouseLeftClick func(event MouseEvent) error
	OnFocus          func() error
	OnLostFocus      func() error

	OnKeyEvent  func(event KeyEvent) error
	Data        interface{}
	Controller  interface{}
	Draggable   bool
	Droppable   bool
	OnDragBegin func(event DragBeginEvent) (bool, *DragContext, error)
	OnDragging  func(event DraggingEvent) error
	OnDragEnd   func(event DragEndEvent) error
	OnDragEnter func(event DragEnterEvent) error
	OnDragLeave func(event DragLeaveEvent) error
	OnDragOver  func(event DragOverEvent) error
	OnDrop      func(event DropEvent) error
	CanDrop     func(event DropEvent) (bool, error)

	Properties Properties

	Theme *Theme
	// contains filtered or unexported fields
}

func NewBox

func NewBox(options ...Option) (*Pencere, error)

func NewButton

func NewButton(label string, options ...Option) (*Pencere, error)

func NewMenuBar

func NewMenuBar(options ...Option) (*Pencere, error)

func NewOutput

func NewOutput(options ...Option) (*Pencere, error)

func NewPencere

func NewPencere(options ...Option) (*Pencere, error)

func NewStatusBar

func NewStatusBar(options ...Option) (*Pencere, error)

func Root

func Root() *Pencere

func (*Pencere) AddWindow

func (this *Pencere) AddWindow(c Control)

func (*Pencere) Apply

func (this *Pencere) Apply(options ...Option) error

func (*Pencere) Childs

func (this *Pencere) Childs() []*Pencere

func (*Pencere) FireEvent

func (this *Pencere) FireEvent(eventType string, data interface{}) error

func (*Pencere) GetLayoutOrder

func (this *Pencere) GetLayoutOrder() int

func (*Pencere) GetZIndex

func (this *Pencere) GetZIndex() int

func (*Pencere) GlobalPosition

func (this *Pencere) GlobalPosition() (int, int)

func (*Pencere) Parent

func (this *Pencere) Parent() *Pencere

func (*Pencere) SetButtomBar

func (this *Pencere) SetButtomBar(p *Pencere)

func (*Pencere) SetLayoutOrder

func (this *Pencere) SetLayoutOrder(layoutOrder int)

func (*Pencere) SetLeftBar

func (this *Pencere) SetLeftBar(p *Pencere)

func (*Pencere) SetRightBar

func (this *Pencere) SetRightBar(p *Pencere)

func (*Pencere) SetTopBar

func (this *Pencere) SetTopBar(p *Pencere)

func (*Pencere) SetZIndex

func (this *Pencere) SetZIndex(zindex int)

func (*Pencere) TranslateToXY

func (this *Pencere) TranslateToXY(globalX int, globalY int) (int, int)

func (*Pencere) Window

func (this *Pencere) Window() *Pencere

type PencereOrder

type PencereOrder struct {
	Order   int
	Pencere *Pencere
}

type Properties

type Properties map[string]interface{}

func NewProperties

func NewProperties() Properties

func (Properties) GetColor

func (this Properties) GetColor(key string, def Color) Color

func (Properties) GetInt

func (this Properties) GetInt(key string, def int) int

func (Properties) GetString

func (this Properties) GetString(key string, def string) string

func (Properties) GetValue

func (this Properties) GetValue(name string) interface{}

func (Properties) SetColor

func (this Properties) SetColor(name string, value Color)

func (Properties) SetInt

func (this Properties) SetInt(name string, value int)

func (Properties) SetString

func (this Properties) SetString(name string, value string)

func (Properties) SetValue

func (this Properties) SetValue(name string, value interface{})

type RuneBuffer

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

RuneBuffer provides readline functionality for text widgets.

func (*RuneBuffer) Backspace

func (r *RuneBuffer) Backspace()

Backspace deletes the rune left of the cursor.

func (*RuneBuffer) CursorPos

func (r *RuneBuffer) CursorPos() image.Point

CursorPos returns the coordinate for the cursor for a given width.

func (*RuneBuffer) Delete

func (r *RuneBuffer) Delete()

Delete deletes the rune at the current cursor position.

func (*RuneBuffer) Kill

func (r *RuneBuffer) Kill()

Kill deletes all runes from the cursor until the end of the line.

func (*RuneBuffer) Len

func (r *RuneBuffer) Len() int

Len returns the number of runes in the buffer.

func (*RuneBuffer) MoveBackward

func (r *RuneBuffer) MoveBackward()

MoveBackward moves the cursor back by one rune.

func (*RuneBuffer) MoveForward

func (r *RuneBuffer) MoveForward()

MoveForward moves the cursor forward by one rune.

func (*RuneBuffer) MoveToLineEnd

func (r *RuneBuffer) MoveToLineEnd()

MoveToLineEnd moves the cursor to the end of the current line.

func (*RuneBuffer) MoveToLineStart

func (r *RuneBuffer) MoveToLineStart()

MoveToLineStart moves the cursor to the start of the current line.

func (*RuneBuffer) Pos

func (r *RuneBuffer) Pos() int

Pos returns the current index in the buffer.

func (*RuneBuffer) Runes

func (r *RuneBuffer) Runes() []rune

Runes return the buffer

func (*RuneBuffer) Set

func (r *RuneBuffer) Set(buf []rune)

Set the buffer and the index at the end of the buffer.

func (*RuneBuffer) SetMaxWidth

func (r *RuneBuffer) SetMaxWidth(w int)

SetMaxWidth sets the maximum text width.

func (*RuneBuffer) SetWithIdx

func (r *RuneBuffer) SetWithIdx(idx int, buf []rune)

SetWithIdx set the the buffer with a given index.

func (*RuneBuffer) SplitByLine

func (r *RuneBuffer) SplitByLine() []string

SplitByLine returns the lines for a given width.

func (*RuneBuffer) String

func (r *RuneBuffer) String() string

func (*RuneBuffer) Width

func (r *RuneBuffer) Width() int

Width returns the width of the rune buffer, taking into account for CJK.

func (*RuneBuffer) WriteRune

func (r *RuneBuffer) WriteRune(s rune)

WriteRune appends a rune to the buffer.

func (*RuneBuffer) WriteRunes

func (r *RuneBuffer) WriteRunes(s []rune)

WriteRunes appends runes to the buffer.

type Style

type Style struct {
	Fg Color
	Bg Color

	Texture   rune
	Brush     rune
	Reverse   Decoration
	Bold      Decoration
	Underline Decoration
}

type Table

type Table struct {
	*Pencere

	Header []string
	Rows   [][]string

	ColWidths  []int
	CellXPos   []int  // column position
	ColResizer func() // for widgets that inherit a Table and want to overload the ColResize method
	Gap        int    // gap between columns
	PadLeft    int

	Cursor      bool
	CursorColor Color

	UniqueCol    int    // the column used to identify the selected item
	SelectedItem string // used to keep the cursor on the correct item if the data changes
	SelectedRow  int
	TopRow       int // used to indicate where in the table we are scrolled at
}

Table tracks all the attributes of a Table instance

func NewTable

func NewTable() *Table

NewTable returns a new Table instance

func (*Table) Bottom

func (self *Table) Bottom()

func (*Table) Click

func (self *Table) Click(x, y int)

func (*Table) ColResize

func (self *Table) ColResize()

ColResize is the default column resizer, but can be overriden. ColResize calculates the width of each column.

func (*Table) Down

func (self *Table) Down()

func (*Table) HalfPageDown

func (self *Table) HalfPageDown()

func (*Table) HalfPageUp

func (self *Table) HalfPageUp()

func (*Table) OnKeyEvent

func (self *Table) OnKeyEvent(event KeyEvent) error

func (*Table) PageDown

func (self *Table) PageDown()

func (*Table) PageUp

func (self *Table) PageUp()

func (*Table) Render

func (self *Table) Render(buf *Buffer) error

Buffer implements the Bufferer interface.

func (*Table) Top

func (self *Table) Top()

func (*Table) Up

func (self *Table) Up()

type Theme

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

func NewTheme

func NewTheme() *Theme

NewTheme return an empty theme.

func (*Theme) HasStyle

func (p *Theme) HasStyle(name string) bool

HasStyle returns whether an identifier is associated with an identifier.

func (*Theme) SetStyle

func (p *Theme) SetStyle(n string, i Style)

SetStyle sets a style for a given identifier.

func (*Theme) Style

func (p *Theme) Style(name string) Style

Style returns the style associated with an identifier. If there is no Style associated with the name, it returns a default Style.

type VerticalSplitter

type VerticalSplitter struct {
	*Pencere
	LeftSide  *Pencere
	RightSide *Pencere
}

func NewVerticalSplitter

func NewVerticalSplitter(options ...Option) (*VerticalSplitter, error)

func (*VerticalSplitter) Layout

func (this *VerticalSplitter) Layout() error

func (*VerticalSplitter) OnDragBegin

func (this *VerticalSplitter) OnDragBegin(event DragBeginEvent) (bool, *DragContext, error)

func (*VerticalSplitter) Render

func (this *VerticalSplitter) Render(buf *Buffer) error

type Widget

type Widget interface {
	Control
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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