1
1
mirror of https://github.com/Yvee1/hascard.git synced 2024-10-06 03:58:29 +03:00

Update info and fix some nav. issues

This commit is contained in:
Steven van den Broek 2020-09-13 11:02:51 +02:00
parent afd48ae65b
commit 6c0112b55c
7 changed files with 40 additions and 23 deletions

View File

@ -62,8 +62,8 @@ main = do
opts :: Parser Opts
opts = Opts
<$> optional (hsubparser
( command "run" (info (Run <$> runOpts) ( progDesc "Run hascard directly on a file"))
<> command "import" (info (Import <$> importOpts) (progDesc "Convert a delimited file to syntax compatible with hascard"))))
( command "run" (info (Run <$> runOpts) ( progDesc "Run hascard with CLI options"))
<> command "import" (info (Import <$> importOpts) (progDesc "Convert a TAB delimited file to syntax compatible with hascard. So, terms and definitions should be seperated by tabs, and rows by new lines. When converting to 'open' cards, multiple correct answers can be seperated by semicolons (;), backslashes (/) or commas (,)."))))
<*> switch (long "version" <> short 'v' <> help "Show version number")
runOpts :: Parser RunOpts
@ -76,14 +76,14 @@ runOpts = RunOpts
importOpts :: Parser ImportOpts
importOpts = ImportOpts
<$> argument str (metavar "INPUT" <> help "A file ...")
<$> argument str (metavar "INPUT" <> help "A TSV file")
<*> argument str (metavar "DESINATION" <> help "The filename/path to which the output should be saved")
<*> option auto (long "type" <> short 't' <> metavar "'open' or 'def'" <> help "The type of card to which the input is transformed, default: open" <> value Open)
<*> switch (long "reverse" <> short 'r' <> help "Reverse direction of question and answer, i.e. right part becomes the question.")
optsWithHelp :: ParserInfo Opts
optsWithHelp = info (opts <**> helper) $
fullDesc <> progDesc "Run the normal application without argument, or run it directly on a deck of flashcards by providing a text file. Options work either way."
fullDesc <> progDesc "Run the normal application with `hascard`. To run directly on a file, and with CLI options, see `hascard run --help`. For converting TAB seperated files, see `hascard import --help`."
<> header "Hascard - a TUI for reviewing notes"
nothingIf :: (a -> Bool) -> a -> Maybe a

View File

@ -15,15 +15,16 @@ import qualified Data.Text as T
import qualified Graphics.Vty as V
mkParameterForm :: Int -> Parameters -> Form Parameters e Name
mkParameterForm n =
mkParameterForm n ps =
let label s w = padBottom (Pad 1) $ padRight (Pad 2) (strWrap s) <+> w
in newForm
[ label "Shuffle the deck?" @@= yesnoField True pShuffle ShuffleField ""
, label "Number of cards:" @@= naturalNumberField n (subsetLens n) SubsetField ("/" <> pack (show n))
, label "Select chunk:" @@= chunkField n pChunk ChunkField
, label "Review mode?" @@= yesnoField True pReviewMode ReviewModeField ""
, hCenter @@= okField pOk ParametersOkField "Ok"
]
form = newForm
[ label "Select chunk:" @@= chunkField n pChunk ChunkField
, label "Number of cards:" @@= naturalNumberField n (subsetLens n) SubsetField ("/" <> pack (show n))
, label "Shuffle the deck?" @@= yesnoField True pShuffle ShuffleField ""
, label "Review mode?" @@= yesnoField True pReviewMode ReviewModeField ""
, hCenter @@= okField pOk ParametersOkField "Ok"
] ps
in setFormFocus ParametersOkField form
subsetLens :: Int -> Lens' Parameters Int
subsetLens n f ps =

View File

