fortytwo 0.0.3 → 0.0.4
raw patch · 10 files changed
+39/−22 lines, 10 filesdep −terminfoPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependencies removed: terminfo
API changes (from Hackage documentation)
- FortyTwo.Utils: getKey :: IO [Char]
+ FortyTwo.Utils: getKey :: IO String
Files
- README.md +3/−3
- fortytwo.cabal +1/−2
- src/FortyTwo/Constants.hs +8/−0
- src/FortyTwo/Prompts/Confirm.hs +2/−2
- src/FortyTwo/Prompts/Multiselect.hs +2/−1
- src/FortyTwo/Renderers/Question.hs +3/−2
- src/FortyTwo/Types.hs +1/−0
- src/FortyTwo/Utils.hs +12/−4
- test/Specs/Utils.hs +6/−1
- test/doctests.hs +1/−7
README.md view
@@ -5,7 +5,7 @@ [![Build Status][travis-image]][travis-url] [![MIT License][license-image]][license-url] -<img src="fortytwo.jpg" width="100%" />+ ## Installation @@ -15,7 +15,7 @@ ## Demo -<img src="demo.gif" />+ ## API @@ -54,7 +54,7 @@ import FortyTwo (confirmWithDefault) main :: IO Bool-main = confirmWithDefault "What's your name?" "Undefined"+main = confirmWithDefault "Are you old enough to see this?" True ``` #### Password
fortytwo.cabal view
@@ -1,5 +1,5 @@ name: fortytwo-version:0.0.3+version:0.0.4 synopsis: Interactive terminal prompt description: List of Prompt helpers to pimp the UIs of your haskell programs homepage: https://github.com/gianlucaguarini/fortytwo#readme@@ -32,7 +32,6 @@ FortyTwo.Renderers.Multiselect build-depends: base >= 4.7 && < 5, text <= 1.2.2.2,- terminfo <= 0.4.1.0, ansi-terminal >= 0.6.0.0 && <= 0.7.1.1 default-language: Haskell2010
src/FortyTwo/Constants.hs view
@@ -1,26 +1,34 @@ module FortyTwo.Constants where +-- | Up arrow key identifier upKey :: String upKey = "\ESC[A" +-- | Down arrow key identifier downKey :: String downKey = "\ESC[B" +-- | Right arrow key identifier rightKey :: String rightKey = "\ESC[C" +-- | Left arrow key identifier leftKey :: String leftKey = "\ESC[D" +-- | Delete key identifier delKey :: String delKey = "\DEL" +-- | Enter key identifier enterKey :: String enterKey = "\n" +-- | Space key identifier spaceKey :: String spaceKey = " " +-- | Just an empty string to use here and there... emptyString :: String emptyString = ""
src/FortyTwo/Prompts/Confirm.hs view
@@ -8,6 +8,8 @@ import FortyTwo.Constants (emptyString) -- | Normalize a string transforming it to lowercase and trimming it and getting either n or y+-- >>> normalizeString "Yes"+-- "y" normalizeString :: String -> String normalizeString s = take 1 $ T.unpack $ T.strip . T.toLower $ T.pack s @@ -16,7 +18,6 @@ getCleanConfirm = do s <- getLine; return $ normalizeString s -- | Ask a confirm falling back to a default value if no answer will be provided--- confirmWithDefault "Do you like music?" True confirmWithDefault :: String -> Bool -> IO Bool confirmWithDefault question defaultAnswer = do putStrLn emptyString@@ -35,6 +36,5 @@ defaultAnswerHumanized = if defaultAnswer then "yes" else "no" -- | Ask a confirm question by default it will be true--- confirm "Do you like music?" confirm :: String -> IO Bool confirm question = confirmWithDefault question False
src/FortyTwo/Prompts/Multiselect.hs view
@@ -9,7 +9,7 @@ import FortyTwo.Utils import FortyTwo.Constants --- | Loop to let the users select an single option+-- | Loop to let the users select multiple options loop :: Options -> IO [Int] loop options = do noEcho@@ -29,6 +29,7 @@ | key == enterKey = return $ getSelecteOptionsIndexes options | otherwise = loop options +-- | Toggle the isSelected value of a single option element toggle :: Options -> (Int, Int, Maybe Int) -> Options toggle options (minVal, maxVal, focusedIndex) = case focusedIndex of Just x -> toggleFocusedOption x options
src/FortyTwo/Renderers/Question.hs view
@@ -10,15 +10,16 @@ setSGR [SetColor Foreground Dull Green] putStr "? " setSGR [Reset]- setSGR [SetColor Foreground Dull White]+ setSGR [SetConsoleIntensity BoldIntensity] putStr message setSGR [Reset] | messageType == Answer = do+ setSGR [SetConsoleIntensity BoldIntensity] setSGR [SetColor Foreground Dull Cyan] putStr $ " " ++ message setSGR [Reset] | messageType == DefaultAnswer = do- setSGR [SetColor Foreground Vivid Yellow]+ setSGR [SetConsoleIntensity FaintIntensity] putStr $ " (" ++ message ++ ")" setSGR [Reset]
src/FortyTwo/Types.hs view
@@ -10,4 +10,5 @@ value :: String } deriving (Eq, Show) +-- | List of options type Options = [Option]
src/FortyTwo/Utils.hs view
@@ -10,22 +10,27 @@ import FortyTwo.Types(Option(..), Options) import FortyTwo.Constants (emptyString) +-- | Disable the stdin stdout output buffering noBuffering :: IO() noBuffering = do hSetBuffering stdin NoBuffering hSetBuffering stdout NoBuffering +-- | Enaable the stdin stdout buffering restoreBuffering :: IO() restoreBuffering = do hSetBuffering stdin LineBuffering hSetBuffering stdout LineBuffering +-- | Avoid echoing the user input noEcho :: IO () noEcho = hSetEcho stdin False +-- | Restore the user input echos restoreEcho :: IO () restoreEcho = hSetEcho stdin True +-- | Clear terminal lines from the current cursor position clearLines :: Int -> IO() clearLines lines' = do -- move up of 1 line...@@ -41,6 +46,8 @@ filter' :: Eq a => (Int -> a -> Bool) -> [a] -> [a] filter' f xs = [x | x <- xs, f (fromJust (elemIndex x xs)) x] +-- | Get the value of any keyboard press+getKey :: IO String getKey = reverse <$> getKey' emptyString where getKey' chars = do@@ -48,6 +55,10 @@ more <- hReady stdin (if more then getKey' else return) (char:chars) +-- | Flush the output buffer+flush :: IO()+flush = hFlush stdout+ -- | Get useful informations from the options collection, like minVal, maxVal, activeIndex getOptionsMeta :: Options -> (Int, Int, Maybe Int) getOptionsMeta options = (0, length options - 1, getFocusedOptionIndex options)@@ -66,6 +77,7 @@ isSelected = getOptionIsSelected o, isFocused = focusedIndex == i }+ -- | Toggle the isSelected flag for a single option toggleFocusedOption :: Int -> Options -> Options toggleFocusedOption focusedIndex = map' $ \ i o ->@@ -80,10 +92,6 @@ -- | Print a list to comma separated toCommaSeparatedString :: [String] -> String toCommaSeparatedString = intercalate ", "---- | Flush the output buffer-flush :: IO()-flush = hFlush stdout -- | Get the value of any option getOptionValue :: Option -> String
test/Specs/Utils.hs view
@@ -13,10 +13,15 @@ let options = stringsToOptions list getOptionValue ((!!) options 0) `shouldBe` (!!) list 0 - it "Can update properly an options collection with a defined index" $ do+ it "Can focus properly an option in a collection via index" $ do let selectedIndex = 1 let options = focusOption selectedIndex $ stringsToOptions list fromJust (getFocusedOptionIndex options) `shouldBe` selectedIndex it "Return 0 if no option will be selected" $ fromMaybe 0 (getFocusedOptionIndex $ stringsToOptions list) `shouldBe` 0++ it "Can activate properly an option in a collection via index" $ do+ let selectedIndex = 1+ let options = toggleFocusedOption selectedIndex $ stringsToOptions list+ getSelecteOptionsIndexes options `shouldBe` [selectedIndex]
test/doctests.hs view
@@ -3,10 +3,4 @@ import Test.DocTest main :: IO ()-main = doctest [- "src/FortyTwo.hs",- "src/FortyTwo/TheAnswerToEverything.hs",- "src/FortyTwo/Types.hs",- "src/FortyTwo/Prompts/Input.hs",- "src/FortyTwo/Renderers/Question.hs"- ]+main = doctest ["src"]