1
1
mirror of https://github.com/Yvee1/hascard.git synced 2024-11-22 12:51:58 +03:00

Add centered wrapped text widget

This commit is contained in:
Yvee1 2020-07-09 13:07:51 +02:00
parent bd2eedd52f
commit 641432b570
8 changed files with 79 additions and 23 deletions

View File

@ -2,6 +2,15 @@ module Main where
import Lib (runBrickFlashcards)
import System.Environment (getArgs)
import BrickHelpers
import Brick
import Brick.Widgets.Center
main :: IO ()
main = runBrickFlashcards
main = runBrickFlashcards
-- test :: Widget ()
-- test = hCenter $ myStrWrap "Heey, this is a very long string of text, well maybe not that long... but pretty long in any case."
-- main :: IO ()
-- main = simpleMain test

View File

@ -20,3 +20,33 @@ Part of the eye that turns _light_ into _electrical neural impulses_.
---
# Choroid
The choroid seperates from sclera and becomes the _ciliary body_, and then the _iris_.
---
# LGN
_lateral geniculate nuclei_
---
# LGN 6 layers
2 layers receive signals from _M_-ganglion cells.
4 layers receive signals from _P_-ganglion cells.
between is a thin layers that receives signals from _K_-ganglion cells.
---
# Color vision: types of cones
Three different types of cones, for different wavelengths.
S (short) corresponds roughly to the colour _blue_.
M (medium) corresponds roughly to the colour _green_.
L (long) corresponds roughly to the colour _red_.
---
# Color vision: color channels
Achromatic channel = M + L
Blue-yellow channel = M + L - S
Red-green channel = L + S - M
---
# The visual system
The visual system consists of two parts:
- An optical system that produces an image on the retina.
- An image-processing system. This spends most of its resources on the fovea.
---

View File

@ -21,7 +21,7 @@ description: Please see the README on GitHub at <https://github.com/Yvee
dependencies:
- base >= 4.7 && < 5
- brick
- brick >= 0.52
- word-wrap
- vty
- microlens-platform
@ -31,6 +31,7 @@ dependencies:
- text
- vector
- filepath
- microlens
library:
source-dirs: src

16
src/BrickHelpers.hs Normal file
View File

@ -0,0 +1,16 @@
module BrickHelpers where
import Text.Wrap
import Brick
import Brick.Widgets.Center
import Data.Text (pack)
import Lens.Micro
-- hCenteredStrWrap :: String -> Widget ()
-- hCenteredStrWrap = myStrWrap'
hCenteredStrWrap :: String -> Widget n
hCenteredStrWrap p = Widget Greedy Fixed $ do
c <- getContext
let w = c^.availWidthL
let result = vBox $ map (hCenter . txt) $ wrapTextToLines defaultWrapSettings w (pack p)
render result

View File

