File: divider.go

package info (click to toggle)
golang-github-gcla-gowid 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,456 kB
  • sloc: makefile: 4
file content (147 lines) | stat: -rw-r--r-- 3,023 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2019-2022 Graham Clark. All rights reserved.  Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.

// Package divider provides a widget that draws a dividing line between other widgets.
package divider

import (
	"fmt"
	"runtime"

	"github.com/gcla/gowid"
)

//======================================================================

var (
	HorizontalLine    = '━'
	AltHorizontalLine = '▀'
)

func init() {
	switch runtime.GOOS {
	case "windows":
		HorizontalLine = '─'
		AltHorizontalLine = HorizontalLine
	case "darwin":
		AltHorizontalLine = HorizontalLine
	}
}

type IDivider interface {
	Opts() Options
}

type IWidget interface {
	gowid.IWidget
	IDivider
}

type Widget struct {
	opts Options
	gowid.RejectUserInput
	gowid.NotSelectable
}

type Options struct {
	Chr          rune
	Above, Below int
}

func New(opts ...Options) *Widget {
	var opt Options
	if len(opts) == 0 {
		opt = Options{
			Chr: '-',
		}
	} else {
		opt = opts[0]
	}
	res := &Widget{
		opts: opt,
	}
	var _ IWidget = res
	return res
}

func NewAscii() *Widget {
	return New(Options{
		Chr: '-',
	})
}

func NewBlank() *Widget {
	return New(Options{
		Chr: ' ',
	})
}

func NewUnicode() *Widget {
	return New(Options{
		Chr: HorizontalLine,
	})
}

func NewUnicodeAlt() *Widget {
	return New(Options{
		Chr: AltHorizontalLine,
	})
}

func (w *Widget) String() string {
	return fmt.Sprintf("div[%c]", w.Opts().Chr)
}

func (w *Widget) Opts() Options {
	return w.opts
}

func (w *Widget) RenderSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
	return RenderSize(w, size, focus, app)
}

func (w *Widget) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
	return Render(w, size, focus, app)
}

//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

func RenderSize(w IDivider, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
	if flow, ok := size.(gowid.IRenderFlowWith); !ok {
		panic(gowid.WidgetSizeError{Widget: w, Size: size, Required: "gowid.IRenderFlowWith"})
	} else {
		return gowid.RenderBox{C: flow.FlowColumns(), R: w.Opts().Above + w.Opts().Below + 1}
	}
}

func Render(w IDivider, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
	flow, ok := size.(gowid.IRenderFlowWith)
	if !ok {
		panic(gowid.WidgetSizeError{Widget: w, Size: size, Required: "gowid.IRenderFlowWith"})
	}

	div := gowid.CellFromRune(w.Opts().Chr)
	divArr := make([]gowid.Cell, flow.FlowColumns())
	for i := 0; i < flow.FlowColumns(); i++ {
		divArr[i] = div
	}

	res := gowid.NewCanvas()
	for i := 0; i < w.Opts().Above; i++ {
		res.AppendLine([]gowid.Cell{}, false)
	}
	res.AppendLine(divArr, false)
	for i := 0; i < w.Opts().Below; i++ {
		res.AppendLine([]gowid.Cell{}, false)
	}
	res.AlignRight()

	return res
}

//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End: