summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNorfair <>2019-08-13 10:44:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2019-08-13 10:44:00 (GMT)
commit23fa8afc7c4af2a2fc06c1361ceaed47cf62a08e (patch)
treea5c63b7b3dad68c5cf8c9ca3e50327f8516aeb8f
parentbea4d08fb030caa87d60238a05490deb838a7445 (diff)
version 0.1.0.0HEAD0.1.0.0master
-rw-r--r--Setup.hs1
-rw-r--r--cursor-brick.cabal7
-rw-r--r--src/Cursor/Brick.hs14
-rw-r--r--src/Cursor/Brick/Forest.hs24
-rw-r--r--src/Cursor/Brick/List.hs6
-rw-r--r--src/Cursor/Brick/List/NonEmpty.hs32
-rw-r--r--src/Cursor/Brick/Map.hs30
-rw-r--r--src/Cursor/Brick/Map/KeyValue.hs11
-rw-r--r--src/Cursor/Brick/Text.hs69
-rw-r--r--src/Cursor/Brick/TextField.hs36
-rw-r--r--src/Cursor/Brick/Tree.hs3
11 files changed, 189 insertions, 44 deletions
diff --git a/Setup.hs b/Setup.hs
index 9a994af..e8ef27d 100644
--- a/Setup.hs
+++ b/Setup.hs
@@ -1,2 +1,3 @@
import Distribution.Simple
+
main = defaultMain
diff --git a/cursor-brick.cabal b/cursor-brick.cabal
index 12c3c96..4d2c1d6 100644
--- a/cursor-brick.cabal
+++ b/cursor-brick.cabal
@@ -4,10 +4,10 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
--- hash: 7d8cfb93a2e1e9e9cbfb16cbe022245b498e38c2b591cb9da187759a356bee6f
+-- hash: dd4cb07a29abf9dd36525dc5fa9f67eb370539e7b2f41f461bafbd5e117883d9
name: cursor-brick
-version: 0.0.0.0
+version: 0.1.0.0
description: Please see the README on GitHub at <https://github.com/NorfairKing/cursor-brick#readme>
homepage: https://github.com/NorfairKing/cursor-brick#readme
bug-reports: https://github.com/NorfairKing/cursor-brick/issues
@@ -32,6 +32,8 @@ library
Cursor.Brick.List.NonEmpty
Cursor.Brick.Map
Cursor.Brick.Map.KeyValue
+ Cursor.Brick.Text
+ Cursor.Brick.TextField
Cursor.Brick.Tree
other-modules:
Paths_cursor_brick
@@ -41,4 +43,5 @@ library
base >=4.7 && <5
, brick
, cursor
+ , text
default-language: Haskell2010
diff --git a/src/Cursor/Brick.hs b/src/Cursor/Brick.hs
index 3df9c03..724752c 100644
--- a/src/Cursor/Brick.hs
+++ b/src/Cursor/Brick.hs
@@ -1,7 +1,19 @@
module Cursor.Brick
- ( module Cursor.Brick.List
+ ( module Cursor.Brick.Forest
+ , module Cursor.Brick.List
, module Cursor.Brick.List.NonEmpty
+ , module Cursor.Brick.Map
+ , module Cursor.Brick.Map.KeyValue
+ , module Cursor.Brick.Text
+ , module Cursor.Brick.TextField
+ , module Cursor.Brick.Tree
) where
+import Cursor.Brick.Forest
import Cursor.Brick.List
import Cursor.Brick.List.NonEmpty
+import Cursor.Brick.Map
+import Cursor.Brick.Map.KeyValue
+import Cursor.Brick.Text
+import Cursor.Brick.TextField
+import Cursor.Brick.Tree
diff --git a/src/Cursor/Brick/Forest.hs b/src/Cursor/Brick/Forest.hs
index bb1fa07..cb416d7 100644
--- a/src/Cursor/Brick/Forest.hs
+++ b/src/Cursor/Brick/Forest.hs
@@ -3,16 +3,12 @@
module Cursor.Brick.Forest where
-import Control.Monad
-
import Cursor.Forest
import Cursor.Tree
import Brick.Types
-import Brick.Widgets.Core
import Cursor.Brick.List.NonEmpty
-import Cursor.Brick.Tree
horizontalForestCursorWidgetM ::
Monad m
@@ -22,7 +18,8 @@ horizontalForestCursorWidgetM ::
-> ForestCursor a b
-> m (Widget n)
horizontalForestCursorWidgetM prevFunc curFunc nextFunc =
- horizontalNonEmptyCursorWidgetM prevFunc curFunc nextFunc . forestCursorListCursor
+ horizontalNonEmptyCursorWidgetM prevFunc curFunc nextFunc .
+ forestCursorListCursor
horizontalForestCursorWidget ::
(CTree b -> Widget n)
@@ -31,7 +28,8 @@ horizontalForestCursorWidget ::
-> ForestCursor a b
-> Widget n
horizontalForestCursorWidget prevFunc curFunc nextFunc =
- horizontalNonEmptyCursorWidget prevFunc curFunc nextFunc . forestCursorListCursor
+ horizontalNonEmptyCursorWidget prevFunc curFunc nextFunc .
+ forestCursorListCursor
verticalForestCursorWidgetM ::
Monad m
@@ -41,7 +39,8 @@ verticalForestCursorWidgetM ::
-> ForestCursor a b
-> m (Widget n)
verticalForestCursorWidgetM prevFunc curFunc nextFunc =
- verticalNonEmptyCursorWidgetM prevFunc curFunc nextFunc . forestCursorListCursor
+ verticalNonEmptyCursorWidgetM prevFunc curFunc nextFunc .
+ forestCursorListCursor
verticalForestCursorWidget ::
(CTree b -> Widget n)
@@ -50,12 +49,17 @@ verticalForestCursorWidget ::
-> ForestCursor a b
-> Widget n
verticalForestCursorWidget prevFunc curFunc nextFunc =
- verticalNonEmptyCursorWidget prevFunc curFunc nextFunc . forestCursorListCursor
+ verticalNonEmptyCursorWidget prevFunc curFunc nextFunc .
+ forestCursorListCursor
forestCursorWidgetM ::
- ([CTree b] -> TreeCursor a b -> [CTree b] -> m (Widget n)) -> ForestCursor a b -> m (Widget n)
+ ([CTree b] -> TreeCursor a b -> [CTree b] -> m (Widget n))
+ -> ForestCursor a b
+ -> m (Widget n)
forestCursorWidgetM = foldForestCursor
forestCursorWidget ::
- ([CTree b] -> TreeCursor a b -> [CTree b] -> Widget n) -> ForestCursor a b -> Widget n
+ ([CTree b] -> TreeCursor a b -> [CTree b] -> Widget n)
+ -> ForestCursor a b
+ -> Widget n
forestCursorWidget = foldForestCursor
diff --git a/src/Cursor/Brick/List.hs b/src/Cursor/Brick/List.hs
index 29d05d4..0d703c8 100644
--- a/src/Cursor/Brick/List.hs
+++ b/src/Cursor/Brick/List.hs
@@ -4,10 +4,10 @@ module Cursor.Brick.List where
import Cursor.List
-import Brick.Types as B
-import Brick.Widgets.Core as B
+import Brick.Types
-listCursorWidgetM :: ([a] -> [a] -> m (Widget n)) -> ListCursor a -> m (Widget n)
+listCursorWidgetM ::
+ ([a] -> [a] -> m (Widget n)) -> ListCursor a -> m (Widget n)
listCursorWidgetM = foldListCursor
listCursorWidget :: ([a] -> [a] -> Widget n) -> ListCursor a -> Widget n
diff --git a/src/Cursor/Brick/List/NonEmpty.hs b/src/Cursor/Brick/List/NonEmpty.hs
index 895fa50..5fb10d9 100644
--- a/src/Cursor/Brick/List/NonEmpty.hs
+++ b/src/Cursor/Brick/List/NonEmpty.hs
@@ -16,13 +16,20 @@ horizontalNonEmptyCursorWidgetM ::
-> f (Widget n)
horizontalNonEmptyCursorWidgetM beforeFunc curFunc afterFunc =
nonEmptyCursorWidgetM $ \befores current afters ->
- hBox <$> sequenceA (concat [map beforeFunc befores, [curFunc current], map afterFunc afters])
+ hBox <$>
+ sequenceA
+ (concat [map beforeFunc befores, [curFunc current], map afterFunc afters])
horizontalNonEmptyCursorWidget ::
- (b -> Widget n) -> (a -> Widget n) -> (b -> Widget n) -> NonEmptyCursor a b -> Widget n
+ (b -> Widget n)
+ -> (a -> Widget n)
+ -> (b -> Widget n)
+ -> NonEmptyCursor a b
+ -> Widget n
horizontalNonEmptyCursorWidget beforeFunc curFunc afterFunc =
nonEmptyCursorWidget $ \befores current afters ->
- hBox $ concat [map beforeFunc befores, [curFunc current], map afterFunc afters]
+ hBox $
+ concat [map beforeFunc befores, [curFunc current], map afterFunc afters]
verticalNonEmptyCursorWidgetM ::
Applicative f
@@ -33,16 +40,25 @@ verticalNonEmptyCursorWidgetM ::
-> f (Widget n)
verticalNonEmptyCursorWidgetM beforeFunc curFunc afterFunc =
nonEmptyCursorWidgetM $ \befores current afters ->
- vBox <$> sequenceA (concat [map beforeFunc befores, [curFunc current], map afterFunc afters])
+ vBox <$>
+ sequenceA
+ (concat [map beforeFunc befores, [curFunc current], map afterFunc afters])
verticalNonEmptyCursorWidget ::
- (b -> Widget n) -> (a -> Widget n) -> (b -> Widget n) -> NonEmptyCursor a b -> Widget n
+ (b -> Widget n)
+ -> (a -> Widget n)
+ -> (b -> Widget n)
+ -> NonEmptyCursor a b
+ -> Widget n
verticalNonEmptyCursorWidget beforeFunc curFunc afterFunc =
nonEmptyCursorWidget $ \befores current afters ->
- vBox $ concat [map beforeFunc befores, [curFunc current], map afterFunc afters]
+ vBox $
+ concat [map beforeFunc befores, [curFunc current], map afterFunc afters]
-nonEmptyCursorWidgetM :: ([b] -> a -> [b] -> m (Widget n)) -> NonEmptyCursor a b -> m (Widget n)
+nonEmptyCursorWidgetM ::
+ ([b] -> a -> [b] -> m (Widget n)) -> NonEmptyCursor a b -> m (Widget n)
nonEmptyCursorWidgetM = foldNonEmptyCursor
-nonEmptyCursorWidget :: ([b] -> a -> [b] -> Widget n) -> NonEmptyCursor a b -> Widget n
+nonEmptyCursorWidget ::
+ ([b] -> a -> [b] -> Widget n) -> NonEmptyCursor a b -> Widget n
nonEmptyCursorWidget = foldNonEmptyCursor
diff --git a/src/Cursor/Brick/Map.hs b/src/Cursor/Brick/Map.hs
index c9d0c97..111214d 100644
--- a/src/Cursor/Brick/Map.hs
+++ b/src/Cursor/Brick/Map.hs
@@ -3,17 +3,11 @@
module Cursor.Brick.Map where
-import Control.Monad
-
-import Cursor.List.NonEmpty
import Cursor.Map
import Brick.Types
import Brick.Widgets.Core
-import Cursor.Brick.List.NonEmpty
-import Cursor.Brick.Map.KeyValue
-
horizontalMapCursorWidget ::
(k -> v -> Widget n)
-> (KeyValueCursor kc vc k v -> Widget n)
@@ -23,7 +17,11 @@ horizontalMapCursorWidget ::
horizontalMapCursorWidget beforeFunc curFunc afterFunc =
mapCursorWidget $ \befores current afters ->
hBox $
- concat [map (uncurry beforeFunc) befores, [curFunc current], map (uncurry afterFunc) afters]
+ concat
+ [ map (uncurry beforeFunc) befores
+ , [curFunc current]
+ , map (uncurry afterFunc) afters
+ ]
horizontalMapCursorWidgetM ::
Applicative f
@@ -36,7 +34,11 @@ horizontalMapCursorWidgetM beforeFunc curFunc afterFunc =
mapCursorWidgetM $ \befores current afters ->
hBox <$>
sequenceA
- (concat [map (uncurry beforeFunc) befores, [curFunc current], map (uncurry afterFunc) afters])
+ (concat
+ [ map (uncurry beforeFunc) befores
+ , [curFunc current]
+ , map (uncurry afterFunc) afters
+ ])
verticalMapCursorWidget ::
(k -> v -> Widget n)
@@ -47,7 +49,11 @@ verticalMapCursorWidget ::
verticalMapCursorWidget beforeFunc curFunc afterFunc =
mapCursorWidget $ \befores current afters ->
vBox $
- concat [map (uncurry beforeFunc) befores, [curFunc current], map (uncurry afterFunc) afters]
+ concat
+ [ map (uncurry beforeFunc) befores
+ , [curFunc current]
+ , map (uncurry afterFunc) afters
+ ]
verticalMapCursorWidgetM ::
Applicative f
@@ -60,7 +66,11 @@ verticalMapCursorWidgetM beforeFunc curFunc afterFunc =
mapCursorWidgetM $ \befores current afters ->
vBox <$>
sequenceA
- (concat [map (uncurry beforeFunc) befores, [curFunc current], map (uncurry afterFunc) afters])
+ (concat
+ [ map (uncurry beforeFunc) befores
+ , [curFunc current]
+ , map (uncurry afterFunc) afters
+ ])
mapCursorWidget ::
([(k, v)] -> KeyValueCursor kc vc k v -> [(k, v)] -> Widget n)
diff --git a/src/Cursor/Brick/Map/KeyValue.hs b/src/Cursor/Brick/Map/KeyValue.hs
index 1cc7a71..550b65f 100644
--- a/src/Cursor/Brick/Map/KeyValue.hs
+++ b/src/Cursor/Brick/Map/KeyValue.hs
@@ -3,18 +3,15 @@
module Cursor.Brick.Map.KeyValue where
-import Control.Monad
-
-import Cursor.List.NonEmpty
import Cursor.Map
import Brick.Types
-import Brick.Widgets.Core
-
-import Cursor.Brick.List.NonEmpty
keyValueWidget ::
- (kc -> v -> Widget n) -> (k -> vc -> Widget n) -> KeyValueCursor kc vc k v -> Widget n
+ (kc -> v -> Widget n)
+ -> (k -> vc -> Widget n)
+ -> KeyValueCursor kc vc k v
+ -> Widget n
keyValueWidget = foldKeyValueCursor
keyValueWidgetM ::
diff --git a/src/Cursor/Brick/Text.hs b/src/Cursor/Brick/Text.hs
new file mode 100644
index 0000000..0890c9c
--- /dev/null
+++ b/src/Cursor/Brick/Text.hs
@@ -0,0 +1,69 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module Cursor.Brick.Text where
+
+import qualified Data.Text as T
+import Data.Text (Text)
+
+import Cursor.Text
+
+import Brick.Types as Brick
+import Brick.Widgets.Core as Brick
+
+-- | Make a text cursor widget with a blink-y box.
+--
+-- This function does not wrap the given text.
+--
+-- Otherwise, because of the way indexes work, there would be rendering errors for text that crosses the side of the terminal.
+selectedTextCursorWidget :: n -> TextCursor -> Widget n
+selectedTextCursorWidget n tc =
+ Brick.showCursor n (Brick.Location (textCursorIndex tc, 0)) $
+ textCursorWidget tc
+
+-- | Make a text cursor widget without a blink-y box.
+--
+-- This function does not wrap the given text.
+textCursorWidget :: TextCursor -> Widget n
+textCursorWidget tc =
+ txt $
+ let t = sanitiseText $ rebuildTextCursor tc
+ in if T.null t
+ then " "
+ else t
+
+-- | Draw an arbitrary Text, it will be sanitised.
+textWidget :: Text -> Widget n
+textWidget = txt . nonNullLinesText . sanitiseText
+
+-- | Draw an arbitrary Text (with wrapping), it will be sanitised.
+textWidgetWrap :: Text -> Widget n
+textWidgetWrap = txtWrap . nonNullLinesText . sanitiseText
+
+-- | Draw an arbitrary single-line Text, it will be sanitised.
+textLineWidget :: Text -> Widget n
+textLineWidget = txt . nonNullText . sanitiseText
+
+-- | Draw an arbitrary single-line Text (with wrapping), it will be sanitised.
+textLineWidgetWrap :: Text -> Widget n
+textLineWidgetWrap = txtWrap . nonNullText . sanitiseText
+
+-- | Makes every line of a Text non-empty using `nonNullText`
+nonNullLinesText :: Text -> Text
+nonNullLinesText = T.intercalate "\n" . map nonNullText . T.splitOn "\n"
+
+-- | Makes a text non-empty.
+--
+-- This turns the empty text into " " and leaves other text as-is.
+nonNullText :: Text -> Text
+nonNullText "" = " "
+nonNullText t = t
+
+-- | Replace tabs by spaces so that brick doesn't render nonsense.
+--
+-- See https://hackage.haskell.org/package/brick/docs/Brick-Widgets-Core.html#v:txt
+sanitiseText :: Text -> Text
+sanitiseText =
+ T.map $ \c ->
+ case c of
+ '\t' -> ' '
+ _ -> c
diff --git a/src/Cursor/Brick/TextField.hs b/src/Cursor/Brick/TextField.hs
new file mode 100644
index 0000000..b20c5b8
--- /dev/null
+++ b/src/Cursor/Brick/TextField.hs
@@ -0,0 +1,36 @@
+module Cursor.Brick.TextField where
+
+import Cursor.List.NonEmpty
+import Cursor.TextField
+
+import Brick.Types as Brick
+import Brick.Widgets.Core as Brick
+
+import Cursor.Brick.Text
+
+-- | Make a textfield cursor widget with a blink-y box.
+--
+-- This function does not wrap lines.
+-- Otherwise, because of the way indexes work, there would be rendering errors for text that crosses the side of the terminal.
+selectedTextFieldCursorWidget :: n -> TextFieldCursor -> Widget n
+selectedTextFieldCursorWidget n (TextFieldCursor tfc) =
+ flip foldNonEmptyCursor tfc $ \befores current afters ->
+ vBox $
+ concat
+ [ map textWidget befores
+ , [selectedTextCursorWidget n current]
+ , map textWidget afters
+ ]
+
+-- | Make a textfield cursor widget without a blink-y box.
+--
+-- This function does not wrap lines .
+textFieldCursorWidget :: TextFieldCursor -> Widget n
+textFieldCursorWidget (TextFieldCursor tfc) =
+ flip foldNonEmptyCursor tfc $ \befores current afters ->
+ vBox $
+ concat
+ [ map textWidget befores
+ , [textCursorWidget current]
+ , map textWidget afters
+ ]
diff --git a/src/Cursor/Brick/Tree.hs b/src/Cursor/Brick/Tree.hs
index ecfd5c9..8a55e6f 100644
--- a/src/Cursor/Brick/Tree.hs
+++ b/src/Cursor/Brick/Tree.hs
@@ -3,12 +3,9 @@
module Cursor.Brick.Tree where
-import Control.Monad
-
import Cursor.Tree
import Brick.Types
-import Brick.Widgets.Core
treeCursorWidgetM ::
forall a b n m. Monad m