File: model.go

package info (click to toggle)
termshark 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,800 kB
  • sloc: sh: 98; makefile: 9
file content (151 lines) | stat: -rw-r--r-- 3,720 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
148
149
150
151
// Copyright 2019-2021 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 psmlmodel

import (
	"sort"

	"github.com/gcla/gowid"
	"github.com/gcla/gowid/widgets/button"
	"github.com/gcla/gowid/widgets/columns"
	"github.com/gcla/gowid/widgets/holder"
	"github.com/gcla/gowid/widgets/isselected"
	"github.com/gcla/gowid/widgets/styled"
	"github.com/gcla/gowid/widgets/table"
	"github.com/gcla/gowid/widgets/text"
	"github.com/gcla/termshark/v2/widgets/expander"
)

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

// Model is a table model that provides a widget that will render
// in one row only when not selected.
type Model struct {
	*table.SimpleModel
	styler gowid.ICellStyler
}

func New(m *table.SimpleModel, st gowid.ICellStyler) *Model {
	return &Model{
		SimpleModel: m,
		styler:      st,
	}
}

// Provides the ith "cell" widget, upstream makes the "row"
func (c *Model) CellWidget(i int, s string) gowid.IWidget {
	w := table.SimpleCellWidget(c, i, s)
	if w != nil {
		w = expander.New(w)
	}
	return w
}

func (c *Model) CellWidgets(row table.RowId) []gowid.IWidget {
	return table.SimpleCellWidgets(c, row)
}

// table.ITable2
func (c *Model) HeaderWidget(ws []gowid.IWidget, focus int) gowid.IWidget {
	hws := c.HeaderWidgets()
	hw := c.SimpleModel.HeaderWidget(hws, focus).(*columns.Widget)
	hw2 := isselected.NewExt(
		hw,
		styled.New(hw, c.styler),
		styled.New(hw, c.styler),
	)
	return hw2
}

func (c *Model) HeaderWidgets() []gowid.IWidget {
	var res []gowid.IWidget
	if c.Headers != nil {

		res = make([]gowid.IWidget, 0, len(c.Headers))
		bhs := make([]*holder.Widget, len(c.Headers))
		bms := make([]*button.Widget, len(c.Headers))
		for i, s := range c.Headers {
			i2 := i
			var all, label gowid.IWidget
			label = text.New(s + " ")

			sorters := c.Comparators
			if sorters != nil {
				sorteri := sorters[i2]
				if sorteri != nil {
					bmid := button.NewBare(text.New("-"))
					bfor := button.NewBare(text.New("^"))
					brev := button.NewBare(text.New("v"))
					bh := holder.New(bmid)
					bhs[i] = bh
					bms[i] = bmid

					action := func(rev bool, next *button.Widget, app gowid.IApp) {
						sorter := &table.SimpleTableByColumn{
							SimpleModel: c.SimpleModel,
							Column:      i2,
						}
						if rev {
							sort.Sort(sort.Reverse(sorter))
						} else {
							sort.Sort(sorter)
						}
						bh.SetSubWidget(next, app)
						for j, bhj := range bhs {
							if j != i2 {
								bhj.SetSubWidget(bms[j], app)
							}
						}
					}

					bmid.OnClick(gowid.MakeWidgetCallback("cb", func(app gowid.IApp, widget gowid.IWidget) {
						action(false, bfor, app)
					}))

					bfor.OnClick(gowid.MakeWidgetCallback("cb", func(app gowid.IApp, widget gowid.IWidget) {
						action(true, brev, app)
					}))

					brev.OnClick(gowid.MakeWidgetCallback("cb", func(app gowid.IApp, widget gowid.IWidget) {
						action(false, bfor, app)
					}))

					all = columns.NewFixed(label, styled.NewFocus(bh, gowid.MakeStyledAs(gowid.StyleReverse)))
				}
			}
			var w gowid.IWidget
			if c.Style.HeaderStyleProvided {
				w = isselected.New(
					styled.New(
						all,
						c.GetStyle().HeaderStyleNoFocus,
					),
					styled.New(
						all,
						c.GetStyle().HeaderStyleSelected,
					),
					styled.New(
						all,
						c.GetStyle().HeaderStyleFocus,
					),
				)
			} else {
				w = styled.NewExt(
					all,
					nil,
					gowid.MakeStyledAs(gowid.StyleReverse),
				)
			}
			res = append(res, w)
		}
	}
	return res
}

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