-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatusbar.lua
453 lines (439 loc) · 10.9 KB
/
statusbar.lua
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
local wezterm = require('wezterm')
local utils = require('utils')
local number_day = 3
local SOLID_LEFT_ARROW = utf8.char(0xe0b2)
local TABLE_ICON = utf8.char(0xf0ce)
local LEADER_COLOR = 'gold'
local TABLE_COLOR = 'lime'
local COMPOSITION_COLOR = 'cyan'
---@param weeknum number
---@diagnostic disable-next-line: unused-local, unused-function
local function day_of_week_in_japan(weeknum)
local days = { '日', '月', '火', '水', '木', '金', '土' }
return days[weeknum + 1]
end
---@param weeknum number
local function day_of_week_in_english(weeknum)
local days = { 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' }
return days[weeknum + 1]
end
-- https://open-meteo.com/en/docs
-- local function weather_symbol(weathercode)
-- local weathers = {
-- [-1] = '❓', -- undefined
-- [0] = '☀️',
-- [1] = '🌤',
-- [2] = '🌦',
-- [3] = '☁️',
-- [45] = '🌫',
-- [48] = '🌫',
-- [51] = '🌦',
-- [53] = '🌦',
-- [55] = '🌧',
-- [56] = '🌨',
-- [57] = '🌨',
-- [61] = '☂',
-- [63] = '☔',
-- [65] = '☔',
-- [66] = '☂',
-- [67] = '☔',
-- [71] = '⛄',
-- [73] = '☃',
-- [75] = '☃',
-- [77] = '🌨',
-- [80] = '🌧',
-- [81] = '☂',
-- [82] = '☔',
-- [85] = '🌨',
-- [86] = '🌨',
-- [95] = '⛈',
-- [96] = '⛈',
-- [99] = '⛈',
-- }
-- return weathers[weathercode]
-- end
---@param weathercode string|number
local function weather_symbol(weathercode)
--[[
plus ➕ ⚠️
green 🟩
blue 🟦
yellow 🟨
orange 🟧
red 🟥
]]
local weathers = {
[-1] = '❓', -- undefined
---
['晴'] = '☀️',
['少云'] = '🌤️',
['晴间多云'] = '🌤️',
['多云'] = '☁️',
['阴'] = '☁️',
--
['有风'] = '🎐',
['平静'] = '🎐',
['微风'] = '🎐',
['和风'] = '🎐',
['清风'] = '🎐',
['强风/劲风'] = '💨',
['疾风'] = '💨',
['大风'] = '💨',
['烈风'] = '💨',
['风暴'] = '💨',
['狂爆风'] = '💨',
['飓风'] = '🌀',
['热带风暴'] = '🌀',
--
['霾'] = '🌫',
['中度霾'] = '🌫',
['重度霾'] = '🌫',
['严重霾'] = '🌫',
--
['阵雨'] = '🌩️',
['雷阵雨'] = '🌩️',
['雷阵雨并伴有冰雹'] = '⛈️',
['小雨'] = '🌦️',
['中雨'] = '🌧️',
['大雨'] = '🌧️',
['暴雨'] = '☔',
['大暴雨'] = '☔',
['特大暴雨'] = '☔',
['强阵雨'] = '☔',
['强雷阵雨'] = '⛈️',
['极端降雨'] = '☔',
['毛毛雨/细雨'] = '🌦️',
['雨'] = '🌧️',
['小雨-中雨'] = '🌦️',
['中雨-大雨'] = '🌧️',
['大雨-暴雨'] = '☔',
['暴雨-大暴雨'] = '☔',
['大暴雨-特大暴雨'] = '☔',
['雨雪天气'] = '🌨️',
['雨夹雪'] = '🌨️',
['阵雨夹雪'] = '🌨️',
['冻雨'] = '🌨️',
--
['雪'] = '☃️',
['阵雪'] = '☃️',
['小雪'] = '☃️',
['中雪'] = '☃️',
['大雪'] = '❄️',
['暴雪'] = '❄️',
['小雪-中雪'] = '☃️',
['中雪-大雪'] = '❄️',
['大雪-暴雪'] = '❄️',
--
['浮尘'] = '💨',
['扬沙'] = '💨',
['沙尘暴'] = '🌪️',
['强沙尘暴'] = '🌪️',
['龙卷风'] = '🌪️',
--
['雾'] = '🌫️',
['浓雾'] = '🌫️',
['强浓雾'] = '🌫️',
['轻雾'] = '🌫️',
['大雾'] = '🌫️',
['特强浓雾'] = '🌫️',
--
['热'] = '🥵',
['冷'] = '🥶',
['未知'] = '❓',
}
return weathers[weathercode]
end
local function styled_whether(weathercode, temperature_max, temperature_min)
return {
{
Foreground = {
Color = '#c0c0c0',
},
},
{
Text = string.format(' %s', weather_symbol(weathercode)),
},
{
Foreground = {
Color = '#f08300', -- hot color
},
},
{
Text = string.format('%s', temperature_max),
},
{
Foreground = {
Color = '#c0c0c0',
},
},
{
Text = '|',
},
{
Foreground = {
Color = '#89c3eb', -- cool color
},
},
{
Text = string.format('%s ', temperature_min),
},
}
end
local function powerline(styled_text, color) -- decorate right prompt
return {
{
Foreground = {
Color = color,
},
},
{
Text = SOLID_LEFT_ARROW,
},
{
Background = {
Color = color,
},
},
table.unpack(styled_text),
}
end
local function update_whether()
---@diagnostic disable-next-line: unused-local
local success, stdout, stderr = wezterm.run_child_process({
'curl',
'--silent',
--'https://api.open-meteo.com/v1/forecast?latitude=35.7&longitude=139.82&daily=weathercode,temperature_2m_max,temperature_2m_min&forecast_days=3&timezone=Asia%2FTokyo',
-- 'https://api.open-meteo.com/v1/forecast?latitude=31.365&longitude=120.6357&daily=weathercode,temperature_2m_max,temperature_2m_min&forecast_days=3&timezone=Asia%2FSingapore',
'http://restapi.amap.com/v3/weather/weatherInfo?key=a1331d8bbee7aa4d1b22fc506a3f46c4&city=320507&extensions=all',
}) -- At Tokyo SkyTree
if not success or not stdout then
return
end
-- local res = wezterm.json_parse(stdout).daily
local json_ = wezterm.json_parse(stdout)
local res = json_.forecasts[1].casts
wezterm.GLOBAL.daily_weather = res
end
---@param current_dir string
---@return table|nil
local function styled_current_git_branch(current_dir)
---@diagnostic disable-next-line: unused-local
local success, stdout, _stderr = wezterm.run_child_process({
'git',
'--git-dir',
string.format('%s/.git', current_dir),
'branch',
'--show-current',
})
if success then
return {
{
Foreground = {
Color = '#c0c0c0',
},
},
{
Text = string.format(' %s ', string.gsub(stdout, '^(.+)\n$', '%1')),
},
}
else
return nil
end
-- return {
-- {
-- Foreground = {
-- Color = '#c0c0c0',
-- },
-- },
-- {
-- Text = success
-- and string.format(' %s ', string.gsub(stdout, '^(.+)\n$', '%1'))
-- or ' ❓ ',
-- },
-- }
end
---@diagnostic disable-next-line: unused-local
local function create_powerlines(window, pane)
local current_dir
if wezterm.target_triple == 'x86_64-pc-windows-msvc' then
-- Windows: C:/Users/username/
current_dir = (pane:get_current_working_dir() or ''):sub(9)
else -- other: /home/username/
current_dir = (pane:get_current_working_dir() or ''):sub(8)
end
local daily_weather = wezterm.GLOBAL.daily_weather
-- local weather_infos = zip(
-- daily_weather.weathercode,
-- daily_weather.temperature_2m_max,
-- daily_weather.temperature_2m_min
-- )
-- local weather_infos = {
-- {daily_weather[1].dayweather,daily_weather[1].daytemp_float, daily_weather[1].nighttemp_float},
-- {daily_weather[2].dayweather,daily_weather[2].daytemp_float, daily_weather[2].nighttemp_float},
-- {daily_weather[3].dayweather,daily_weather[3].daytemp_float, daily_weather[3].nighttemp_float},
-- }
local weather_infos = {}
if daily_weather ~= nil then
for i = 1, number_day do
if daily_weather[i] == nil then
break
end
table.insert(weather_infos, {
daily_weather[i].dayweather,
daily_weather[i].daytemp_float,
daily_weather[i].nighttemp_float,
})
end
end
---@diagnostic disable-next-line: unused-local
local styled_whethers = utils.enumerate(
weather_infos,
function(weather_info, index)
return styled_whether(table.unpack(weather_info))
end
)
local styled_texts = {}
-- leader, table, composition
if window:leader_is_active() then
table.insert(styled_texts, {
{
Foreground = {
Color = LEADER_COLOR,
},
},
{
Text = ' LEADER ',
},
})
table.insert(styled_texts, {
{
Foreground = {
Color = LEADER_COLOR,
},
},
{
Text = TABLE_ICON,
},
})
end
local name = window:active_key_table()
if name then
table.insert(styled_texts, {
{
Foreground = {
Color = TABLE_COLOR,
},
},
{
Text = ' ' .. name .. ' ',
},
})
table.insert(styled_texts, {
{
Foreground = {
Color = TABLE_COLOR,
},
},
{
Text = TABLE_ICON,
},
})
end
local composition = window:composition_status()
if composition then
table.insert(styled_texts, {
{
Foreground = {
Color = COMPOSITION_COLOR,
},
},
{
Text = ' ' .. composition .. ' ',
},
})
table.insert(styled_texts, {
{
Foreground = {
Color = COMPOSITION_COLOR,
},
},
{
Text = TABLE_ICON,
},
})
end
-- weather
for i = 1, number_day do
if styled_whethers[i] == nil then
break
end
table.insert(styled_texts, styled_whethers[i])
end
local git_branch = styled_current_git_branch(current_dir)
if git_branch ~= nil then
table.insert(styled_texts, git_branch)
end
-- table.insert(styled_texts, git_branch)
table.insert(styled_texts, {
{
Foreground = {
Color = '#c0c0c0',
},
},
{
Text = string.format(' %s ', utils.short_path(current_dir)),
-- Text = string.format(' %s ', current_dir),
},
})
local workspace = window:active_workspace()
local workspace_color = utils.generate_window_color(workspace)
print('workspace_color: ' .. workspace_color)
table.insert(styled_texts, {
{
Foreground = {
Color = workspace_color,
},
},
{
Text = ' ' .. workspace .. ' ',
},
})
table.insert(styled_texts, {
{
Foreground = {
Color = '#c0c0c0',
},
},
{
Text = string.format(
' %s(%s) %s ',
wezterm.strftime('%-m/%-d'),
day_of_week_in_english(wezterm.strftime('%u')),
wezterm.strftime('%H:%M:%S')
),
},
})
return utils.flatten1(
utils.enumerate(styled_texts, function(styled_text, index)
local color =
string.format('hsl(%sdeg 75%% 25%%)', index % 2 == 0 and 240 or 224)
return powerline(styled_text, color)
end)
)
end
local FOUR_HOURS = 3600 * 4
local THIRTY_SECONDS = 30
wezterm.on('update-status', function(window, pane)
local counter = wezterm.GLOBAL.weather_loop_counter or 0
if counter % FOUR_HOURS == 0 then -- every 4 hours
update_whether()
end
if
wezterm.GLOBAL.daily_weather == nil
and counter < (FOUR_HOURS - THIRTY_SECONDS)
then
counter = FOUR_HOURS - THIRTY_SECONDS
end
wezterm.GLOBAL.weather_loop_counter = counter + 1
window:set_right_status(wezterm.format(create_powerlines(window, pane)))
end)