ConsoleUtils.hs revision e5b79e9fe9606fd386dc840ea9f1514e7b9b32b9
{-
Module : $Header$
Copyright : (c) Till Mossakowski and Klaus L�ttich, Uni Bremen 2002-2005
Licence : similar to LGPL, see HetCATS/LICENCE.txt or LIZENZ.txt
Maintainer : hets@tzi.de
Stability : provisional
Portability : non-portable (imports Logic)
Utilities on top of HTk
-}
module GUI.ConsoleUtils where
import Data.Char
import Data.List
-- | present a list of choices and return the selection
listBox :: String -> [String] -> IO (Maybe Int)
listBox prompt choices = do
putStrLn prompt
mapM_ putStrLn $ zipWith (\ n c -> shows n ": " ++ c) [0::Int ..] choices
putStrLn "Please enter a number on the next line"
s <- getLine
if all isDigit s then
let n = foldl ( \ r a -> r * 10 + digitToInt a) 0 s in
if n >= length choices then
putStrLn "number to large, try again"
>> listBox prompt choices
else putStrLn ("you have chosen entry \"" ++ choices!!n)
>> return (Just n)
else putStrLn ("illegal input \"" ++ s ++ "\" try again")
>> listBox prompt choices
-- | show text and ask to save it
createTextSaveDisplay :: String -- ^ title of the window
-> String -- ^ default filename for saving the text
-> String -- ^ text to be displayed
-> IO()
createTextSaveDisplay t f txt = do
putStrLn t
putStrLn $ replicate (length t) '='
putStrLn ""
putStrLn txt
putStrLn ""
askFileNameAndSave f txt
-- | ask for a file name
askFileNameAndSave :: String -- ^ default filename for saving the text
-> String -- ^ text to be saved
-> IO ()
askFileNameAndSave f txt = do
putStrLn $ "save text in file \"" ++ f ++ "\"? (y/n)"
s <- getLine
if "y" `isPrefixOf` map toLower s then writeFile f txt
else do
putStrLn "enter new file name (or abort with return):"
n <- getLine
if null n then return () else askFileNameAndSave n txt