output.lua 4.94 KB
Newer Older
epsi sayidina's avatar
epsi sayidina committed
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

epsi sayidina's avatar
epsi sayidina committed
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 19 20
_M.segment_windowtitle = '' -- empty string
_M.tags_status         = {} -- empty table
_M.segment_datetime    = '' -- empty string
epsi sayidina's avatar
epsi sayidina committed
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

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

_M.separator = '^bg()^fg(' .. gmc.color['black'] .. ')|^bg()^fg()'

-- http://fontawesome.io/
_M.font_awesome = '^fn(FontAwesome-9)'

-- Powerline Symbol
_M.right_hard_arrow = '^fn(powerlinesymbols-14)^fn()'
_M.right_soft_arrow = '^fn(powerlinesymbols-14)^fn()'
_M.left_hard_arrow  = '^fn(powerlinesymbols-14)^fn()'
_M.left_soft_arrow  = '^fn(powerlinesymbols-14)^fn()'

-- theme
_M.pre_icon    = '^fg(' .. gmc.color['yellow500'] .. ')' 
              .. _M.font_awesome
_M.post_icon   = '^fn()^fg()'

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

function _M.get_statusbar_text(monitor)
    local text = ''
    
epsi sayidina's avatar
epsi sayidina committed
47 48
    -- draw tags, non zero based
    for index = 1, #(_M.tags_status) do
epsi sayidina's avatar
epsi sayidina committed
49 50
        text = text .. _M.output_by_tag(monitor, _M.tags_status[index])
    end
epsi sayidina's avatar
epsi sayidina committed
51 52 53 54

    -- draw date and time
    text = text .. _M.output_by_datetime()

epsi sayidina's avatar
epsi sayidina committed
55
    -- draw window title    
56
    text = text .. _M.output_by_title()
epsi sayidina's avatar
epsi sayidina committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70
  
    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
epsi sayidina's avatar
epsi sayidina committed
71 72

    local text_pre = ''
epsi sayidina's avatar
epsi sayidina committed
73
    if tag_mark == '#' then
epsi sayidina's avatar
epsi sayidina committed
74 75 76 77 78
        text_pre = '^bg(' .. gmc.color['blue500'] .. ')'
                .. '^fg(' .. gmc.color['black'] .. ')'
                .. _M.right_hard_arrow
                .. '^bg(' .. gmc.color['blue500'] .. ')'
                .. '^fg(' .. gmc.color['white'] .. ')'
epsi sayidina's avatar
epsi sayidina committed
79
    elseif tag_mark == '+' then
epsi sayidina's avatar
epsi sayidina committed
80 81
        text_pre = '^bg(' .. gmc.color['yellow500'] .. ')'
                .. '^fg(' .. gmc.color['grey400'] .. ')'
epsi sayidina's avatar
epsi sayidina committed
82
    elseif tag_mark == ':' then
epsi sayidina's avatar
epsi sayidina committed
83 84
        text_pre = '^bg()'
                 .. '^fg(' .. gmc.color['white'] .. ')'
epsi sayidina's avatar
epsi sayidina committed
85
    elseif tag_mark == '!' then
epsi sayidina's avatar
epsi sayidina committed
86 87
        text_pre = '^bg(' .. gmc.color['red500'] .. ')'
                .. '^fg(' .. gmc.color['white'] .. ')'
epsi sayidina's avatar
epsi sayidina committed
88
    else
epsi sayidina's avatar
epsi sayidina committed
89 90
        text_pre = '^bg()'
                .. '^fg(' .. gmc.color['grey600'] .. ')'
epsi sayidina's avatar
epsi sayidina committed
91 92 93 94 95 96
    end

    -- ----- tag by number
    
    -- assuming using dzen2_svn
    -- clickable tags if using SVN dzen
epsi sayidina's avatar
epsi sayidina committed
97 98 99 100
    local text_name = '^ca(1,herbstclient focus_monitor '
                   .. '"' .. monitor .. '" && '
                   .. 'herbstclient use "' .. tag_index .. '")'
                   .. ' ' .. tag_name ..' ^ca()'
epsi sayidina's avatar
epsi sayidina committed
101 102 103

    -- ----- post tag

epsi sayidina's avatar
epsi sayidina committed
104
    local text_post = ""
epsi sayidina's avatar
epsi sayidina committed
105
    if (tag_mark == '#') then
epsi sayidina's avatar
epsi sayidina committed
106 107 108
        text_post = '^bg(' .. gmc.color['black'] .. ')'
                       .. '^fg(' .. gmc.color['blue500'] .. ')'
                       .. _M.right_hard_arrow
epsi sayidina's avatar
epsi sayidina committed
109
    end
epsi sayidina's avatar
epsi sayidina committed
110

epsi sayidina's avatar
epsi sayidina committed
111
     
epsi sayidina's avatar
epsi sayidina committed
112
    return text_pre .. text_name .. text_post
epsi sayidina's avatar
epsi sayidina committed
113 114
end

115
function _M.output_by_title()
epsi sayidina's avatar
epsi sayidina committed
116 117 118 119 120 121
    local text = ' ^r(5x0) ' .. _M.separator .. ' ^r(5x0) '
               .. _M.segment_windowtitle

    return text
end

epsi sayidina's avatar
epsi sayidina committed
122 123 124 125 126 127 128
function _M.output_by_datetime()
    local text = ' ^r(5x0) ' .. _M.separator .. ' ^r(5x0) '
               .. _M.segment_datetime

    return text
end

epsi sayidina's avatar
epsi sayidina committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
-- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
-- 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
      
    _M.segment_windowtitle = ' ' .. icon ..
        ' ^bg()^fg(' .. gmc.color['grey700'] .. ') ' .. windowtitle
end

epsi sayidina's avatar
epsi sayidina committed
150 151 152 153 154 155 156 157 158 159 160 161 162
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 .. ' ^bg()'
        .. '^fg(' .. 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 .. ' ^bg()'
        .. '^fg(' .. gmc.color['blue500'] .. ') ' .. time_str

    _M.segment_datetime = date_text .. '  ' .. time_text
end
epsi sayidina's avatar
epsi sayidina committed
163 164 165 166 167

-- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
-- return

return _M