aboutsummaryrefslogtreecommitdiffstats
path: root/lib/statusline/state.go
blob: 30029c13cdde12265bb8c22b27f3d68b98bdaae7 (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
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
package statusline

import (
	"fmt"
	"strings"
)

type State struct {
	Name      string
	Multiple  bool
	Separator string

	Connection   string
	ConnActivity string
	Connected    bool

	Search         string
	Filter         string
	FilterActivity string

	Threading   string
	Passthrough string
}

func NewState(name string, multipleAccts bool, sep string) *State {
	return &State{Name: name, Multiple: multipleAccts, Separator: sep}
}

func (s *State) String() string {
	var line []string
	if s.Connection != "" || s.ConnActivity != "" {
		conn := s.Connection
		if s.ConnActivity != "" {
			conn = s.ConnActivity
		}
		if s.Multiple {
			line = append(line, fmt.Sprintf("[%s] %s", s.Name, conn))
		} else {
			line = append(line, conn)
		}
	}
	if s.Connected {
		if s.FilterActivity != "" {
			line = append(line, s.FilterActivity)
		} else {
			if s.Filter != "" {
				line = append(line, s.Filter)
			}
		}
		if s.Search != "" {
			line = append(line, s.Search)
		}
		if s.Threading != "" {
			line = append(line, s.Threading)
		}
		if s.Passthrough != "" {
			line = append(line, s.Passthrough)
		}
	}
	return strings.Join(line, s.Separator)
}

type SetStateFunc func(s *State)

func Connected(state bool) SetStateFunc {
	return func(s *State) {
		s.ConnActivity = ""
		s.Connected = state
		if state {
			s.Connection = "Connected"
		} else {
			s.Connection = "Disconnected"
		}
	}
}

func ConnectionActivity(desc string) SetStateFunc {
	return func(s *State) {
		s.ConnActivity = desc
	}
}

func SearchFilterClear() SetStateFunc {
	return func(s *State) {
		s.Search = ""
		s.FilterActivity = ""
		s.Filter = ""
	}
}

func FilterActivity(str string) SetStateFunc {
	return func(s *State) {
		s.FilterActivity = str
	}
}

func FilterResult(str string) SetStateFunc {
	return func(s *State) {
		s.FilterActivity = ""
		s.Filter = concatFilters(s.Filter, str)
	}
}

func concatFilters(existing, next string) string {
	if existing == "" {
		return next
	}
	return fmt.Sprintf("%s && %s", existing, next)
}

func Search(desc string) SetStateFunc {
	return func(s *State) {
		s.Search = desc
	}
}

func Threading(on bool) SetStateFunc {
	return func(s *State) {
		s.Threading = ""
		if on {
			s.Threading = "threading"
		}
	}
}

func Passthrough(on bool) SetStateFunc {
	return func(s *State) {
		s.Passthrough = ""
		if on {
			s.Passthrough = "passthrough"
		}
	}
}