output.lua 4.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
local gmc = require('.gmc')
local common = require('.common')
local helper = require('.helper')

local _M = {}

-- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
-- initialize

epsi sayidina's avatar
epsi sayidina committed
10
11
12
-- assuming $ herbstclient tag_status
-- 	#1	:2	:3	:4	.5	.6	.7	.8	.9

13
14
15
16
17
-- custom tag names
_M.tag_shows = {'一 ichi', '二 ni', '三 san', '四 shi', 
  '五 go', '六 roku', '七 shichi', '八 hachi', '九 kyū', '十 jū'}

-- initialize variable segment
epsi sayidina's avatar
epsi sayidina committed
18
_M.segment_windowtitle = '' -- empty string
epsi sayidina's avatar
epsi sayidina committed
19
20
_M.tags_status         = {} -- empty table
_M.segment_datetime    = '' -- empty string
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

-- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
-- decoration

_M.separator = '%{B-}%{F' .. gmc.color['yellow500'] .. '}|%{B-}%{F-}'

-- Powerline Symbol
_M.right_hard_arrow = ""
_M.right_soft_arrow = ""
_M.left_hard_arrow  = ""
_M.left_soft_arrow  = ""

-- theme
_M.pre_icon    = '%{F' .. gmc.color['yellow500'] .. '}'
_M.post_icon   = '%{F-}'

-- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
-- main

function _M.get_statusbar_text(monitor)
    local text = ''
    
epsi sayidina's avatar
epsi sayidina committed
43
    -- draw tags, non zero based
44
    text = text .. '%{l}'
epsi sayidina's avatar
epsi sayidina committed
45
    for index = 1, #(_M.tags_status) do
46
47
        text = text .. _M.output_by_tag(monitor, _M.tags_status[index])
    end
epsi sayidina's avatar
epsi sayidina committed
48
49
50
51
52

    -- draw date and time
    text = text .. '%{c}'
    text = text .. _M.output_by_datetime()

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
    -- draw window title 
    text = text .. '%{r}'
    text = text .. _M.output_by_title()
  
    return text
end

-- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
-- each segments

function _M.output_by_tag(monitor, tag_status)
    local tag_index  = string.sub(tag_status, 2, 2)
    local tag_mark   = string.sub(tag_status, 1, 1)
    local index      = tonumber(tag_index)-- not a zero based array
    local tag_name   = _M.tag_shows[index]

    -- ----- pre tag

    local text_pre = ''
    if tag_mark == '#' then
        text_pre = '%{B' .. gmc.color['blue500'] .. '}'
                .. '%{F' .. gmc.color['black'] .. '}'
                .. '%{U' .. gmc.color['white'] .. '}%{+u}' 
                .. _M.right_hard_arrow
                .. '%{B' .. gmc.color['blue500'] .. '}'
                .. '%{F' .. gmc.color['white'] .. '}'
                .. '%{U' .. gmc.color['white'] .. '}%{+u}'
    elseif tag_mark == '+' then
        text_pre = '%{B' .. gmc.color['yellow500'] .. '}'
                .. '%{F' .. gmc.color['grey400'] .. '}'
    elseif tag_mark == ':' then
        text_pre = '%{B-}%{F' .. gmc.color['white'] .. '}'
                .. '%{U' .. gmc.color['red500'] .. '}%{+u}'
    elseif tag_mark == '!' then
        text_pre = '%{B' .. gmc.color['red500'] .. '}'
                .. '%{F' .. gmc.color['white'] .. '}'
                .. '%{U' .. gmc.color['white'] .. '}%{+u}'
    else
        text_pre = '%{B-}%{F' .. gmc.color['grey600'] .. '}%{-u}'
    end

    -- ----- tag by number
95
96
97
98
99
100

    -- clickable tags
    local text_name = '%{A:herbstclient focus_monitor '
                   .. '"' .. monitor .. '" && '
                   .. 'herbstclient use "' .. tag_index .. '":}'
                   .. ' ' .. tag_name ..' %{A} '
101
102
    
    -- non clickable tags
103
    -- local text_name = ' ' .. tag_name .. ' '
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

    -- ----- post tag

    local text_post = ""
    if (tag_mark == '#') then
        text_post = '%{B-}' 
                 .. '%{F' .. gmc.color['blue500'] .. '}' 
                 .. '%{U' .. gmc.color['red500'] .. '}%{+u}' 
                 .. _M.right_hard_arrow
    end

    text_clear = '%{B-}%{F-}%{-u}'

     
    return text_pre .. text_name .. text_post .. text_clear
end

function _M.output_by_title()
    local text = _M.segment_windowtitle .. ' ' .. _M.separator .. '  '

    return text
end

epsi sayidina's avatar
epsi sayidina committed
127
128
129
130
function _M.output_by_datetime()
    return _M.segment_datetime
end

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
-- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
-- setting variables, response to event handler

function _M.set_tag_value(monitor)
    local command = 'herbstclient tag_status ' .. monitor
    local handle = io.popen(command)
    local result = handle:read("*a")
    handle:close() 
        
    local raw = common.trim1(result)  
    _M.tags_status = common.split(raw, "\t")
end

function _M.set_windowtitle(windowtitle)
    local icon = _M.pre_icon .. '' .. _M.post_icon
    if (windowtitle == nil) then windowtitle = '' end
epsi sayidina's avatar
epsi sayidina committed
147

148
149
150
151
    _M.segment_windowtitle = ' ' .. icon ..
        ' %{B-}%{F' .. gmc.color['grey700'] .. '} ' .. windowtitle
end

epsi sayidina's avatar
epsi sayidina committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
function _M.set_datetime()
    local date_icon = _M.pre_icon .. '' .. _M.post_icon
    local date_str  = os.date('%a %b %d')     
    local date_text = date_icon .. ' %{B-}'
        .. '%{F' .. gmc.color['grey700'] .. '} ' .. date_str

    local time_icon = _M.pre_icon .. '' .. _M.post_icon
    local time_str  = os.date('%H:%M:%S')
    local time_text = time_icon .. ' %{B-}'
        .. '%{F' .. gmc.color['blue500'] .. '} ' .. time_str

    _M.segment_datetime = date_text .. '  ' .. time_text
end

166
167
168
169
-- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
-- return

return _M