Skip to content

Commit

Permalink
Allow wrapping at a specific column
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervdj committed Feb 12, 2024
1 parent 1032678 commit 8405de8
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 9 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ are several places where you can put your configuration.
### Line wrapping

Line wrapping can be enabled by setting `wrap: true` in the configuration. This
will re-wrap all lines to fit the terminal width better.
will re-wrap all lines to fit the terminal width better. You can also ask patat
to wrap at a specific column using e.g. `wrap: 60`.

### Margins

Expand Down Expand Up @@ -352,8 +353,8 @@ patat:
Hello world
```

Setting `wrap: true` is recommended when vertically centering content if there
are any lines that are too wide for the terminal.
[Line wrapping](#line-wrapping) is recommended when vertically centering content
if there are any lines that are too wide for the terminal.

### Auto advancing

Expand Down
9 changes: 5 additions & 4 deletions lib/Patat/Presentation/Display.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ displayWithBorders (Size rows columns) pres@Presentation {..} f =
settings = activeSettings pres
ds = DisplaySettings
{ dsSize = canvasSize
, dsWrap = fromMaybe False $ psWrap settings
, dsWrap = fromMaybe NoWrap $ psWrap settings
, dsMargins = margins settings
, dsTheme = fromMaybe Theme.defaultTheme (psTheme settings)
, dsSyntaxMap = pSyntaxMap
Expand Down Expand Up @@ -204,9 +204,10 @@ prettyFragment ds (Fragment blocks) = vertical $
Auto -> (columns - dcols) `div` 2
indentation = PP.Indentation left mempty

horizontalWrap doc0
| dsWrap ds = PP.wrapAt (Just $ columns - right - left) doc0
| otherwise = doc0
horizontalWrap doc0 = case dsWrap ds of
NoWrap -> doc0
AutoWrap -> PP.wrapAt (Just $ columns - right - left) doc0
WrapAt col -> PP.wrapAt (Just col) doc0
where
right = case mRight of
Auto -> 0
Expand Down
3 changes: 2 additions & 1 deletion lib/Patat/Presentation/Display/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Patat.Presentation.Display.Internal

--------------------------------------------------------------------------------
import Patat.Presentation.Internal (Margins)
import Patat.Presentation.Settings (Wrap)
import qualified Patat.PrettyPrint as PP
import Patat.Size (Size)
import qualified Patat.Theme as Theme
Expand All @@ -16,7 +17,7 @@ import qualified Skylighting as Skylighting
--------------------------------------------------------------------------------
data DisplaySettings = DisplaySettings
{ dsSize :: !Size
, dsWrap :: !Bool
, dsWrap :: !Wrap
, dsMargins :: !Margins
, dsTheme :: !Theme.Theme
, dsSyntaxMap :: !Skylighting.SyntaxMap
Expand Down
15 changes: 14 additions & 1 deletion lib/Patat/Presentation/Settings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Patat.Presentation.Settings
( PresentationSettings (..)
, defaultPresentationSettings

, Wrap (..)
, AutoOr (..)
, MarginSettings (..)

Expand All @@ -25,6 +26,7 @@ module Patat.Presentation.Settings


--------------------------------------------------------------------------------
import Control.Applicative ((<|>))
import Control.Monad (mplus)
import qualified Data.Aeson.Extended as A
import qualified Data.Aeson.TH.Extended as A
Expand All @@ -46,7 +48,7 @@ data PresentationSettings = PresentationSettings
{ psRows :: !(Maybe (A.FlexibleNum Int))
, psColumns :: !(Maybe (A.FlexibleNum Int))
, psMargins :: !(Maybe MarginSettings)
, psWrap :: !(Maybe Bool)
, psWrap :: !(Maybe Wrap)
, psTheme :: !(Maybe Theme.Theme)
, psIncrementalLists :: !(Maybe Bool)
, psAutoAdvanceDelay :: !(Maybe (A.FlexibleNum Int))
Expand Down Expand Up @@ -101,6 +103,17 @@ defaultPresentationSettings = mempty
}


--------------------------------------------------------------------------------
data Wrap = NoWrap | AutoWrap | WrapAt Int deriving (Show)


--------------------------------------------------------------------------------
instance A.FromJSON Wrap where
parseJSON val =
((\w -> if w then AutoWrap else NoWrap) <$> A.parseJSON val) <|>
(WrapAt <$> A.parseJSON val)


--------------------------------------------------------------------------------
data AutoOr a = Auto | NotAuto a deriving (Show)

Expand Down
35 changes: 35 additions & 0 deletions tests/golden/inputs/wrap-at.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!--config:
wrap: 40
-->

This is a really long sentence which could be wrapped.

- The above sentence should be wrapped.
- This bullet list should also be wrapped.

---

<!--config:
wrap: 40
margins:
left: auto
right: auto
-->

This is a really long sentence which could be wrapped.

- The above sentence should be wrapped.
- This bullet list should also be wrapped.

---

<!--config:
wrap: 40
margins:
left: 10
-->

This is a really long sentence which could be wrapped.

- The above sentence should be wrapped.
- This bullet list should also be wrapped.
37 changes: 37 additions & 0 deletions tests/golden/outputs/wrap-at.md.dump
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
 wrap-at.md 

This is a really long sentence which
could be wrapped.

 - The above sentence should be
 wrapped.
 - This bullet list should also be
 wrapped.

 1 / 3 

{slide}
 wrap-at.md 

 This is a really long sentence which
 could be wrapped.

 - The above sentence should be
 wrapped.
 - This bullet list should also be
 wrapped.

 2 / 3 

{slide}
 wrap-at.md 

 This is a really long sentence which
 could be wrapped.

 - The above sentence should be
 wrapped.
 - This bullet list should also be
 wrapped.

 3 / 3 

0 comments on commit 8405de8

Please sign in to comment.