タイトルバーにつけているタグを表示してみる (awesome)

タイル型WMのawesomeを使っていて、複数のタグを同時に表示していると各クライアントにどのタグをつけているのかわからなくなってしまうことがあったので、各クライントのタイトルバーにつけているタグを表示するようにしてみた。

まず、awful.titlebar.addでタイトルバーをつけるときに、タグ名を書いたウィジェットを追加する。

titlebar_taglist = widget({ type = "textbox"})
titlebar_taglist.text = "tag:"
for i, v in ipairs(c:tags()) do
    titlebar_taglist.text = titlebar_taglist.text .. " " .. v.name
end
awful.titlebar.add(c, { modkey = modkey, widget=titlebar_taglist })

あとは、クライアントのタグを付け替えたときに、表示が変わるように設定する。
タグリストからマウスでタグを付け替えることはないので、そちらには対応しなかった。

for i = 1, keynumber do
        awful.key({ modkey, "Shift" }, "#" .. i + 9,
                  function ()
                      if client.focus and tags[client.focus.screen][i] then
                          client.focus.titlebar.widgets[2].text = "tag: " .. tags[client.focus.screen][i].name
                          awful.client.movetotag(tags[client.focus.screen][i])
                      end
                  end),
        awful.key({ modkey, "Control", "Shift" }, "#" .. i + 9,
                  function ()
                      if client.focus and tags[client.focus.screen][i] then
                          local old_tags = {}
                          local old_tags_count = 0
                          for _, v in ipairs(client.focus:tags()) do
                              old_tags[v] = true  
                              old_tags_count = old_tags_count + 1
                          end

                          if old_tags[tags[client.focus.screen][i]] then
                              if old_tags_count > 1 then
                                  old_tags[tags[client.focus.screen][i]] = nil
                              end
                          else
                              old_tags[tags[client.focus.screen][i]] = true
                          end
                              
                          local client_tags = {}
                          local index = 1
                          for i, v in ipairs(tags[client.focus.screen]) do
                              if old_tags[v] then
                                  client_tags[index] = v
                                  index = index + 1
                              end
                          end

                          local c = client.focus
                          c.titlebar.widgets[2].text = "tag:"
                          for i, v in ipairs(client_tags) do
                              c.titlebar.widgets[2].text = c.titlebar.widgets[2].text .. " " .. v.name
                          end

                          awful.client.toggletag(tags[client.focus.screen][i])
                      end
                  end))
end

Luaの配列の扱いにけっこう苦戦したけど、慣れたらいろいろ設定できて面白そう。