Very basic incomplete text editor experiment

This commit is contained in:
Jonathan Daugherty 2015-05-09 01:19:09 -07:00
parent 4fc7813918
commit 5e91b0b617
2 changed files with 28 additions and 3 deletions

View File

@ -10,12 +10,13 @@ import Brick
data St =
St { msg :: String
, focus :: FocusRing
, stEditor :: Editor
}
drawUI :: St -> Widget
drawUI st =
let editor = txt (msg st) `withNamedCursor` (Name "edit", Location (length $ msg st, 0))
in vBox [ editor `withAttr` (cyan `on` blue)
let ew = edit (stEditor st)
in vBox [ ew `withAttr` (cyan `on` blue)
, hBorder '-'
, "stuff and things"
]
@ -34,8 +35,10 @@ pickCursor st ls =
initialState :: St
initialState =
let eName = Name "edit"
St { msg = ""
, focus = focusRing [Name "edit"]
, focus = focusRing [eName]
, stEditor = editor eName ""
}
main :: IO ()

View File

@ -34,6 +34,28 @@ txt s = Widget { render = \_ _ a -> ( string a s
instance IsString Widget where
fromString = txt
data Editor =
Editor { editStr :: String
, editCursorPos :: Int
, editorName :: Name
}
editor :: Name -> String -> Editor
editor name s = Editor s (length name) name
edit :: Editor -> Widget
edit e =
Widget { render = renderEditor
}
where
renderEditor loc sz@(width, _) attr =
let cursorPos = CursorLocation (Location (editCursorPos e, 0)) (Just $ editorName e)
w = hBox [ txt (editStr e)
, txt (replicate (width - (length $ editStr e)) ' ')
]
(img, _) = render_ w loc sz attr
in (img, [cursorPos])
hBorder :: Char -> Widget
hBorder ch =
Widget { render = \_ (width, _) attr -> ( charFill attr ch width 1