mirror of
https://github.com/jtdaugherty/brick.git
synced 2024-12-12 12:23:21 +03:00
Edit: docstrings, export only useful lenses
This commit is contained in:
parent
00798b78b3
commit
8399654b76
@ -1,12 +1,24 @@
|
|||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
{-# LANGUAGE TemplateHaskell #-}
|
{-# LANGUAGE TemplateHaskell #-}
|
||||||
|
-- | This module provides a basic single-line text editor widget. You'll
|
||||||
|
-- need to embed an 'Editor' in your application state and transform it
|
||||||
|
-- with 'handleEvent' when relevant events arrive. To get the contents
|
||||||
|
-- of the editor, just use 'editContents'.
|
||||||
|
--
|
||||||
|
-- The editor's 'HandleEvent' instance handles a set of basic input
|
||||||
|
-- events that should suffice for most purposes; see the source for a
|
||||||
|
-- complete list.
|
||||||
module Brick.Widgets.Edit
|
module Brick.Widgets.Edit
|
||||||
( Editor(editContents, editCursorPos, editorName, editDrawContents)
|
( Editor(editContents, editCursorPos, editorName, editDrawContents)
|
||||||
|
-- * Constructing an editor
|
||||||
, editor
|
, editor
|
||||||
|
-- * Lenses for working with editors
|
||||||
, editContentsL
|
, editContentsL
|
||||||
, editCursorPosL
|
, editCursorPosL
|
||||||
, editorNameL
|
, editDrawContentsL
|
||||||
|
-- * Rendering editors
|
||||||
, renderEditor
|
, renderEditor
|
||||||
|
-- * Attributes
|
||||||
, editAttr
|
, editAttr
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
@ -20,11 +32,16 @@ import Brick.Widgets.Core
|
|||||||
import Brick.Util (clamp)
|
import Brick.Util (clamp)
|
||||||
import Brick.AttrMap
|
import Brick.AttrMap
|
||||||
|
|
||||||
|
-- | Editor state.
|
||||||
data Editor =
|
data Editor =
|
||||||
Editor { editContents :: !String
|
Editor { editContents :: !String
|
||||||
|
-- ^ The contents of the editor
|
||||||
, editCursorPos :: !Int
|
, editCursorPos :: !Int
|
||||||
|
-- ^ The editor's cursor position
|
||||||
, editDrawContents :: String -> Widget
|
, editDrawContents :: String -> Widget
|
||||||
|
-- ^ The function the editor uses to draw its contents
|
||||||
, editorName :: Name
|
, editorName :: Name
|
||||||
|
-- ^ The name of the editor
|
||||||
}
|
}
|
||||||
|
|
||||||
suffixLenses ''Editor
|
suffixLenses ''Editor
|
||||||
@ -80,12 +97,21 @@ insertChar c theEdit =
|
|||||||
listInsert :: a -> Int -> [a] -> [a]
|
listInsert :: a -> Int -> [a] -> [a]
|
||||||
listInsert a i as = take i as ++ [a] ++ drop i as
|
listInsert a i as = take i as ++ [a] ++ drop i as
|
||||||
|
|
||||||
editor :: Name -> (String -> Widget) -> String -> Editor
|
-- | Construct an editor.
|
||||||
|
editor :: Name
|
||||||
|
-- ^ The editor's name (must be unique)
|
||||||
|
-> (String -> Widget)
|
||||||
|
-- ^ The content rendering function
|
||||||
|
-> String
|
||||||
|
-- ^ The initial content
|
||||||
|
-> Editor
|
||||||
editor name draw s = Editor s (length s) draw name
|
editor name draw s = Editor s (length s) draw name
|
||||||
|
|
||||||
|
-- | The attribute assigned to the editor
|
||||||
editAttr :: AttrName
|
editAttr :: AttrName
|
||||||
editAttr = "edit"
|
editAttr = "edit"
|
||||||
|
|
||||||
|
-- | Turn an editor state value into a widget
|
||||||
renderEditor :: Editor -> Widget
|
renderEditor :: Editor -> Widget
|
||||||
renderEditor e =
|
renderEditor e =
|
||||||
let cursorLoc = Location (e^.editCursorPosL, 0)
|
let cursorLoc = Location (e^.editCursorPosL, 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user