{-# LANGUAGE NamedFieldPuns #-}
module FortyTwo.Prompts.Multiselect (multiselect, multiselectWithDefault) where
import Control.Monad.IO.Class
import System.Console.ANSI (hideCursor, showCursor)
import FortyTwo.Renderers.Multiselect (renderOptions)
import FortyTwo.Renderers.Question (renderQuestion)
import FortyTwo.Types(Options)
import FortyTwo.Utils
import FortyTwo.Constants
loop :: Options -> IO [Int]
loop :: Options -> IO [Int]
loop Options
options = do
forall (m :: * -> *). MonadIO m => m ()
noEcho
forall (m :: * -> *). MonadIO m => Options -> m ()
renderOptions Options
options
String
key <- IO String
getKey
forall (m :: * -> *). MonadIO m => Int -> m ()
clearLines forall a b. (a -> b) -> a -> b
$ Options -> Int
getOptionsLines Options
options
[Int]
res <- Options -> String -> IO [Int]
handleEvent Options
options String
key
forall (m :: * -> *). MonadIO m => m ()
restoreEcho
forall (m :: * -> *) a. Monad m => a -> m a
return [Int]
res
handleEvent :: Options -> String -> IO [Int]
handleEvent :: Options -> String -> IO [Int]
handleEvent Options
options String
key
| String
key forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String
upKey, String
leftKey] = Options -> IO [Int]
loop forall a b. (a -> b) -> a -> b
$ Options -> (Int, Int, Maybe Int) -> Options
moveUp Options
options forall a b. (a -> b) -> a -> b
$ Options -> (Int, Int, Maybe Int)
getOptionsMeta Options
options
| String
key forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String
downKey, String
rightKey] = Options -> IO [Int]
loop forall a b. (a -> b) -> a -> b
$ Options -> (Int, Int, Maybe Int) -> Options
moveDown Options
options forall a b. (a -> b) -> a -> b
$ Options -> (Int, Int, Maybe Int)
getOptionsMeta Options
options
| String
key forall a. Eq a => a -> a -> Bool
== String
spaceKey = Options -> IO [Int]
loop forall a b. (a -> b) -> a -> b
$ Options -> (Int, Int, Maybe Int) -> Options
toggle Options
options forall a b. (a -> b) -> a -> b
$ Options -> (Int, Int, Maybe Int)
getOptionsMeta Options
options
| String
key forall a. Eq a => a -> a -> Bool
== String
enterKey = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Options -> [Int]
getSelecteOptionsIndexes Options
options
| Bool
otherwise = Options -> IO [Int]
loop Options
options
toggle :: Options -> (Int, Int, Maybe Int) -> Options
toggle :: Options -> (Int, Int, Maybe Int) -> Options
toggle Options
options (Int
_, Int
_, Maybe Int
focusedIndex) = case Maybe Int
focusedIndex of
Just Int
x -> Int -> Options -> Options
toggleFocusedOption Int
x Options
options
Maybe Int
Nothing -> Options
options
moveUp :: Options -> (Int, Int, Maybe Int) -> Options
moveUp :: Options -> (Int, Int, Maybe Int) -> Options
moveUp Options
options (Int
minVal, Int
maxVal, Maybe Int
focusedIndex) = case Maybe Int
focusedIndex of
Just Int
x -> if Int
x forall a. Eq a => a -> a -> Bool
== Int
minVal then
Int -> Options -> Options
focusOption Int
x Options
options
else
Int -> Options -> Options
focusOption (Int
x forall a. Num a => a -> a -> a
- Int
1) Options
options
Maybe Int
Nothing -> Int -> Options -> Options
focusOption Int
maxVal Options
options
moveDown :: Options -> (Int, Int, Maybe Int) -> Options
moveDown :: Options -> (Int, Int, Maybe Int) -> Options
moveDown Options
options (Int
minVal, Int
maxVal, Maybe Int
focusedIndex) = case Maybe Int
focusedIndex of
Just Int
x -> if Int
x forall a. Eq a => a -> a -> Bool
== Int
maxVal then
Int -> Options -> Options
focusOption Int
x Options
options
else
Int -> Options -> Options
focusOption (Int
x forall a. Num a => a -> a -> a
+ Int
1) Options
options
Maybe Int
Nothing -> Int -> Options -> Options
focusOption Int
minVal Options
options
multiselectWithDefault :: MonadIO m => String -> [String] -> [String] -> m [String]
multiselectWithDefault :: forall (m :: * -> *).
MonadIO m =>
String -> [String] -> [String] -> m [String]
multiselectWithDefault String
question [String]
options [String]
defaultAnswer = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
String -> IO ()
putStrLn String
emptyString
forall (m :: * -> *).
MonadIO m =>
String -> String -> String -> m ()
renderQuestion String
question ([String] -> String
toCommaSeparatedString [String]
defaultAnswer) String
emptyString
String -> IO ()
putStrLn String
emptyString
IO ()
hideCursor
IO ()
flush
forall (m :: * -> *). MonadIO m => m ()
noBuffering
[Int]
res <- Options -> IO [Int]
loop forall a b. (a -> b) -> a -> b
$ [String] -> Options
stringsToOptions [String]
options
forall (m :: * -> *). MonadIO m => m ()
restoreBuffering
IO ()
showCursor
forall (m :: * -> *). MonadIO m => Int -> m ()
clearLines Int
1
if forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Int]
res then do
forall (m :: * -> *).
MonadIO m =>
String -> String -> String -> m ()
renderQuestion String
question String
emptyString ([String] -> String
toCommaSeparatedString [String]
defaultAnswer)
forall (m :: * -> *) a. Monad m => a -> m a
return [String]
defaultAnswer
else do
let answer :: [String]
answer = forall a. Eq a => (Int -> a -> Bool) -> [a] -> [a]
filter' (\ Int
i String
_ -> forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem Int
i [Int]
res) [String]
options
forall (m :: * -> *).
MonadIO m =>
String -> String -> String -> m ()
renderQuestion String
question String
emptyString ([String] -> String
toCommaSeparatedString [String]
answer)
forall (m :: * -> *) a. Monad m => a -> m a
return [String]
answer
multiselect :: MonadIO m => String -> [String] -> m [String]
multiselect :: forall (m :: * -> *). MonadIO m => String -> [String] -> m [String]
multiselect String
question [String]
options = forall (m :: * -> *).
MonadIO m =>
String -> [String] -> [String] -> m [String]
multiselectWithDefault String
question [String]
options []