1// Copyright 2019 Google Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Binary buttondemo shows the functionality of a button widget.
16package main
17
18import (
19	"context"
20	"fmt"
21	"time"
22
23	"github.com/mum4k/termdash"
24	"github.com/mum4k/termdash/align"
25	"github.com/mum4k/termdash/cell"
26	"github.com/mum4k/termdash/container"
27	"github.com/mum4k/termdash/linestyle"
28	"github.com/mum4k/termdash/terminal/tcell"
29	"github.com/mum4k/termdash/terminal/terminalapi"
30	"github.com/mum4k/termdash/widgets/button"
31	"github.com/mum4k/termdash/widgets/segmentdisplay"
32)
33
34func main() {
35	t, err := tcell.New()
36	if err != nil {
37		panic(err)
38	}
39	defer t.Close()
40
41	ctx, cancel := context.WithCancel(context.Background())
42
43	val := 0
44	display, err := segmentdisplay.New()
45	if err != nil {
46		panic(err)
47	}
48	if err := display.Write([]*segmentdisplay.TextChunk{
49		segmentdisplay.NewChunk(fmt.Sprintf("%d", val)),
50	}); err != nil {
51		panic(err)
52	}
53
54	addB, err := button.New("(a)dd", func() error {
55		val++
56		return display.Write([]*segmentdisplay.TextChunk{
57			segmentdisplay.NewChunk(fmt.Sprintf("%d", val)),
58		})
59	},
60		button.GlobalKey('a'),
61		button.WidthFor("(s)ubtract"),
62	)
63	if err != nil {
64		panic(err)
65	}
66
67	subB, err := button.New("(s)ubtract", func() error {
68		val--
69		return display.Write([]*segmentdisplay.TextChunk{
70			segmentdisplay.NewChunk(fmt.Sprintf("%d", val)),
71		})
72	},
73		button.FillColor(cell.ColorNumber(220)),
74		button.GlobalKey('s'),
75	)
76	if err != nil {
77		panic(err)
78	}
79
80	c, err := container.New(
81		t,
82		container.Border(linestyle.Light),
83		container.BorderTitle("PRESS Q TO QUIT"),
84		container.SplitHorizontal(
85			container.Top(
86				container.PlaceWidget(display),
87			),
88			container.Bottom(
89				container.SplitVertical(
90					container.Left(
91						container.PlaceWidget(addB),
92						container.AlignHorizontal(align.HorizontalRight),
93					),
94					container.Right(
95						container.PlaceWidget(subB),
96						container.AlignHorizontal(align.HorizontalLeft),
97					),
98				),
99			),
100			container.SplitPercent(60),
101		),
102	)
103	if err != nil {
104		panic(err)
105	}
106
107	quitter := func(k *terminalapi.Keyboard) {
108		if k.Key == 'q' || k.Key == 'Q' {
109			cancel()
110		}
111	}
112
113	if err := termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter), termdash.RedrawInterval(100*time.Millisecond)); err != nil {
114		panic(err)
115	}
116}
117