首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于环境变量的Xmonad集合布局

基于环境变量的Xmonad集合布局
EN

Stack Overflow用户
提问于 2020-03-17 09:29:10
回答 1查看 279关注 0票数 1

假设我有一个环境变量IS_REMOTE。我想有一套布局,如果它是"1",另一套,如果它不是,或未定义。到目前为止,我已经

代码语言:javascript
复制
import XMonad
import XMonad.Config.Desktop
import XMonad.Hooks.ManageDocks
import XMonad.Layout.LayoutModifier
import XMonad.Layout.MultiColumns
import XMonad.Layout.PerWorkspace
import XMonad.Layout.ThreeColumns
import Data.Maybe
import System.Environment

main = do
  isRemoteEnv <- lookupEnv "IS_REMOTE"
  xmonad $ desktopConfig
    {
      layoutHook = myLayout (fromMaybe "0" isRemoteEnv)
    }

myLayout remote = if remote == "1"
  then onWorkspace "web" (avoidStruts $ (multiCol [1] 1 0.02 (-0.5)) ||| Full) $
       (avoidStruts $ (ThreeColMid 1 0.02 (1/2)) ||| Full)
  else onWorkspace "web" (avoidStruts $ Mirror (multiCol [1] 1 0.02 (-0.5)) ||| Full) $
       (avoidStruts $ Mirror (ThreeColMid 1 0.02 (1/2)) ||| Full)

这不能编译,因为(我认为) if的两个分支具有不同的类型。但这是我的Haskell知识的极限。执行此操作的正确方法是什么?

代码语言:javascript
复制
xmonad.hs:21:8: error:
    * Couldn't match type `Mirror MultiCol' with `MultiCol'
      Expected type: PerWorkspace
                       (ModifiedLayout AvoidStruts (Choose MultiCol Full))
                       (ModifiedLayout AvoidStruts (Choose ThreeCol Full))
                       a
        Actual type: PerWorkspace
                       (ModifiedLayout AvoidStruts (Choose (Mirror MultiCol) Full))
                       (ModifiedLayout AvoidStruts (Choose (Mirror ThreeCol) Full))
                       a
    * In the expression:
        onWorkspace
          "web" (avoidStruts $ Mirror (multiCol [1] 1 0.02 (- 0.5)) ||| Full)
          $ (avoidStruts $ Mirror (ThreeColMid 1 0.02 (1 / 2)) ||| Full)
      In the expression:
        if remote == "1" then
            onWorkspace
              "web" (avoidStruts $ (multiCol [1] 1 0.02 (- 0.5)) ||| Full)
              $ (avoidStruts $ (ThreeColMid 1 0.02 (1 / 2)) ||| Full)
        else
            onWorkspace
              "web" (avoidStruts $ Mirror (multiCol [1] 1 0.02 (- 0.5)) ||| Full)
              $ (avoidStruts $ Mirror (ThreeColMid 1 0.02 (1 / 2)) ||| Full)
      In an equation for `myLayout':
          myLayout remote
            = if remote == "1" then
                  onWorkspace
                    "web" (avoidStruts $ (multiCol [1] 1 0.02 (- 0.5)) ||| Full)
                    $ (avoidStruts $ (ThreeColMid 1 0.02 (1 / 2)) ||| Full)
              else
                  onWorkspace
                    "web" (avoidStruts $ Mirror (multiCol [1] 1 0.02 (- 0.5)) ||| Full)
                    $ (avoidStruts $ Mirror (ThreeColMid 1 0.02 (1 / 2)) ||| Full)
   |
21 |   else onWorkspace "web" (avoidStruts $ Mirror (multiCol [1] 1 0.02 (-0.5)) ||| Full) $
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-17 11:44:54

这里的核心问题来自于Haskell缺乏对存在类型或联合类型的一流支持。一个简单得多的例子是,即使if x then show 'a' else show "abc"是有效的,show $ if x then 'a' else "abc"也是无效的。无论如何,为了解决你的问题,这不是很好,但它是有效的:

代码语言:javascript
复制
import XMonad
import XMonad.Config.Desktop
import XMonad.Hooks.ManageDocks
import XMonad.Layout.LayoutModifier
import XMonad.Layout.MultiColumns
import XMonad.Layout.PerWorkspace
import XMonad.Layout.ThreeColumns
import Data.Kind
import Data.Maybe
import System.Environment

main = do
  isRemoteEnv <- lookupEnv "IS_REMOTE"
  case myLayout (fromMaybe "0" isRemoteEnv) of Layout c -> xmonad $ desktopConfig {
      layoutHook = c
    }

myLayout remote = if remote == "1"
  then Layout $ onWorkspace "web" (avoidStruts $ (multiCol [1] 1 0.02 (-0.5)) ||| Full) $
       (avoidStruts $ (ThreeColMid 1 0.02 (1/2)) ||| Full)
  else Layout $ onWorkspace "web" (avoidStruts $ Mirror (multiCol [1] 1 0.02 (-0.5)) ||| Full) $
       (avoidStruts $ Mirror (ThreeColMid 1 0.02 (1/2)) ||| Full)

它使用the Layout existential type来使两个分支具有相同的类型。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60715148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档