@ -57,6 +57,14 @@ goToState gs s = gs & states %~ M.insert (getMode s) s
moveToState :: GlobalState -> State -> GlobalState
moveToState gs = goToState (popState gs)
-- popState until at mode of state s.
removeToState :: GlobalState -> State -> GlobalState
removeToState gs s = go (popState gs)
where go global =
let current = Stack.head (global ^. stack)
in if current == getMode s then moveToState global s
else go (popState global)
popState :: GlobalState -> GlobalState
popState gs = let
s = gs ^. stack
@ -75,17 +83,26 @@ goToModeOrQuit gs mode =
maybe (halt gs) (continue . goToState gs) $ M.lookup mode (gs ^. states)
moveToModeOrQuit :: GlobalState -> Mode -> EventM n (Next GlobalState)
moveToModeOrQuit gs mode =
maybe (halt gs) (continue . moveToState gs) $ M.lookup mode (gs ^. states)
moveToModeOrQuit = moveToModeOrQuit' return
moveToModeOrQuit' :: (State -> IO State) -> GlobalState -> Mode -> EventM n (Next GlobalState)
moveToModeOrQuit' f gs mode =
maybe (halt gs) (\s -> continue . moveToState gs =<< liftIO (f s)) $ M.lookup mode (gs ^. states)
removeToModeOrQuit :: GlobalState -> Mode -> EventM n (Next GlobalState)
removeToModeOrQuit = removeToModeOrQuit' return
removeToModeOrQuit' :: (State -> IO State) -> GlobalState -> Mode -> EventM n (Next GlobalState)
removeToModeOrQuit' f gs mode =
maybe (halt gs) (\s -> continue . removeToState gs =<< liftIO (f s)) $ M.lookup mode (gs ^. states)
refreshRecents :: CSS -> IO CSS
refreshRecents s = do
rs <- getRecents
let prettyRecents = shortenFilepaths (toList rs)
options = Vec.fromList (prettyRecents ++ ["Select file from system"])
return $ s & recents .~ rs
& list .~ L.list Ordinary options 1
& list .~ L.list Ordinary options 1
refreshRecents' :: GlobalState -> IO GlobalState
refreshRecents' gs = maybe (return gs) ((updateCSS gs <$>) . refreshRecents) ((\(CardSelectorState s) -> s) <$> M.lookup CardSelector (gs^.states))

View File

@ -236,14 +236,14 @@ drawReorder s = case (s ^. cardState, s ^. currentCard) of
---------------------- Events ----------------------
----------------------------------------------------
halt' :: GlobalState -> EventM n (Next GlobalState)
halt' = flip (moveToModeOrQuit' (\(CardSelectorState s) -> CardSelectorState <$> refreshRecents s)) CardSelector
halt' = flip (removeToModeOrQuit' (\(CardSelectorState s) -> CardSelectorState <$> refreshRecents s)) CardSelector
handleEvent :: GlobalState -> CS -> BrickEvent Name Event -> EventM Name (Next GlobalState)
handleEvent gs s (VtyEvent e) =
let update = updateCS gs
continue' = continue . update in
case e of
V.EvKey V.KEsc [] -> halt' gs
V.EvKey V.KEsc [] -> continue $ popState gs
V.EvKey V.KRight [V.MCtrl] -> if not (s^.reviewMode) then next gs s else continue gs
V.EvKey V.KLeft [V.MCtrl] -> if not (s^.reviewMode) then previous gs s else continue gs

View File

@ -75,11 +75,10 @@ handleEvent gs s@FBS{_fb=b, _exception'=excep} (VtyEvent ev) =
Left exc -> continue' (s' & exception' ?~ displayException exc)
Right file -> case parseCards file of
Left parseError -> continue' (s & exception' ?~ parseError)
-- Right result -> halt' (s' & parsedCards .~ result & filePath ?~ fp)
Right result -> continue =<< liftIO (do
addRecent fp
let gs' = update s'
return (gs' `moveToState` parameterState (gs'^.parameters) fp result))
gs' <- refreshRecents' gs
return (gs' `goToState` parameterState (gs'^.parameters) fp result))
_ -> halt' gs
_ -> continue' s'

View File

@ -67,4 +67,4 @@ info = unlines
, ""
, " * Use F1 to show the answers of a open question"
, ""
, " * Use CTRL+Left and CTRL+Right to move to previous and next cards without having to answer them"]
, " * Use CTRL+Left and CTRL+Right to move to previous and next cards without having to answer them; this is disabled in review mode"]

View File

@ -44,7 +44,7 @@ handleEvent gs s ev@(VtyEvent e) =
(Just n) = focusGetCurrent focus
down = if n == ParametersOkField then continue gs
else continue'' $ form { formFocus = focusNext focus }
up = if n == ShuffleField then continue gs
up = if n == ChunkField then continue gs
else continue'' $ form { formFocus = focusPrev focus }
in case e of
@ -58,7 +58,7 @@ handleEvent gs s ev@(VtyEvent e) =
V.EvKey V.KBackTab [] -> continue gs
_ -> do f <- handleFormEvent ev form
if formState f ^. pOk
then continue =<< (gs `moveToState`)
then continue =<< (gs `goToState`)
<$> liftIO (cardsWithOptionsState
(gs & parameters .~ formState f)
(s ^. psFp)