Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clone slices to loosen reference #324

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions style.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ const (
ColorWhite Color = 7
)

// Colors represents a slice of colors
type Colors []Color

// Clone the colors slice to not keep a reference
func (c Colors) Clone() Colors {
return append(make([]Color, 0), c...)
}

type Modifier uint

const (
Expand All @@ -37,6 +45,14 @@ type Style struct {
Modifier Modifier
}

// Styles represents a slice of styles
type Styles []Style

// Clone the styles slice to not keep a reference
func (s Styles) Clone() Styles {
return append(make([]Style, 0), s...)
}

// StyleClear represents a default Style, with no colors or modifiers
var StyleClear = Style{
Fg: ColorClear,
Expand Down
36 changes: 18 additions & 18 deletions theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package termui

var StandardColors = []Color{
var StandardColors = Colors{
ColorRed,
ColorGreen,
ColorYellow,
Expand All @@ -14,7 +14,7 @@ var StandardColors = []Color{
ColorWhite,
}

var StandardStyles = []Style{
var StandardStyles = Styles{
NewStyle(ColorRed),
NewStyle(ColorGreen),
NewStyle(ColorYellow),
Expand Down Expand Up @@ -48,9 +48,9 @@ type BlockTheme struct {
}

type BarChartTheme struct {
Bars []Color
Nums []Style
Labels []Style
Bars Colors
Nums Styles
Labels Styles
}

type GaugeTheme struct {
Expand All @@ -59,7 +59,7 @@ type GaugeTheme struct {
}

type PlotTheme struct {
Lines []Color
Lines Colors
Axes Color
}

Expand All @@ -78,7 +78,7 @@ type ParagraphTheme struct {
}

type PieChartTheme struct {
Slices []Color
Slices Colors
}

type SparklineTheme struct {
Expand All @@ -87,9 +87,9 @@ type SparklineTheme struct {
}

type StackedBarChartTheme struct {
Bars []Color
Nums []Style
Labels []Style
Bars Colors
Nums Styles
Labels Styles
}

type TabTheme struct {
Expand All @@ -112,17 +112,17 @@ var Theme = RootTheme{
},

BarChart: BarChartTheme{
Bars: StandardColors,
Nums: StandardStyles,
Labels: StandardStyles,
Bars: StandardColors.Clone(),
Nums: StandardStyles.Clone(),
Labels: StandardStyles.Clone(),
},

Paragraph: ParagraphTheme{
Text: NewStyle(ColorWhite),
},

PieChart: PieChartTheme{
Slices: StandardColors,
Slices: StandardColors.Clone(),
},

List: ListTheme{
Expand All @@ -136,9 +136,9 @@ var Theme = RootTheme{
},

StackedBarChart: StackedBarChartTheme{
Bars: StandardColors,
Nums: StandardStyles,
Labels: StandardStyles,
Bars: StandardColors.Clone(),
Nums: StandardStyles.Clone(),
Labels: StandardStyles.Clone(),
},

Gauge: GaugeTheme{
Expand All @@ -152,7 +152,7 @@ var Theme = RootTheme{
},

Plot: PlotTheme{
Lines: StandardColors,
Lines: StandardColors.Clone(),
Axes: ColorWhite,
},

Expand Down
4 changes: 2 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func TrimString(s string, w int) string {
return s
}

func SelectColor(colors []Color, index int) Color {
func SelectColor(colors Colors, index int) Color {
return colors[index%len(colors)]
}

func SelectStyle(styles []Style, index int) Style {
func SelectStyle(styles Styles, index int) Style {
return styles[index%len(styles)]
}

Expand Down
12 changes: 6 additions & 6 deletions widgets/barchart.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (

type BarChart struct {
Block
BarColors []Color
LabelStyles []Style
NumStyles []Style // only Fg and Modifier are used
BarColors Colors
LabelStyles Styles
NumStyles Styles // only Fg and Modifier are used
NumFormatter func(float64) string
Data []float64
Labels []string
Expand All @@ -29,9 +29,9 @@ type BarChart struct {
func NewBarChart() *BarChart {
return &BarChart{
Block: *NewBlock(),
BarColors: Theme.BarChart.Bars,
NumStyles: Theme.BarChart.Nums,
LabelStyles: Theme.BarChart.Labels,
BarColors: Theme.BarChart.Bars.Clone(),
NumStyles: Theme.BarChart.Nums.Clone(),
LabelStyles: Theme.BarChart.Labels.Clone(),
NumFormatter: func(n float64) string { return fmt.Sprint(n) },
BarGap: 1,
BarWidth: 3,
Expand Down
2 changes: 1 addition & 1 deletion widgets/piechart.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type PieChartLabel func(dataIndex int, currentValue float64) string
type PieChart struct {
Block
Data []float64 // list of data items
Colors []Color // colors to by cycled through
Colors Colors // colors to by cycled through
LabelFormatter PieChartLabel // callback function for labels
AngleOffset float64 // which angle to start drawing at? (see piechartOffsetUp)
}
Expand Down
4 changes: 2 additions & 2 deletions widgets/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Plot struct {
DataLabels []string
MaxVal float64

LineColors []Color
LineColors Colors
AxesColor Color // TODO
ShowAxes bool

Expand Down Expand Up @@ -64,7 +64,7 @@ const (
func NewPlot() *Plot {
return &Plot{
Block: *NewBlock(),
LineColors: Theme.Plot.Lines,
LineColors: Theme.Plot.Lines.Clone(),
AxesColor: Theme.Plot.Axes,
Marker: MarkerBraille,
DotMarkerRune: DOT,
Expand Down
12 changes: 6 additions & 6 deletions widgets/stacked_barchart.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (

type StackedBarChart struct {
Block
BarColors []Color
LabelStyles []Style
NumStyles []Style // only Fg and Modifier are used
BarColors Colors
LabelStyles Styles
NumStyles Styles // only Fg and Modifier are used
NumFormatter func(float64) string
Data [][]float64
Labels []string
Expand All @@ -29,9 +29,9 @@ type StackedBarChart struct {
func NewStackedBarChart() *StackedBarChart {
return &StackedBarChart{
Block: *NewBlock(),
BarColors: Theme.StackedBarChart.Bars,
LabelStyles: Theme.StackedBarChart.Labels,
NumStyles: Theme.StackedBarChart.Nums,
BarColors: Theme.StackedBarChart.Bars.Clone(),
LabelStyles: Theme.StackedBarChart.Labels.Clone(),
NumStyles: Theme.StackedBarChart.Nums.Clone(),
NumFormatter: func(n float64) string { return fmt.Sprint(n) },
BarGap: 1,
BarWidth: 3,
Expand Down