packages feed

fortytwo 1.0.2 → 1.0.3

raw patch · 13 files changed

+51/−26 lines, 13 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ FortyTwo.Utils: addBreakingLinesSpacing :: String -> String -> String
+ FortyTwo.Utils: getOptionsLines :: Options -> Int

Files

demo/InputExample.hs view
@@ -4,5 +4,5 @@  main :: IO String main = do-  input "What's your name?"+  input "What's \n your name?"   inputWithDefault "How old are you?" "22"
demo/Main.hs view
@@ -13,4 +13,5 @@   MultiselectExample.main   SelectExample.main   PasswordExample.main+  putStrLn "All done"   return ()
demo/MultiselectExample.hs view
@@ -6,6 +6,6 @@ main = do   multiselect "Which kind of sports do you like?" ["Soccer", "Tennis", "Golf"]   multiselectWithDefault-    "What are your favourite books?"-    ["1984", "Moby Dick", "The Hitchhiker's Guide to the Galaxy"]+    "What are your \nfavourite books?"+    ["1984", "Multi\nline\nentry", "Moby Dick", "The Hitchhiker's Guide\n to the Galaxy"]     ["1984", "The Hitchhiker's Guide to the Galaxy"]
demo/PasswordExample.hs view
@@ -3,4 +3,4 @@ import FortyTwo (password)  main :: IO String-main = password "What's your secret password?"+main = password "What's your \nsecret password?"
demo/SelectExample.hs view
@@ -5,4 +5,4 @@ main :: IO String main = do   select "What's your favourite color?" ["Red", "Yellow", "Blue"]-  selectWithDefault "What's your favourite film?" ["28 Days Later", "Blade Runner", "Matrix"] "28 Days Later"+  selectWithDefault "What's your favourite film?" ["28 Days Later", "Multi\nline\nentry", "Blade Runner", "Matrix", "Multi\nline\ntext"] "28 Days Later"
fortytwo.cabal view
@@ -1,5 +1,5 @@ name:                fortytwo-version:1.0.2+version:1.0.3 synopsis:            Interactive terminal prompt description:         List of Prompt helpers to pimp the UIs of your haskell programs homepage:            https://github.com/gianlucaguarini/fortytwo#readme
src/FortyTwo/Prompts/Multiselect.hs view
@@ -5,7 +5,7 @@ import System.Console.ANSI (hideCursor, showCursor) import FortyTwo.Renderers.Multiselect (renderOptions) import FortyTwo.Renderers.Question (renderQuestion)-import FortyTwo.Types(Option(..), Options)+import FortyTwo.Types(Options) import FortyTwo.Utils import FortyTwo.Constants @@ -15,7 +15,7 @@   noEcho   renderOptions options   key <- getKey-  clearLines $ length options+  clearLines $ getOptionsLines options   res <- handleEvent options key   restoreEcho   return res@@ -31,7 +31,7 @@  -- | Toggle the isSelected value of a single option element toggle :: Options -> (Int, Int, Maybe Int) -> Options-toggle options (minVal, maxVal, focusedIndex) = case focusedIndex of+toggle options (_, _, focusedIndex) = case focusedIndex of   Just x -> toggleFocusedOption x options   Nothing -> options @@ -72,7 +72,7 @@     renderQuestion question emptyString (toCommaSeparatedString defaultAnswer)     return defaultAnswer   else do-    let answer = filter' (\ i o -> elem i res) options+    let answer = filter' (\ i _ -> elem i res) options     renderQuestion question emptyString (toCommaSeparatedString answer)     return answer 
src/FortyTwo/Prompts/Select.hs view
@@ -5,7 +5,7 @@ import System.Console.ANSI (hideCursor, showCursor) import FortyTwo.Renderers.Select (renderOptions) import FortyTwo.Renderers.Question (renderQuestion)-import FortyTwo.Types(Option(..), Options)+import FortyTwo.Types(Options) import FortyTwo.Utils import FortyTwo.Constants @@ -15,7 +15,7 @@   noEcho   renderOptions options   key <- getKey-  clearLines $ length options+  clearLines $ getOptionsLines options   res <- handleEvent options key   restoreEcho   return res
src/FortyTwo/Renderers/Multiselect.hs view
@@ -3,8 +3,8 @@ module FortyTwo.Renderers.Multiselect (renderOptions, renderOption) where  import FortyTwo.Types (Option(..), Options)+import FortyTwo.Utils (addBreakingLinesSpacing) import System.Console.ANSI-import Control.Monad (when) import FortyTwo.Constants  -- | Render all the options collection@@ -25,5 +25,5 @@     putStr [selectedIcon]     setSGR [Reset]   else putStr [unselectedIcon]-  putStr $ " " ++ value+  putStr $ " " ++ addBreakingLinesSpacing "    " value   putStrLn emptyString
src/FortyTwo/Renderers/Password.hs view
@@ -8,4 +8,4 @@  -- | Hide any string replacing its letters with the passwordHiddenChar hideLetters :: String -> String-hideLetters letters = [passwordHiddenChar | x <- letters]+hideLetters letters = [passwordHiddenChar | _ <- letters]
src/FortyTwo/Renderers/Question.hs view
@@ -11,16 +11,20 @@       putStr "? "       setSGR [Reset]       setSGR [SetConsoleIntensity BoldIntensity]-      putStr message+      putStr text       setSGR [Reset]   | messageType == Answer = do       setSGR [SetColor Foreground Dull Cyan]-      putStr $ " " ++ message+      putStr $ " " ++ text       setSGR [Reset]   | messageType == DefaultAnswer = do     setSGR [SetConsoleIntensity FaintIntensity]-    putStr $ " (" ++ message ++ ")"+    putStr $ " (" ++ text ++ ")"     setSGR [Reset]+  | otherwise =+      putStr text+    where+      text = (unwords . lines) message  -- | Print the question message renderQuestion :: String -> String -> String -> IO ()
src/FortyTwo/Renderers/Select.hs view
@@ -4,6 +4,7 @@  import FortyTwo.Types (Option(..), Options) import System.Console.ANSI+import FortyTwo.Utils (addBreakingLinesSpacing) import FortyTwo.Constants  -- | Render all the options collection@@ -12,10 +13,13 @@  -- | Render a single option items renderOption :: Option -> IO()-renderOption Option { isSelected, isFocused, value } =+renderOption Option { isFocused, value } =   if isFocused then do     setSGR [SetColor Foreground Dull Cyan]-    putStrLn $ unwords [[focusIcon], value]+    putStrLn $ unwords [[focusIcon], text]     setSGR [Reset]   else-    putStrLn $ "  " ++ value+    putStrLn $ separator ++ text+  where+    separator = "  "+    text = addBreakingLinesSpacing separator value
src/FortyTwo/Utils.hs view
@@ -1,11 +1,10 @@-{-# LANGUAGE NamedFieldPuns #-}+{-# LANGUAGE NamedFieldPuns, OverloadedStrings #-}  module FortyTwo.Utils where  import System.Console.ANSI (cursorUpLine, clearFromCursorToScreenEnd) import System.IO (hSetBuffering, hFlush, hSetEcho, hReady, stdin, stdout, BufferMode(..)) import Data.List (findIndex, findIndices, elemIndex, intercalate)-import Control.Monad (when) import Control.Applicative ((<$>)) import Data.Maybe (fromJust) import FortyTwo.Types(Option(..), Options)@@ -33,9 +32,9 @@  -- | Clear terminal lines from the current cursor position clearLines :: Int -> IO()-clearLines lines' = do-  -- move up of 1 line...-  cursorUpLine lines'+clearLines l = do+  -- move up of some lines...+  cursorUpLine l   -- and clear them   clearFromCursorToScreenEnd @@ -64,6 +63,10 @@ getOptionsMeta :: Options -> (Int, Int, Maybe Int) getOptionsMeta options = (0, length options - 1, getFocusedOptionIndex options) +-- | Get the amount of breaking lines needed to display all the options+getOptionsLines :: Options -> Int+getOptionsLines = sum . map (length . lines . getOptionValue)+ -- | Convert a string array to stringsToOptions :: [String] -> Options stringsToOptions options = [@@ -78,6 +81,19 @@     isSelected = getOptionIsSelected o,     isFocused = focusedIndex == i   }++-- | Normalise the select/multiselect multi lines adding the spaces to format them properly+addBreakingLinesSpacing :: String -> String -> String+addBreakingLinesSpacing separator value =+  if null multiLines then+    value+  else+    firstLine ++ "\n" ++ unlines (take (length normalisedLines - 1) normalisedLines) ++ last normalisedLines+  where+    values = lines value+    firstLine = head values+    multiLines = tail values+    normalisedLines = map (\text -> separator ++ text) multiLines  -- | Toggle the isSelected flag for a single option toggleFocusedOption :: Int -> Options -> Options