March 5, 2014

Awesome WM vocabulary widget

換到 awesome wm 之後,上方的 bar 其實有很多空間。因為自己的記性不好,乾脆寫個小 widget,大約 60 秒就 show 一個單字,不然每次五十音我都會一直忘記

抄抄寫寫一堆很醜的 lua code 的結果如下,他會讀取 .vocabulary 這個檔案,每 60 秒亂數取其中一行顯示

檔案更新怎麼辦?重開一下 awesome 就好囉!

vocabulary_widget = widget({ type = "textbox" })

function vocabularyUpdate(lines)
    vocabulary_widget.text = lines[math.random(0, #lines)]
end

function readFile(filename)
    local arr = {}
    local i = 0
    local file=io.open(filename, "r")
    if not file then return "empty" end
    for line in file:lines() do
        arr[i] = line
        i = i + 1
    end
    return arr
end

local file = "/home/walkingice/.vocabulary"
local period = 60

math.randomseed(os.time())
content = readFile(file)

vocabulary_timer = timer({timeout = period})
vocabulary_timer:add_signal("timeout", function()  vocabularyUpdate(content) end)
vocabulary_timer:start()

No comments:

Post a Comment