forked from soumya92/barista
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icons.go
105 lines (92 loc) · 2.82 KB
/
icons.go
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
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
Package icons provides an interface for using icon fonts in a bar.
To use an icon font:
- Clone a supported repository
- Link the ttf into ~/.fonts
- Load the icon by passing it the path to the repo
- Use icons as pango constructs in your bar
Compatible icon fonts:
- Material Design Icons (+community fork)
- FontAwesome
- Typicons
Example usage:
material.Load("/Users/me/Github/google/material-design-icons")
...
return pango.Icon("material-today").Color(colors.Hex("#ddd")).
Append(pango.Text(now.Sprintf("%H:%M")))
*/
package icons // import "barista.run/pango/icons"
import (
"strconv"
"barista.run/pango"
)
// Provider provides pango nodes for icons
type Provider struct {
symbols map[string]string
styles []func(*pango.Node)
}
// NewProvider creates a new icon provider with the given name,
// registers it with pango.Icon, and returns it so that an appropriate
// Load method can be used.
func NewProvider(name string) *Provider {
p := &Provider{symbols: map[string]string{}}
pango.AddIconProvider(name, p.icon)
return p
}
// icon creates a pango node that renders the named icon.
func (p *Provider) icon(name string) *pango.Node {
symbol, ok := p.symbols[name]
if !ok {
return nil
}
n := pango.Text(symbol)
for _, s := range p.styles {
s(n)
}
return n
}
// Hex adds a symbol to the provider where the value is given
// in hex-encoded form.
func (p *Provider) Hex(name, value string) error {
sym, err := SymbolFromHex(value)
if err != nil {
return err
}
p.Symbol(name, sym)
return nil
}
// Symbol adds a symbol to the provider where the value is the
// symbol/string to use for the icon.
func (p *Provider) Symbol(name, value string) {
p.symbols[name] = value
}
// Font sets the font set on the returned pango nodes.
func (p *Provider) Font(font string) {
p.AddStyle(func(n *pango.Node) { n.Font(font) })
}
// AddStyle sets additional styles on all returned pango nodes.
func (p *Provider) AddStyle(style func(*pango.Node)) {
p.styles = append(p.styles, style)
}
// SymbolFromHex parses a hex string (e.g. "1F44D") and converts
// it to a string (e.g. "👍").
func SymbolFromHex(hex string) (string, error) {
intVal, err := strconv.ParseUint(hex, 16, 32)
if err != nil {
return "", err
}
return string(intVal), nil
}