Wicked/zh-hans
Wicked是一个Lua库,你可以在rc.lua通过require加载它。通过使用wicked,你可以在同一个配置文件中设置好你的widget而不需要运行额外的程序。
Contents |
获取wicked
Arch Linux
如果你正在使用Arch Linux,aur里面已经有了一个可以使用的包了,你可以在这里下载到: http://aur.archlinux.org/packages.php?ID=17232. Arch源:
[awesome] #awesome3 Server = http://www.camazotz.de/awesome/
[deelab] #awesome-git Server = http://www.deelab.org/arch/i686
Source Mage GNU/Linux
你所需要做的仅仅是"cast wicked"
Gentoo Linux
你可以在这里下载到gentoo包wicked-9999.ebuild.
Manually
如果你用的不是Arch或者你更喜欢单独下载这个库,你可以用git在这里下载到它:http://git.glacicle.com/?p=awesome/wicked.git;a=summary. 然后手动安装它:
git clone git://git.glacicle.com/awesome/wicked.git sudo cp wicked/wicked.lua /usr/share/awesome/lib/ sudo cp wicked/wicked.7.gz /usr/share/man/man7/
载入wicked
要载入wicked很容易:
require("wicked")
创建widget
创建一个widget和在rc.lua进行函数调用一样容易,下面是你可以使用的widget的例子。如果你想知道还有哪些更多的widget类型和选线,可以在安装wicket以后运行命令`man wicked`。不要忘记在创建widget以后将它添加到statusbar里面去。
要学习怎么创建widget,可以看看在作者的配置文件中的例子current config。
例如你可以通过如下的代码创建显示内存使用情况的widget:
memwidget = widget({
type = 'textbox',
name = 'memwidget'
})
wicked.register(memwidget, wicked.widgets.mem,
' <span color="white">Memory:</span> $1 ($2Mb/$3Mb)')
然后你需要把你要的widget加入到一个已有的wibox或者新创建的wibox中去。比如说我创建了一个在屏幕下沿的状态栏:
-- 设置状态栏的位置、颜色
mystatebar = wibox( {position = "bottom", fg = beautiful.fg_normal, bg = beautiful.bg_normal} )
-- 把你创建过的widget添加进去,比如说上面所说的memwidget
mystatebar.widgets = {
cpugraphwidget,
memwidget,
fswidget,
netwidget
}
-- 设置状态栏所在的显示器,对于单显示器的机器来说设置为1就可以了
mystatebar.screen = 1
以上就是在statuesbar中加入一个widget所需要的所有步骤了。
各种可用的widget
wicked提供好几种widget可以使用,包括CPU占用率、内存使用率、文件系统空间、网络传输速率等。基本上wicked的使用方法就是先调用widget()函数创建一个widget,然后设置好参数调用wicked.register(),最后再创建一个wibox把先前创建的widget放在里面就好了。有的widget不能用,会导致awesome当掉(估计应该是bug)。经过我实验可以用的有:内存占用率、文件系统使用率、网络流量、CPU占用率和CPU图像。 日期
显示现在的时间
datewidget = widget({
type = 'textbox',
name = 'datewidget'
})
wicked.register(datewidget, wicked.widgets.date,
' <span color="white">Date:</span> %c')
MPD 正在播放
显示正在Music Playing Daemon中正在播放的歌曲
mpdwidget = widget({
type = 'textbox',
name = 'mpdwidget'
})
wicked.register(mpdwidget, wicked.widgets.mpd,
' <span color="white">Now Playing:</span> $1')
如果你想要它只是在播放歌曲的时候显示的话,可以把register函数的调用改为如下所示:
wicked.register(mpdwidget, wicked.widgets.mpd,
function (widget, args)
if args[1]:find("volume:") == nil then
return ' <span color="white">Now Playing:</span> '..args[1]
else
return ''
end
end)
内存监视器
在状态栏中显示已用/总共的内存量
memwidget = widget({
type = 'textbox',
name = 'memwidget'
})
wicked.register(memwidget, wicked.widgets.mem,
' <span color="white">Memory:</span> $1 ($2Mb/$3Mb)')
内存占用率进度条
用一个进度条显示内存的相对使用情况
membarwidget = widget({
type = 'progressbar',
name = 'membarwidget',
align = 'right'
})
membarwidget:properties_set('mem', {
width = 40,
height = 0.65,
gap = 0,
border_padding = 1,
border_width = 1,
ticks_count = 0,
ticks_gap = 0,
vertical = false
})
membarwidget:bar_properties_set('mem', {
bg = '#222222',
fg = '#285577',
fg_center = '#285577',
fg_end = '#285577',
fg_off = '#222222',
reverse = false,
min_value = 0,
max_value = 100
})
wicked.register(membarwidget, wicked.widgets.mem, '$1', 1, 'mem')
CPU占用率
显示CPU占用率的百分比
cpuwidget = widget({
type = 'textbox',
name = 'cpuwidget'
})
wicked.register(cpuwidget, wicked.widgets.cpu,
' <span color="white">CPU:</span> $1%')
CPU占用率图像
用一个图像来显示CPU占用率的变化
cpugraphwidget = widget({
type = 'graph',
name = 'cpugraphwidget',
align = 'right'
})
cpugraphwidget.height = 0.85
cpugraphwidget.width = 45
cpugraphwidget.bg = '#333333'
cpugraphwidget.border_color = '#0a0a0a'
cpugraphwidget.grow = 'left'
cpugraphwidget:plot_properties_set('cpu', {
fg = '#AEC6D8',
fg_center = '#285577',
fg_end = '#285577',
vertical_gradient = false
})
wicked.register(cpugraphwidget, wicked.widgets.cpu, '$1', 1, 'cpu')
文件系统
显示已用/总共的文件空间,以及百分比
fswidget = widget({
type = 'textbox',
name = 'fswidget'
})
wicked.register(fswidget, wicked.widgets.fs,
' <span color="white">FS:</span> ${/ used}/${/ size} (${/ usep} used)', 120)
网络监视器
显示eth0的网络流量
netwidget = widget({
type = 'textbox',
name = 'netwidget'
})
wicked.register(netwidget, wicked.widgets.net,
' <span color="white">NET</span>: ${eth0 down} / ${eth0 up} [ ${eth0 rx} // ${eth0 tx} ]',
nil, nil, 3)
电池电量
显示电池的电量
settings.batteries = 2
-- Label
batterywidget = widget({type = 'textbox',
name = 'batterywidget',
align = 'right'})
wicked.register(batterywidget, function() return {} end,
settings.widget_separator ..
beautiful.markup.heading('Bat') ..
settings.widget_spacer,
nil, nil, 500)
table.insert(settings.widgets, {1, batterywidget})
-- Function to extract charge percentage
function read_battery_life(number)
return function(format)
local fh = io.popen('acpi')
output = fh:read("*a")
fh:close()
count = 0
for s in string.gmatch(output, "(%d+)%%") do
if number == count then
return {s}
end
count = count + 1
end
end
end
-- Display one vertical progressbar per battery
for battery=0, settings.batteries-1 do
batterygraphwidget = widget({ type = 'progressbar',
name = 'batterygraphwidget',
align = 'right' })
batterygraphwidget.height = 0.85
batterygraphwidget.width = 8
batterygraphwidget.bg = '#333333'
batterygraphwidget.border_color = '#0a0a0a'
batterygraphwidget.vertical = true
batterygraphwidget:bar_properties_set('battery',
{ fg = '#AEC6D8',
fg_center = '#285577',
fg_end = '#285577',
fg_off = '#222222',
vertical_gradient = true,
horizontal_gradient = false,
ticks_count = 0,
ticks_gap = 0 })
wicked.register(batterygraphwidget, read_battery_life(battery), '$1', 1, 'battery')
table.insert(settings.widgets, {1, batterygraphwidget})
spacewidget = widget({type = 'textbox',
name = 'spacewidget',
align = 'right'})
wicked.register(spacewidget, function() return {} end,
settings.widget_spacer,
nil, nil, 500)
table.insert(settings.widgets, {1, spacewidget})
end
自定义Widgets
You can create your own custom widgets by passing your own functions as the type parameter, using this, you can specify any arbitrary command to be outputted into a widget at any interval. The following example displays the volume from alsa (gotten by running the `amixer` command and parsing the output), every 4 seconds. (Note: requires alsa-utils)
volumewidget = widget({
type = 'textbox',
name = 'volumewidget'
})
function amixer_volume(format)
local f = io.popen('amixer get PCM')
local l = f:lines()
local v = ''
for line in l do
if line:find('Front Left:') ~= nil then
pend = line:find('%]', 0, true)
pstart = line:find('[', 0, true)
v = line:sub(pstart+1, pend)
end
end
f:close()
return {v}
end
wicked.register(volumewidget, amixer_volume, "<span color='white'>Volume</span>: $1", 4)
You can use the following to run any external script you might have written that collects data and outputs it:
mywidget = widget({
type = 'textbox',
name = 'mywidget'
})
function run_script()
local filedescriptor = io.popen('my_nifty_script.py')
local value = filedescriptor:read()
filedescriptor:close()
return {value}
end
-- Runs 'my_nifty_script.py' every 10 seconds and puts its output into the widget
wicked.register(mywidget, run_script, "$1", 10)
Awesome假死
因为awesome在继续运行之前会等待lua脚本借书,因此运行一个缓慢的脚本很可能会让你的整个awesome反应迟钝。你可以用一个后台线程或者是临时文件来避免这个问题。通过下面的方法来运行一个缓慢的脚本可以避免阻塞awesome:
mywidget = widget({
type = 'textbox',
name = 'mywidget'
})
function run_slow_script (widget, args)
-- Read the temporary file left by the script
local filedescriptor = io.open('/tmp/script-temp-file')
local value = nil
if filedescriptor ~= nil then
value = filedescriptor:read()
end
filedescriptor:close()
if value == nil then
return {''}
else
return {value}
end
end
-- Runs 'my_slow_script.py' every 10 seconds and puts its output into the widget
wicked.register(mywidget, run_slow_script, "$1", 10)
-- Register a timer to run every 9 seconds
awful.hooks.timer.register(9, mywidget_timer)
-- Use the timer to fill the temporary file
function mywidget_timer ()
os.execute('my_slow_script.py > /tmp/script-temp-file &')
end
临时挂起Wicked
如果你很在意电池的使用时间,那么你可能更希望wicked在你的电脑处于电池供电状态的时候不要一直运行命令。你可以通过绑定命令"wicked.suspend()"来临时停止所有wicked的更新。你可以使用"wicked.activate()"来重新激活被挂起的wicked。
你可以在hal脚本之类的地方使用awesome-cliend来自动调用这些命令。你可以像这么调用:
#!/bin/bash echo "wicked.suspend()" | awesome-client
把suspend在hal脚本的适当的地方换成activate就可以自动唤醒挂起的wicked了。