xmobar

来自 ArchWiki

xmobar 是一个轻量级的、基于文本的状态栏,使用 Haskell 编写。它最初设计用于与 xmonad 一起使用,但它也适用于任何其他 窗口管理器。虽然 xmobar 是用 Haskell 编写的,但安装和使用它不需要任何 Haskell 语言知识。

安装

安装 xmobar 软件包。可用的变体有

运行

如果配置文件保存为 ~/.xmobarrc

$ xmobar &

或者,可以指定配置文件的路径

$ xmobar /path/to/config &

以下是如何使用命令行选项配置 xmobar 的示例

$ xmobar -B white -a right -F blue -t '%LIPB%' -c '[Run Weather "LIPB" [] 36000]' &

这将运行 xmobar 右对齐,白色背景和蓝色文本,使用 Weather 插件。请注意,输出模板必须至少包含一个命令。阅读以下部分以进一步解释选项。

标志 -x number 可以指定运行 xmobar 的 x 显示器,它将有效地覆盖配置选项 pickBroadestallDesktops

配置

xmobar 的配置通常在 ~/.xmobarrc 中定义,或者在启动 xmobar 时通过指定一组命令行选项来定义。任何给定的命令行选项都将覆盖配置文件中的相应选项。这对于测试新配置而无需编辑配置文件非常有用。

以下是 ~/.xmobarrc 文件的示例,后跟每个选项的描述。请注意,每个选项都有一个对应的命令行选项。

注意:配置文件是(Haskell 源代码的子集),因此 '--' 启动单行注释。

Config { 

   -- appearance
     font =         "Bitstream Vera Sans Mono Bold 9"
   , bgColor =      "black"
   , fgColor =      "#646464"
   , position =     Top
   , border =       BottomB
   , borderColor =  "#646464"

   -- layout
   , sepChar =  "%"   -- delineator between plugin names and straight text
   , alignSep = "}{"  -- separator between left-right alignment
   , template = "%battery% | %multicpu% | %coretemp% | %memory% | %dynnetwork% }{ %RJTT% | %date% || %kbd% "

   -- general behavior
   , lowerOnStart =     True    -- send to bottom of window stack on start
   , hideOnStart =      False   -- start with window unmapped (hidden)
   , allDesktops =      True    -- show on all desktops
   , overrideRedirect = True    -- set the Override Redirect flag (Xlib)
   , pickBroadest =     False   -- choose widest display (multi-monitor)
   , persistent =       True    -- enable/disable hiding (True = disabled)

   -- plugins
   --   Numbers can be automatically colored according to their value. xmobar
   --   decides color based on a three-tier/two-cutoff system, controlled by
   --   command options:
   --     --Low sets the low cutoff
   --     --High sets the high cutoff
   --
   --     --low sets the color below --Low cutoff
   --     --normal sets the color between --Low and --High cutoffs
   --     --High sets the color above --High cutoff
   --
   --   The --template option controls how the plugin is displayed. Text
   --   color can be set by enclosing in <fc></fc> tags. For more details
   --   see http://projects.haskell.org/xmobar/#system-monitor-plugins.
   , commands = 

        -- weather monitor
        [ Run Weather "RJTT" [ "--template", "<skyCondition> | <fc=#4682B4><tempC></fc>°C | <fc=#4682B4><rh></fc>% | <fc=#4682B4><pressure></fc>hPa"
                             ] 36000

        -- network activity monitor (dynamic interface resolution)
        , Run DynNetwork     [ "--template" , "<dev>: <tx>kB/s|<rx>kB/s"
                             , "--Low"      , "1000"       -- units: B/s
                             , "--High"     , "5000"       -- units: B/s
                             , "--low"      , "darkgreen"
                             , "--normal"   , "darkorange"
                             , "--high"     , "darkred"
                             ] 10

        -- cpu activity monitor
        , Run MultiCpu       [ "--template" , "Cpu: <total0>%|<total1>%"
                             , "--Low"      , "50"         -- units: %
                             , "--High"     , "85"         -- units: %
                             , "--low"      , "darkgreen"
                             , "--normal"   , "darkorange"
                             , "--high"     , "darkred"
                             ] 10

        -- cpu core temperature monitor
        , Run CoreTemp       [ "--template" , "Temp: <core0>°C|<core1>°C"
                             , "--Low"      , "70"        -- units: °C
                             , "--High"     , "80"        -- units: °C
                             , "--low"      , "darkgreen"
                             , "--normal"   , "darkorange"
                             , "--high"     , "darkred"
                             ] 50
                          
        -- memory usage monitor
        , Run Memory         [ "--template" ,"Mem: <usedratio>%"
                             , "--Low"      , "20"        -- units: %
                             , "--High"     , "90"        -- units: %
                             , "--low"      , "darkgreen"
                             , "--normal"   , "darkorange"
                             , "--high"     , "darkred"
                             ] 10

        -- battery monitor
        , Run Battery        [ "--template" , "Batt: <acstatus>"
                             , "--Low"      , "10"        -- units: %
                             , "--High"     , "80"        -- units: %
                             , "--low"      , "darkred"
                             , "--normal"   , "darkorange"
                             , "--high"     , "darkgreen"

                             , "--" -- battery specific options
                                       -- discharging status
                                       , "-o"	, "<left>% (<timeleft>)"
                                       -- AC "on" status
                                       , "-O"	, "<fc=#dAA520>Charging</fc>"
                                       -- charged status
                                       , "-i"	, "<fc=#006000>Charged</fc>"
                             ] 50

        -- time and date indicator 
        --   (%F = y-m-d date, %a = day of week, %T = h:m:s time)
        , Run Date           "<fc=#ABABAB>%F (%a) %T</fc>" "date" 10

        -- keyboard layout indicator
        , Run Kbd            [ ("us(dvorak)" , "<fc=#00008B>DV</fc>")
                             , ("us"         , "<fc=#8B0000>US</fc>")
                             ]
        ]
   }
  • font - 要使用的字体名称。"。

