【问题标题】:Get hostname in xmonad/xmobar config file在 xmonad/xmobar 配置文件中获取主机名
【发布时间】:2014-05-22 13:02:01
【问题描述】:

我的点文件在计算机之间有 99% 的相似性,但我保留了一些小的调整 对于各种次要设置。 我的计划是使用基于主机名的 if 语句进行区分。 在 bashrc 或 zshrc 等 shell 配置中看起来如下所示的内容

if [ $(hostname) == 'host1' ]; then
# things to do differently on host1.
elif [ $(hostname) == 'host2' ]; then
# things to do differently on host2.
fi

我怀疑 xmobar 只是一个配置文件,其中没有真正的 haskell 进行解析。 关于如何获得类似于我在 xmobar 中的外壳的任何想法?

主要是我想修改 xmobar 中的宽度和网络接口,比如

Config {
if hostname == "host1" 
then
    font = "xft:Fixed-9",
    position = Static { xpos = 0, ypos = 0, width = 1280, height = 16 },
else if hostname == "host2"
then
    font = "xft:Fixed-12",
    position = Static { xpos = 1920, ypos = 0, width = 1800, height = 16 },
lowerOnStart = True,
commands = [
    -- if here as well to switch between eth0 and wls3 
    Run Network "wls3" ["-t","Net: <rx>, <tx>","-H","200","-L","10","-h","#cc9393","-l","#709080","-n","#705050"] 10,
    Run Date "%a %b %_d %l:%M" "date" 10,
    Run Battery ["-t", "Bat: <left>%","-L","10","-H","11","-l","#CC9393","-h","#709080"] 10,
    Run StdinReader
],
sepChar = "%",
alignSep = "}{",
template = "%StdinReader% }{ %multicpu% | %memory% | %Vol% | %wls3% | %battery% |   <fc=#709080>%date%</fc>"

}

我意识到我的语法是一厢情愿并且可能是错误的,我喜欢 xmonad 但还没有学习过 haskell 语法。

【问题讨论】:

  • xmonad的配置文件只是一段Haskell代码,所以你可以通过Network包中的getEnv "HOST"getHostName获取主机名。但是我不认为这就是你想要的答案,所以你能解释一下你在 xmonad 的配置文件中真正想要完成的事情吗?
  • 我刚刚使用 xmobar.hs 文件中的 sn-p 进行了更新。我想“.hs”结尾应该让我知道它是代码而不是简单的配置,而是 haskell 语法对我来说很陌生。
  • @bertabus 我不确定xmobar 的最新版本,但旧版本通常会解析.xmobarrc 文件。因此,您无法在该文件中输入 cmets。如果现在仍然如此,我认为您不能在该文件中使用if else 表达式。如果最近的版本不是这种情况,我会很高兴,但我不确定。

标签: haskell xmonad


【解决方案1】:

由于xmonad.hs是一个haskell文件,你可以使用包hostname,找到它的名字:

在 ghci 中:

λ> import Network.HostName
λ> getHostName
Loading package hostname-1.0 ... linking ... done.
"hostname1"

您似乎希望为您的主机设置不同的xmobar。实现这一点的一种方法是编写一个函数,该函数将为您的给定主机创建一个新的.xmobarrc 文件。它的类型定义如下所示:

createXmobarrc :: String -> IO ()
createXmobarrc hostname = undefined -- Write your logic

然后您可以使用以下模式在 xmonad.hs 文件中的适当位置调用此方法:

main = do
 hostname <- getHostName
 createXmobarrc hostname -- produce appropriate .xmobarrc file for a given host
 -- other xmonad stuff follows here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    相关资源
    最近更新 更多