-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaffybar.hs.colorsbroken
More file actions
321 lines (251 loc) · 9.88 KB
/
Copy pathtaffybar.hs.colorsbroken
File metadata and controls
321 lines (251 loc) · 9.88 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
import Data.Bits (shiftR, (.&.))
import Data.Colour.RGBSpace (uncurryRGB)
import Data.Colour.RGBSpace.HSV (hsv)
import Data.Functor ((<$>))
import Data.IORef (IORef, newIORef, modifyIORef, readIORef, writeIORef)
import Data.List (find, isPrefixOf)
import Data.List.Split (splitOn)
import Data.Word (Word32)
import Foreign.C.String (newCString)
import qualified Graphics.UI.Gtk as Gtk
import qualified System.Information.Battery as B
import System.Information.CPU (cpuLoad)
import System.Information.Network (getNetInfo)
import System.Information.StreamInfo (getTransfer)
import System.Process (rawSystem)
import System.Process (readProcess)
import System.Random (getStdRandom, randomR)
import qualified System.Taffybar as TB
import System.Taffybar.Battery (batteryBarNew, defaultBatteryConfig)
import System.Taffybar.CPUMonitor ()
import System.Taffybar.DiskIOMonitor (dioMonitorNew)
import System.Taffybar.FSMonitor (fsMonitorNew)
import System.Taffybar.SimpleClock (textClockNew)
import System.Taffybar.Systray (systrayNew)
import qualified System.Taffybar.TaffyPager as Pg
import qualified System.Taffybar.Weather as W
import System.Taffybar.Widgets.Graph ()
import qualified System.Taffybar.Widgets.PollingBar as Bar
import qualified System.Taffybar.Widgets.PollingGraph as Gr
import System.Taffybar.Widgets.PollingLabel (pollingLabelNew)
import Text.Printf (printf)
import Text.Regex.PCRE
import Text.Regex.PCRE.Wrap (wrapCompile)
ifaceWifi = "wlp2s0"
ifaceWired = "enp0s25"
ifaces = [ifaceWifi, ifaceWired]
main = do
--hue <- getStdRandom $ randomR (0, 179)
--hueRef <- newIORef $ hue * 2
netRef <- newIORef []
bcOpt <- B.batteryContextNew
TB.defaultTaffybar TB.defaultTaffybarConfig {
TB.barHeight = 24,
TB.widgetSpacing = 4,
TB.startWidgets = [ pager
--, labelNew freqSlow $ rootColor hueRef
] ,
TB.endWidgets = [ space 2
, clock
, space 2
, cpu
, netGraph netRef
, space 1
, labelNew freqMed $ battState bcOpt
, battBar bcOpt
, space 1
, labelNew freqSlow $ wifiName ifaceWifi
, wifiStrength ifaceWifi
, space 2
, systrayNew
, space 2
, weather
]
}
sep = " | "
freqFast = 1
freqMed = 10
freqSlow = 30
labelNew' :: String -> Double -> IO String -> IO Gtk.Widget
labelNew' initial interval action = do
lab <- pollingLabelNew initial interval action
Gtk.widgetShowAll lab
return lab
labelNew = labelNew' ""
space :: Int -> IO Gtk.Widget
space k = labelNew' s 10000 $ return s
where s = replicate k ' '
battState :: Maybe B.BatteryContext -> IO String
battState Nothing = return "!upower"
battState (Just bc) = do
bi <- B.getBatteryInfo bc
let ud = battUpDown $ B.batteryState bi
pct = printf "%.0f%%" $ B.batteryPercentage bi
return $ ud ++ pct
battUpDown B.BatteryStateCharging = "↑"
battUpDown B.BatteryStateDischarging = "↓"
battUpDown B.BatteryStateEmpty = ""
battUpDown B.BatteryStateFullyCharged = ""
battUpDown B.BatteryStatePendingCharge = "↑"
battUpDown B.BatteryStatePendingDischarge = "↓"
battUpDown B.BatteryStateUnknown = "?"
battPercent :: Maybe B.BatteryContext -> IO Double
battPercent Nothing = return 0
battPercent (Just bc) = do
bi <- B.getBatteryInfo bc
return $ B.batteryPercentage bi / 100
battBar = Bar.pollingBarNew cfg freqMed . battPercent
where cfg = (Bar.defaultBarConfig redGreen) {
Bar.barWidth = 14
}
weather = W.weatherNew cfg 5
where cfg = (W.defaultWeatherConfig "KJFK") {
W.weatherTemplate = "$skyCondition$ $tempF$°F"
}
wifiName :: String -> IO String
wifiName iface = do
buf <- readProcess "iwgetid" ["-r", iface] ""
return $ case init buf of
"" -> "off"
s | length s > 10 -> take 10 s ++ "…"
s -> s
wifiQuality :: String -> IO Double
wifiQuality iface = do
buf <- lines <$> readFile "/proc/net/wireless"
return $ case find (isPrefixOf iface) buf of
Nothing -> 0
Just dat -> (read $ init $ words dat !! 2) / 70
wifiStrength :: String -> IO Gtk.Widget
wifiStrength = Bar.pollingBarNew cfg freqMed . wifiQuality
where k = colorBySat hue_deep_sky 0.9
cfg = (Bar.defaultBarConfig k) {
Bar.barWidth = 10
}
pager = Pg.taffyPagerNew cfg
where cfg = Pg.defaultPagerConfig {
Pg.emptyWorkspace = fgcolor silver . spaces,
Pg.hiddenWorkspace = fgcolor martinique . spaces,
Pg.activeWorkspace = fgcolor forest . surround "[" "]",
Pg.visibleWorkspace = fgcolor governor . surround "(" ")",
Pg.urgentWorkspace = fgcolor milano_red . surround "¡" "!",
Pg.activeLayout = fgcolor brandy_punch,
Pg.widgetSep = replicate 2 ' '
}
clock = textClockNew Nothing "<b>%a %_d</b> %b %_H:%M" freqMed
batt = batteryBarNew cfg 30
where cfg = defaultBatteryConfig {
Bar.barColor = redGreen
}
myGraph :: Bool -> String -> [(Color, Double)] -> Gr.GraphConfig
myGraph wide label colors = Gr.defaultGraphConfig {
Gr.graphWidth = if wide then 40 else 25,
Gr.graphLabel = if null label then Nothing else Just label,
Gr.graphDirection = Gr.RIGHT_TO_LEFT,
Gr.graphDataColors = map (\(c,d) -> dquad d c) colors,
Gr.graphPadding = 2
}
cpu = Gr.pollingGraphNew cfg freqFast (f <$> cpuLoad)
where f (_, sys, tot) = [tot]
cfg = myGraph True "" [(mango,1)]
diskIO = dioMonitorNew cfg freqFast "sda"
where cfg = myGraph True "ssd" [(green,1), (magenta,0.5)]
--wifi = Gr.pollingGraphNew cfg freqMed $ get
-- where get = getTransfer 1 $ getNetInfo "wlp2s0"
-- cfg = myGraph False "wl" [(red,1), (magenta,0.5)]
wired = Gr.pollingGraphNew cfg freqMed $ get
where get = getTransfer 1 $ getNetInfo "enp0s25"
cfg = myGraph False "en" [(red,1), (magenta,0.5)]
-- Produces text: [/ 76%] [/home 89%] [/var 50%]
diskUse = fsMonitorNew 300 ["/", "/home", "/var"]
-- Colors that vary across a range
colorByHue lo hi sat val d = uncurryRGB (,,) rgb
where rgb = hsv (d * range + lo) sat val
range = hi - lo
colorBySat :: Double -> Double -> Double -> Color3
colorBySat hue val d = uncurryRGB (,,) $ hsv hue d val
colorByVal hue sat = uncurryRGB (,,) . hsv hue sat
redGreen = colorByHue hue_torch_red hue_green 0.8 0.8
-- Just for testing color ranges
ud r = Bar.pollingBarNew cfg 0.1 $ trackUpDown r
where cfg = Bar.defaultBarConfig $ colorBySat hue_magenta 0.9
delta = 0.05
upDown (d, True) = if d >= 0.95 then (d - delta, False) else (d + delta, True)
upDown (d, False) = if d <= 0.05 then (d + delta, True) else (d - delta, False)
trackUpDown :: IORef (Double, Bool) -> IO Double
trackUpDown ref = do
modifyIORef ref upDown
fst <$> readIORef ref
-- Represent colors using a single Word32 (including alpha in last 8 bits),
-- and then convert to different forms.
type Color = Word32
type Color3 = (Double, Double, Double)
type Color4 = (Double, Double, Double, Double)
hex :: Color -> String
hex = printf "#%06x"
d8 :: Color -> Double
dtrip :: Color -> Color3
dquad :: Double -> Color -> Color4
d8 x = fromIntegral (x .&. 0xff) / 255
dtrip c = (d8 r, d8 g, d8 c)
where r = c `shiftR` 16
g = c `shiftR` 8
dquad a c = (r, g, b, a)
where (r, g, b) = dtrip c
-- Color names from <http://chir.ag/projects/name-that-color/>
-- and <http://www.color-blindness.com/color-name-hue/>
brandy_punch = 0xCD8429 -- yellow
christi = 0x67A712 -- green
forest = 0x228B22 -- green
governor = 0x2F3CB3 -- blue
green = 0x00FF00 -- green
lima = 0x76BD17 -- green
magenta = 0xFF00FF -- violet
mango = 0xE77200 -- orange
martinique = 0x363050 -- violet
milano_red = 0xB81104 -- red
red = 0xFF0000 -- red
silver = 0xC0C0C0 -- grey
hue_deep_sky = 200 -- blue
hue_electric_indigo = 272 -- violet
hue_green = 120 -- green
hue_harlequin = 106 -- green
hue_magenta = 300 -- violet
hue_torch_red = -12 -- red, (==348)
hue_yellow = 60 -- yellow
-- Markup helpers
surround :: String -> String -> String -> String
surround b e s = b ++ s ++ e
surround1 :: String -> String -> String
surround1 be s = be ++ s ++ be
spaces :: String -> String
spaces = surround1 " "
bold :: String -> String
bold = surround "<b>" "</b>"
fgcolor :: Color -> String -> String
fgcolor c = surround ("<span fgcolor='" ++ hex c ++ "'>") "</span>"
rootColor :: IORef Int -> IO String
rootColor hueRef = do
hue <- readIORef hueRef
let f :: Double -> Int
f d = round (255 * d)
hex r g b = printf "#%02x%02x%02x" (f r) (f g) (f b)
color = uncurryRGB hex $ hsv (fromIntegral hue) 0.4 0.3
rawSystem "xsetroot" ["-bitmap", "/usr/include/X11/bitmaps/cross_weave",
"-bg", color]
writeIORef hueRef $ (hue + 2) `mod` 360
return "" --color
netStats :: IORef [Integer] -> IO [Double]
netStats ref = do
prev <- readIORef ref
curr <- concat `fmap` mapM getNetInfo ifaces
let diffs = zipWith (-) curr prev
writeIORef ref curr
return $ map fromIntegral diffs
netGraph :: IORef [Integer] -> IO Gtk.Widget
netGraph = Gr.pollingGraphNew cfg freqFast . netStats
where cfg = myGraph True "" [
(governor, 1), (forest, 0.7),
(milano_red, 1), (martinique, 0.7)
]