示例

font = "Bitstream Vera Sans Mono 8"
  • additionalFonts - 要与 <fn=n> 标签一起使用的字体列表。

示例

additionalFonts = [ "Bitstream Vera Sans Mono 8", "Source Code Pro 10"]
  • fgColor - 字体颜色,接受颜色名称(如 black)和十六进制颜色(如 #000000)。
  • bgColor - 条的颜色,接受颜色名称(如 red)和十六进制颜色(如 #ff0000)。
  • position - 条的位置。关键字为:Top/BottomTopW/BottomWStaticOnScreen
    • Top/Bottom - 屏幕的顶部/底部。
    • TopW/BottomW - 屏幕的顶部/底部,宽度固定。TopW/BottomW 接受 2 个参数
      • 对齐方式:Left,Center 或 Right 对齐。
      • 宽度:条的宽度百分比整数。
    • Static - 屏幕上的固定位置,宽度固定。Static 接受 4 个参数
      • xpos:水平位置,以像素为单位,从左上角开始。
      • ypos:垂直位置,以像素为单位,从左上角开始。
      • width:条的宽度,以像素为单位。
      • height:条的高度,以像素为单位。
    • OnScreen - 指定条定位在哪个屏幕上。接受 2 个参数
      • screenNr:要在其上定位条的屏幕索引。
      • position:条的定位,这是上述关键字之一,后跟其参数。

示例 - 居中于屏幕底部,宽度为屏幕的 75%

position = BottomW C 75

示例 - 屏幕左上角,宽度为 1024 像素,高度为 15 像素

position = Static { xpos = 0 , ypos = 0, width = 1024, height = 15 }
  • border - 边框的位置和外观。关键字为:TopB, TopBM, BottomB, BottomBM, FullB, FullBMNoBorder (默认)
    • TopB/BottomB - 条的顶部/底部
    • FullB - 条的整个周边
    • TopBM/BottomBM/FullBM - 与其他选项相同,但您可以指定边框应从条的边缘偏移多少像素。每个选项都接受一个整数参数。

示例 - 边框放置在条的底边下方 3 像素处

border: BottomBM 3
  • sepChar - 用于指示输出模板中命令的字符。默认字符为 "%"
  • alignSep - 用于在输出模板中对齐文本的字符串。第一个字符之前的文本将左对齐,它们之间的文本将居中对齐,最后一个字符右侧的文本将右对齐。默认字符串为 "}{"
  • iconRoot - 存储图标的根文件夹。
  • template - 输出模板是一个字符串,其中包含将显示的文本和命令。它包含 %command% 的别名、书写文本和设置文本颜色的颜色标签。此模板字符串中使用的标签也可以在 Run command 自定义模板中使用。模板标签包括
    • %command% 打印命令的输出
    • <fc=#f0f0f0,#101010>string</fc> 将打印 string,其中 #f0f0f0 为前景色,#101010 为背景色(两者都可以省略)
    • <fn=1>string</fn> 将使用 additionalFonts 列表中第 n 个字体(在本例中为第一个字体)打印 string,其中 0 为默认字体。
    • <icon=/path/to/icon.xbm/> 将打印路径中的 XBM 或 XPM 图标(如果路径不以 /、./ 或 ../ 开头,它将使用 iconRoot ++ path/to/icon)。透明 XPM 图标将忽略 fc 标签并使用默认背景色,而 XBM 图标将使用本地(或默认)背景色和前景色。xmobar 将使用图标,无论大小如何,但为了将它们与文本 bgcolorfgcolor 对齐,最佳高度为 20 像素,宽度没有限制。
    • <action=`command` button=1>string</action> 将在用按钮 1(鼠标左键)单击 string 时执行命令。
    • <raw=len:string/> 输出长度为 len 的任意字符串。

示例

template = "%StdinReader%}{%cpu% %memory% <fc=#ffaaff>battery:</fc> %battery% %date%"
  • commands 用于设置要运行的程序的选项。Commands 是一个逗号分隔的命令列表,可以选择使用选项指定。

示例 - 运行 Memory 插件,使用指定的模板和 Swap 插件,使用默认参数。两者都每秒更新一次

commands = [Run Memory ["-t","Mem: <usedratio>%"] 10, Run Swap [] 10]

最后是一些控制条的常规行为的选项---每个选项都设置为单个 TrueFalse

  • lowerOnStart - 控制是否将条保持在所有其他窗口之后。
  • hideOnStart - 控制是否在启动时隐藏条。
  • allDesktops - 控制是否在所有桌面上显示条。
  • overrideRedirect - 一些窗口管理器必需的选项,以防止条被视为普通窗口。
  • pickBroadest - 在多显示器设置中,将条放在最宽的显示器上,而不是第一个。
  • persistent - 控制是否始终可见,而不管条的“隐藏”状态如何。

技巧和诀窍

有各种插件可以与 xmobar 一起使用 - 仅举几个例子,有用于磁盘使用率、内存、CPU、电池状态、天气报告和网络活动的插件。每个插件的详细描述、其依赖项以及如何配置它都在项目网站上。

GMail 集成

假设您已安装 xmobar-gmail-darcsxmobar-gmail,您可以按如下方式配置 .xmobarrc。将 GMail 插件添加到命令列表

, Run GMail "gmail.username" "GmailPassword" ["-t", "Mail: <count>"] 3000

然后将命令添加到模板

, template = "... %gmail.username% ..."

MPD 集成

有一个插件可以提取信息以显示在状态栏上,关于 MPD 当前正在播放的歌曲。要添加一个简单的插件,显示当前曲目的艺术家和歌曲,请将此行添加到 ~/.xmobarrc 中的命令列表

, Run MPD ["-t", "<state>: <artist> - <track>"] 10

最后,您需要将插件放置在模板中的某个位置,如下所示

, template = "%StdinReader% }{ ... %mpd% ..."

Conky-Cli 集成

可以利用 conky-cliAUR 的功能,例如磁盘空间、顶部和系统消息,方法是将来自 conky 的信息管道传输到文本文件并从中读取内容。以下是与 xmobar 一起使用的 bash 脚本。

~/.xmonad/conkyscript
#!/bin/bash
conky -c ~/.conkyclirc -i1 -q > conkystat &
sleep 4
killall -q conky
cat conkystat
rm conkystat

将以下行添加到 ~/.xmobarrc 中的 commands 部分。

, Run Com ".xmonad/conkyscript" ["&"] "conky" 300

这使脚本每 30 秒运行一次。

然后将以下内容添加到 .xinitrc 中,在 exec xmonad 条目之前。

.xmonad/conkyscript &
sleep 6 && xmobar &

然后将 %conky% 添加到您的模板部分。

简单的 conky-cli 集成

只需放置此代码

Run Com "conky" ["-q", "-i", "1"] "conky" 600

在您的 .xmobarrc 中。

故障排除

xmonad 无响应,除非焦点窗口

如果 xmonad 对点击和热键没有响应,但您仍然可以在焦点窗口中键入内容,则很可能是 logHook 管道传输到未被读取的句柄所致。

要解决此问题,请在您的 xmobar 的模板中添加 %StdinReader%%UnsafeStdinReader%,并在命令部分运行相应的命令(即 Run StdinReader)。如果 xmobar 有多个实例,请检查您要管道传输到的所有条是否都具有 StdinReader,如果没有,则添加它或从 logHook 中的 ppOutput 中删除该条。

参见