Skip to content

Commit

Permalink
Add txtWrapWith, strWrapWith to provide control over wrapping behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jtdaugherty committed Aug 5, 2017
1 parent 603cdca commit 6de72ae
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
3 changes: 2 additions & 1 deletion brick.cabal
Expand Up @@ -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)
Expand Down
17 changes: 13 additions & 4 deletions programs/TextWrapDemo.hs
Expand Up @@ -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
22 changes: 16 additions & 6 deletions src/Brick/Widgets/Core.hs
Expand Up @@ -13,8 +13,10 @@ module Brick.Widgets.Core
, raw
, txt
, txtWrap
, txtWrapWith
, str
, strWrap
, strWrapWith
, fill

-- * Padding
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6de72ae

Please sign in to comment.