@ -3,10 +3,13 @@
module CardUI (runCardUI) where
import Brick
import BrickHelpers
import Lens.Micro.Platform
import Parser
import Types
import Data.Char (isSeparator)
import Data.Map.Strict (Map)
import Text.Wrap (WrapSettings(..))
import qualified Data.Map.Strict as M
import qualified Brick.Widgets.Border as B
import qualified Brick.Widgets.Border.Style as BS
@ -69,11 +72,11 @@ drawProgress s = C.hCenter $ str (show (s^.index + 1) ++ "/" ++ show (s^.nCards)
drawHeader :: String -> Widget Name
drawHeader title = withAttr titleAttr $
padLeftRight 1 $
C.hCenter (strWrap title)
hCenteredStrWrap title
drawDescr :: String -> Widget Name
drawDescr descr = padLeftRight 1 $
strWrap descr
strWrapWith (WrapSettings {preserveIndentation=False, breakLongWords=True}) descr
listMultipleChoice :: CorrectOption -> [IncorrectOption] -> [String]
listMultipleChoice c = reverse . listMultipleChoice' [] 0 c
@ -102,10 +105,14 @@ applyWhen predicate action = if predicate then action else id
applyUnless :: Bool -> (a -> a) -> a -> a
applyUnless p = applyWhen (not p)
drawDef :: State -> String -> Widget Name
drawHintedDef :: State -> String -> Widget Name
drawHintedDef s def = case s ^. cardState of
DefinitionState {_flipped=f} -> if f then drawDescr def else drawDescr [if isSeparator char || char == '\n' then char else '_' | char <- def]
_ -> error "impossible: "
drawDef:: State -> String -> Widget Name
drawDef s def = case s ^. cardState of
DefinitionState {_flipped=f} -> applyUnless f (withAttr hiddenAttr) $ drawDescr def
DefinitionState {_flipped=f} -> if f then drawDescr def else drawDescr [if char == '\n' then char else ' ' | char <- def]
_ -> error "impossible: "
drawOptions :: State -> [String] -> Widget Name
@ -144,6 +151,9 @@ handleEvent :: State -> BrickEvent Name Event -> EventM Name (Next State)
handleEvent s (VtyEvent ev) = case ev of
V.EvKey V.KEsc [] -> halt s
V.EvKey (V.KChar 'c') [V.MCtrl] -> halt s
V.EvKey V.KRight [] -> next s
V.EvKey V.KLeft [] -> previous s
-- V.EvKey (V.KChar ' ') [] -> next s
ev -> case s ^. cardState of
MultipleChoiceState {_selected = i, _nChoices = nChoices} ->
case ev of
@ -209,9 +219,6 @@ handleEvent s (VtyEvent ev) = case ev of
backspace xs = init xs
_ -> continue s
handleEvent s _ = continue s
-- handleEvent s (VtyEvent (V.EvKey V.KRight [])) = next s
-- handleEvent s (VtyEvent (V.EvKey (V.KChar ' ') [])) = next s
-- handleEvent s (VtyEvent (V.EvKey V.KLeft [])) = previous s
titleAttr :: AttrName
titleAttr = attrName "title"

View File

@ -29,13 +29,6 @@ title = withAttr titleAttr $
str "┬ ┬┌─┐┌─┐┌─┐┌─┐┬─┐┌┬┐" <=>
str "├─┤├─┤└─┐│ ├─┤├┬┘ ││" <=>
str "┴ ┴┴ ┴└─┘└─┘┴ ┴┴└──┴┘"
-- str " _ _" <=>
-- str "| | | |"<=>
-- str "| |__ __ _ ___ ___ __ _ _ __ __| |"<=>
-- str "| '_ \\ / _` / __|/ __/ _` | '__/ _` |"<=>
-- str "| | | | (_| \\__ \\ (_| (_| | | | (_| |"<=>
-- str "|_| |_|\\__,_|___/\\___\\__,_|_| \\__,_|"
drawUI :: State -> [Widget Name]
drawUI s =

View File

@ -17,7 +17,7 @@
#
# resolver: ./custom-snapshot.yaml
# resolver: https://example.com/snapshots/2018-01-01.yaml
resolver: lts-14.20
resolver: lts-16.2
# User packages to be built.
# Various formats can be used as shown in the example below.
@ -34,7 +34,7 @@ packages:
# These entries can reference officially published versions as well as
# forks / in-progress versions pinned to a git hash. For example:
#
# extra-deps:
extra-deps:
# - acme-missiles-0.3
# - git: https://github.com/commercialhaskell/stack.git
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a

View File

@ -6,7 +6,7 @@
packages: []
snapshots:
- completed:
size: 524154
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/14/20.yaml
sha256: 2f5099f69ddb6abfe64400fe1e6a604e8e628f55e6837211cd70a81eb0a8fa4d
original: lts-14.20
size: 531674
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/16/2.yaml
sha256: 4b08fb9338ca297de7ade8318f4be1216f14dff8c0426c001fb7886ee88cb84a
original: lts-16.2