简体   繁体   English

在xmonad / xmobar配置文件中获取主机名

[英]Get hostname in xmonad/xmobar config file

My dotfiles are 99% similar between computers but there are minor tweaks that I keep for various minor settings. 我的dotfile在计算机之间有99%的相似性,但是我对各种次要设置进行了一些细微调整。 My plan was to discriminate using an if statement based on hostname. 我的计划是使用基于主机名的if语句进行区分。 something that would look as follows in a shell config like bashrc or zshrc 在诸如bashrc或zshrc的shell配置中看起来如下所示

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

I suspect that xmobar is simply a config file that is parsed with no real haskell in it. 我怀疑xmobar只是一个解析的配置文件,其中没有真正的haskell。 Any thoughts on how to get something similar to what I have with a shell in xmobar? 关于如何获得与xmobar中的shell类似的任何想法?

Mainly I am wanting to modify the widths and network interfaces in xmobar something like 主要是我想修改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>"

}

I realize my syntax is wishful and likely wrong, I love xmonad but have not learned haskell syntax yet. 我意识到我的语法是一厢情愿的并且可能是错误的,我喜欢xmonad,但是还没有学过haskell语法。

Since xmonad.hs is a haskell file, you can use the package hostname , to find it's name: 由于xmonad.hs是haskell文件,因此可以使用包hostname来查找其名称:

In ghci: 在ghci中:

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

It seems you want to have different xmobar settings for your host. 看来您想为主机设置不同的xmobar设置。 One way to achieve that would be to write a function that will create a new .xmobarrc file for your given host. 一种实现方法是编写一个函数,该函数将为给定的主机创建一个新的.xmobarrc文件。 It's type definition will look something like this: 它的类型定义看起来像这样:

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

You can then call this method in the appropriate place in your xmonad.hs file using the following pattern: 然后,可以使用以下模式在xmonad.hs文件中的适当位置调用此方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM