From 6de72ae6b70c95b907b410fca8ceb6094dc9e993 Mon Sep 17 00:00:00 2001 From: Jonathan Daugherty Date: Sat, 5 Aug 2017 09:20:07 -0700 Subject: [PATCH] Add txtWrapWith, strWrapWith to provide control over wrapping behavior --- brick.cabal | 3 ++- programs/TextWrapDemo.hs | 17 +++++++++++++---- src/Brick/Widgets/Core.hs | 22 ++++++++++++++++------ 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/brick.cabal b/brick.cabal index 39b13d29..3e9ee9d5 100644 --- a/brick.cabal +++ b/brick.cabal @@ -118,7 +118,8 @@ executable brick-text-wrap-demo main-is: TextWrapDemo.hs build-depends: base, brick, - text + text, + word-wrap executable brick-cache-demo if !flag(demos) diff --git a/programs/TextWrapDemo.hs b/programs/TextWrapDemo.hs index 476bdda4..58532155 100644 --- a/programs/TextWrapDemo.hs +++ b/programs/TextWrapDemo.hs @@ -2,12 +2,21 @@ module Main where import Data.Monoid ((<>)) import Brick +import Text.Wrap (defaultWrapSettings, preserveIndentation) ui :: Widget () -ui = strWrap $ "Hello, world! This line is long enough that " <> - "it's likely to wrap on your terminal if your window " <> - "isn't especially wide. Try narrowing and widening " <> - "the window to see what happens to this text." +ui = + t1 <=> (padTop (Pad 1) t2) + where + t1 = strWrap $ "Hello, world! This line is long enough that " <> + "it's likely to wrap on your terminal if your window " <> + "isn't especially wide. Try narrowing and widening " <> + "the window to see what happens to this text." + settings = defaultWrapSettings { preserveIndentation = True } + t2 = strWrapWith settings $ + "This text wraps\n" <> + " with different settings to preserve indentation\n" <> + " so that long lines wrap in nicer way." main :: IO () main = simpleMain ui diff --git a/src/Brick/Widgets/Core.hs b/src/Brick/Widgets/Core.hs index 771a6d39..b78f595b 100644 --- a/src/Brick/Widgets/Core.hs +++ b/src/Brick/Widgets/Core.hs @@ -13,8 +13,10 @@ module Brick.Widgets.Core , raw , txt , txtWrap + , txtWrapWith , str , strWrap + , strWrapWith , fill -- * Padding @@ -103,7 +105,7 @@ import Data.List (sortBy, partition) import qualified Graphics.Vty as V import Control.DeepSeq -import Text.Wrap (wrapTextToLines, defaultWrapSettings) +import Text.Wrap (wrapTextToLines, WrapSettings, defaultWrapSettings) import Brick.Types import Brick.Types.Internal @@ -201,20 +203,28 @@ takeColumns numCols (c:cs) = else "" -- | Make a widget from a string, but wrap the words in the input's --- lines at the available width. +-- lines at the available width using the default wrapping settings. strWrap :: String -> Widget n -strWrap = txtWrap . T.pack +strWrap = strWrapWith defaultWrapSettings + +-- | Make a widget from a string, but wrap the words in the input's +-- lines at the available width using the specified wrapping settings. +strWrapWith :: WrapSettings -> String -> Widget n +strWrapWith settings t = txtWrapWith settings $ T.pack t safeTextWidth :: T.Text -> Int safeTextWidth = V.safeWcswidth . T.unpack -- | Make a widget from text, but wrap the words in the input's lines at --- the available width. +-- the available width using the default wrapping settings. txtWrap :: T.Text -> Widget n -txtWrap s = +txtWrap = txtWrapWith defaultWrapSettings + +txtWrapWith :: WrapSettings -> T.Text -> Widget n +txtWrapWith settings s = Widget Fixed Fixed $ do c <- getContext - let theLines = fixEmpty <$> wrapTextToLines defaultWrapSettings (c^.availWidthL) s + let theLines = fixEmpty <$> wrapTextToLines settings (c^.availWidthL) s fixEmpty l | T.null l = " " | otherwise = l case force theLines of