2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-16 21:25:38 +00:00
cointop/pkg/ui/view.go

246 lines
4.4 KiB
Go
Raw Normal View History

2020-11-17 03:12:24 +00:00
package ui
import (
"fmt"
"github.com/cointop-sh/cointop/pkg/gocui"
"github.com/gdamore/tcell/v2"
2020-11-17 03:12:24 +00:00
)
2020-11-21 22:22:28 +00:00
// IView is the view interface
2020-11-17 03:12:24 +00:00
type IView interface {
Backing() *gocui.View
SetBacking(gocuiView *gocui.View)
Name() string
}
2020-11-21 22:22:28 +00:00
// View is a view sruct
2020-11-17 03:12:24 +00:00
type View struct {
backing *gocui.View
name string
}
// NewView creates a new view
func NewView(name string) *View {
return &View{
name: name,
}
}
// Backing returns the backing gocui view
func (view *View) Backing() *gocui.View {
return view.backing
}
// SetBacking sets the backing gocui view
func (view *View) SetBacking(gocuiView *gocui.View) {
view.backing = gocuiView
}
// HasBacking returns the true if this view has a gocui backing
func (view *View) HasBacking() bool {
return view.backing != nil
}
2021-02-22 07:13:39 +00:00
// Size returns the view size
func (view *View) Size() (int, int) {
if view.HasBacking() {
return view.backing.Size()
}
return 0, 0
}
2020-11-17 03:12:24 +00:00
// Height returns the view height
func (view *View) Height() int {
if view.HasBacking() {
2021-02-22 07:13:39 +00:00
_, h := view.Size()
2020-11-17 03:12:24 +00:00
return h
}
return 0
}
// Width returns the view width
func (view *View) Width() int {
if view.HasBacking() {
2021-02-22 07:13:39 +00:00
w, _ := view.Size()
2020-11-17 03:12:24 +00:00
return w
}
return 0
}
// Cursor returns the view's cursor points
func (view *View) Cursor() (int, int) {
if view.HasBacking() {
return view.backing.Cursor()
}
return 0, 0
}
// CursorX returns the view's cursor X point
func (view *View) CursorX() int {
if view.HasBacking() {
x, _ := view.backing.Cursor()
return x
}
return 0
}
// CursorY returns the view's cursor Y point
func (view *View) CursorY() int {
if view.HasBacking() {
_, y := view.backing.Cursor()
return y
}
return 0
}
// SetCursor sets the view's cursor
func (view *View) SetCursor(x, y int) error {
if view.HasBacking() {
2021-02-22 07:13:39 +00:00
maxX, maxY := view.Size()
if x < 0 || x >= maxX || y < 0 || y >= maxY {
return nil
}
2020-11-17 03:12:24 +00:00
return view.backing.SetCursor(x, y)
}
return nil
}
// Origin returns the view's origin points
func (view *View) Origin() (int, int) {
if view.HasBacking() {
return view.backing.Origin()
}
return 0, 0
}
// OriginX returns the view's origin X point
func (view *View) OriginX() int {
if view.HasBacking() {
x, _ := view.backing.Origin()
return x
}
return 0
}
// OriginY returns the view's origin Y point
func (view *View) OriginY() int {
if view.HasBacking() {
_, y := view.backing.Origin()
return y
}
return 0
}
// SetOrigin sets the view's origin
func (view *View) SetOrigin(x, y int) error {
if view.HasBacking() {
2021-02-22 07:13:39 +00:00
if x < 0 || y < 0 {
return nil
}
2020-11-17 03:12:24 +00:00
return view.backing.SetOrigin(x, y)
}
return nil
}
// Name returns the view's name
func (view *View) Name() string {
return view.name
}
// Clear clears the view content
func (view *View) Clear() error {
if view.HasBacking() {
view.backing.Clear()
}
return nil
}
// Write will write the content to the view
func (view *View) Write(content string) error {
if view.HasBacking() {
fmt.Fprintln(view.backing, content)
}
return nil
}
// Update will clear and write the content to the view
func (view *View) Update(content string) error {
view.Clear()
view.Write(content)
return nil
}
// SetFrame enables the frame border for the view
func (view *View) SetFrame(enabled bool) error {
if view.HasBacking() {
view.backing.Frame = enabled
}
return nil
}
// SetHighlight enables the highlight color for the view
func (view *View) SetHighlight(enabled bool) error {
if view.HasBacking() {
view.backing.Highlight = enabled
}
return nil
}
// SetEditable makes the view editable
func (view *View) SetEditable(enabled bool) error {
if view.HasBacking() {
view.backing.Editable = enabled
}
return nil
}
// SetWrap enables text wrapping for the view
func (view *View) SetWrap(enabled bool) error {
if view.HasBacking() {
view.backing.Wrap = enabled
}
return nil
}
// SetStyle sets the text style for the view
func (view *View) SetStyle(style tcell.Style) {
2020-11-17 03:12:24 +00:00
if view.HasBacking() {
view.backing.Style = style
2020-11-17 03:12:24 +00:00
}
}
// SetSelStyle sets the selection text style for the view
func (view *View) SetSelStyle(style tcell.Style) {
2020-11-17 03:12:24 +00:00
if view.HasBacking() {
view.backing.SelStyle = style
2020-11-17 03:12:24 +00:00
}
}
// Read reads data in bytes buffer
func (view *View) Read(b []byte) (int, error) {
if view.HasBacking() {
return view.backing.Read(b)
}
return 0, nil
}
// Rewind undos view update
func (view *View) Rewind() error {
if view.HasBacking() {
view.backing.Rewind()
}
return nil
}