具有Haskell和Brick的RGB终端颜色

问题描述

我知道BrickVTY黑客不支持转义序列。 VTY仅支持240种颜色。

是否有使用真正的RGB颜色而不弄乱布局的解决方法?

这是我做的一个示例,但是我无法正确设置边框:

module BrickTest where

import Brick                (simpleMain,Widget,str)
import Brick.Widgets.Border (border)
import Text.Printf          (printf)

main :: IO ()
main = simpleMain $ colorWidget (255,0)

type RGB = (Int,Int,Int)

colorWidget :: RGB -> Widget ()
colorWidget (r,g,b) = border $ str (prefix ++ "a" ++ postfix)
    where
        prefix = printf "\ESC[38;2;%d;%d;%dm" r g b
        postfix = "\ESC[0m"

输出:

┌──────────────────┐
│a│
└──────────────────┘

terminal screenshot

解决方法

我找到了解决方法。我设法实现了一个可以打印任何字符串的函数zeroWidthStr,而Brick则将其视为宽度为0的字符串进行处理。但是我无法真正解释它在内部的工作方式,并且可能会有其他一些副作用。

module BrickTest where

import           Brick                       (Widget,raw,simpleMain,str,(<+>))
import           Brick.Widgets.Border        (border)
import           Data.List                   (intercalate)
import           Data.Text.Lazy              (pack)
import           Graphics.Vty                (defAttr)
import           Graphics.Vty.Image.Internal (Image (HorizText))
import           Text.Printf                 (printf)

main :: IO ()
main = simpleMain $ colorWidget (255,0)

type RGB = (Int,Int,Int)

colorWidget :: RGB -> Widget ()
colorWidget (r,g,b) = border $ prefix <+> str "a" <+> postfix
    where
        prefix = zeroWidthStr $ printf "\ESC[38;2;%d;%d;%dm" r g b
        postfix = zeroWidthStr $ "\ESC[0m"

zeroWidthStr :: String -> Widget ()
-- | workaround to print any string in terminal,and hackage Brick (vty) handles it as if it has width 0
zeroWidthStr str = raw image
    where
        image = HorizText defAttr (pack modStr) 0 0
        modStr = str ++ repeatN "\ESC\ESCa" (length str)
        repeatN :: String -> Int -> String
        repeatN str n = intercalate "" $ take n $ repeat str

输出:

terminal screenshot

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...