summary refs log tree commit diff stats
path: root/widgets/spinner.go
blob: bafc712b31c5d1b921073630a170dc748589a730 (plain) (blame)
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
package widgets

import (
	"time"

	"github.com/gdamore/tcell"

	"git.sr.ht/~sircmpwn/aerc2/lib/ui"
)

var (
	frames = []string{
		"[..]    ",
		" [..]   ",
		"  [..]  ",
		"   [..] ",
		"    [..]",
		"   [..] ",
		"  [..]  ",
		" [..]   ",
	}
)

type Spinner struct {
	frame        int
	onInvalidate func(d ui.Drawable)
	stop         chan interface{}
}

func NewSpinner() *Spinner {
	spinner := Spinner{
		stop:  make(chan interface{}),
		frame: -1,
	}
	return &spinner
}

func (s *Spinner) Start() {
	if s.IsRunning() {
		return
	}

	s.frame = 0
	go func() {
		for {
			select {
			case <-s.stop:
				return
			case <-time.After(200 * time.Millisecond):
				s.frame++
				if s.frame >= len(frames) {
					s.frame = 0
				}
				s.Invalidate()
			}
		}
	}()
}

func (s *Spinner) Stop() {
	if !s.IsRunning() {
		return
	}

	s.stop <- nil
	s.frame = -1
	s.Invalidate()
}

func (s *Spinner) IsRunning() bool {
	return s.frame != -1
}

func (s *Spinner) Draw(ctx *ui.Context) {
	if !s.IsRunning() {
		s.Start()
	}

	ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
	col := ctx.Width()/2 - len(frames[0])/2 + 1
	ctx.Printf(col, 0, tcell.StyleDefault, "%s", frames[s.frame])
}

func (s *Spinner) OnInvalidate(onInvalidate func(d ui.Drawable)) {
	s.onInvalidate = onInvalidate
}

func (s *Spinner) Invalidate() {
	if s.onInvalidate != nil {
		s.onInvalidate(s)
	}
}
pan>>&nbsp;</td> <td width="100%"><dl> <dt><font face="helvetica, arial"><a href="__builtin__.html#object">__builtin__.object</a> </font></dt><dd> <dl> <dt><font face="helvetica, arial"><a href="ranger.container.keybuffer.html#KeyBuffer">KeyBuffer</a> </font></dt></dl> </dd> </dl> <p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#ffc8d8"> <td colspan=3 valign=bottom>&nbsp;<br> <font color="#000000" face="helvetica, arial"><a name="KeyBuffer">class <strong>KeyBuffer</strong></a>(<a href="__builtin__.html#object">__builtin__.object</a>)</font></td></tr> <tr><td bgcolor="#ffc8d8"><tt>&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td> <td width="100%">Methods defined here:<br> <dl><dt><a name="KeyBuffer-__init__"><strong>__init__</strong></a>(self)</dt></dl> <dl><dt><a name="KeyBuffer-__str__"><strong>__str__</strong></a>(self)</dt><dd><tt>returns&nbsp;a&nbsp;concatenation&nbsp;of&nbsp;all&nbsp;characters</tt></dd></dl> <dl><dt><a name="KeyBuffer-append"><strong>append</strong></a>(self, key)</dt><dd><tt>Append&nbsp;a&nbsp;key&nbsp;to&nbsp;the&nbsp;keybuffer,&nbsp;or&nbsp;initial&nbsp;numbers&nbsp;to<br> the&nbsp;number&nbsp;attribute.</tt></dd></dl> <dl><dt><a name="KeyBuffer-clear"><strong>clear</strong></a>(self)</dt><dd><tt>Clear&nbsp;the&nbsp;keybuffer&nbsp;and&nbsp;restore&nbsp;the&nbsp;initial&nbsp;state</tt></dd></dl> <dl><dt><a name="KeyBuffer-tuple_with_numbers"><strong>tuple_with_numbers</strong></a>(self)</dt><dd><tt>Get&nbsp;a&nbsp;tuple&nbsp;of&nbsp;ascii&nbsp;codes.</tt></dd></dl> <dl><dt><a name="KeyBuffer-tuple_without_numbers"><strong>tuple_without_numbers</strong></a>(self)</dt><dd><tt>Get&nbsp;a&nbsp;tuple&nbsp;of&nbsp;ascii&nbsp;codes.<br> If&nbsp;the&nbsp;keybuffer&nbsp;starts&nbsp;with&nbsp;numbers,&nbsp;those&nbsp;will<br> be&nbsp;left&nbsp;out.&nbsp;To&nbsp;access&nbsp;them,&nbsp;use&nbsp;keybuffer.number</tt></dd></dl> <hr> Data descriptors defined here:<br> <dl><dt><strong>__dict__</strong></dt> <dd><tt>dictionary&nbsp;for&nbsp;instance&nbsp;variables&nbsp;(if&nbsp;defined)</tt></dd> </dl> <dl><dt><strong>__weakref__</strong></dt> <dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd> </dl> </td></tr></table></td></tr></table><p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#eeaa77"> <td colspan=3 valign=bottom>&nbsp;<br> <font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr> <tr><td bgcolor="#eeaa77"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td> <td width="100%"><dl><dt><a name="-to_string"><strong>to_string</strong></a>(i)</dt></dl> </td></tr></table><p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#55aa55"> <td colspan=3 valign=bottom>&nbsp;<br> <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr> <tr><td bgcolor="#55aa55"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td> <td width="100%"><strong>NINE</strong> = 57<br> <strong>ZERO</strong> = 48</td></tr></table> </body></html>