From 097d51f34beb08784a6081d04615e864bdae5df0 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Tue, 30 May 2023 18:56:44 -0400 Subject: [PATCH] properly centre menu headings --- src/Mtlstats/Menu.hs | 3 +-- src/Mtlstats/Util.hs | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Mtlstats/Menu.hs b/src/Mtlstats/Menu.hs index 7c42a91..a6d6b5d 100644 --- a/src/Mtlstats/Menu.hs +++ b/src/Mtlstats/Menu.hs @@ -36,7 +36,6 @@ module Mtlstats.Menu ( import Brick.Main (halt) import Brick.Types (BrickEvent (VtyEvent), Widget) -import Brick.Widgets.Center (hCenter) import Control.Monad.State.Class (gets, modify) import Data.Char (toUpper) import qualified Data.Map as M @@ -86,7 +85,7 @@ menuStateController menuFunc = Controller drawMenu :: Menu a -> Widget () drawMenu m = let menuLines = lines $ show m - in hCenter $ linesToWidget menuLines + in linesToWidgetC menuLines -- | The event handler for a 'Menu' menuHandler :: Menu a -> Handler a diff --git a/src/Mtlstats/Util.hs b/src/Mtlstats/Util.hs index 6aaae46..4a1dda1 100644 --- a/src/Mtlstats/Util.hs +++ b/src/Mtlstats/Util.hs @@ -19,8 +19,6 @@ along with this program. If not, see . -} -{-# LANGUAGE LambdaCase #-} - module Mtlstats.Util ( nth , modifyNth @@ -29,9 +27,11 @@ module Mtlstats.Util , slice , capitalizeName , linesToWidget + , linesToWidgetC ) where import Brick.Types (Widget) +import Brick.Widgets.Center (hCenter) import Brick.Widgets.Core (str, vBox) import Data.Char (isSpace, toUpper) import qualified Data.Map as M @@ -124,9 +124,15 @@ capitalizeName ch s = s ++ [ch'] | isSpace c = lockFlag' cs | otherwise = False +-- | Converts a list of lines to a widget linesToWidget :: [String] -> Widget () -linesToWidget = vBox . map - ( str . \case - "" -> " " - s -> s - ) +linesToWidget = vBox . map (str . keepBlank) + +-- | Converts a list of lines to a widget with each line horizontally +-- centered +linesToWidgetC :: [String] -> Widget () +linesToWidgetC = vBox . map (hCenter . str . keepBlank) + +keepBlank :: String -> String +keepBlank "" = " " +keepBlank s = s