@@ -17,17 +17,15 @@ import qualified Cardano.CLI.Commands.Key as Cmd
1717import Cardano.CLI.Types.Common (KeyOutputFormat (.. ), SigningKeyFile )
1818import Cardano.CLI.Types.Errors.KeyCmdError
1919 (KeyCmdError (KeyCmdMnemonicError , KeyCmdReadMnemonicFileError , KeyCmdWriteFileError , KeyCmdWrongNumOfMnemonics ))
20- import Cardano.Prelude (isSpace )
20+ import Cardano.Prelude (catch , stdout )
2121
2222import Control.Monad (when )
2323import Data.Bifunctor (Bifunctor (.. ))
2424import Data.Text (Text )
2525import qualified Data.Text as T
2626import Data.Word (Word32 )
27- import System.Console.Haskeline (Completion , InputT , Settings (.. ), completeWord' ,
28- defaultBehavior , defaultPrefs , getInputLineWithInitial ,
29- runInputTBehaviorWithPrefs , simpleCompletion )
30- import System.Console.Haskeline.Completion (CompletionFunc )
27+ import System.IO.Error (isEOFError )
28+ import Cardano.CLI.OS.Posix (hFlush )
3129
3230-- | Generate a mnemonic and write it to a file or stdout.
3331generateMnemonic
@@ -143,43 +141,36 @@ extendedSigningKeyFromMnemonicImpl keyOutputFormat derivedExtendedSigningKeyType
143141 , " - To terminate, press enter on an empty line."
144142 , " - To abort you can press CTRL+C."
145143 , " "
146- , " (If your terminal supports it, you can use the TAB key for word completion.)"
147- , " "
148144 ]
149- runInputTBehaviorWithPrefs defaultBehavior defaultPrefs settings (inputT ( " " , " " ) [] )
145+ inputMnemonic []
150146 where
151- settings :: Monad m => Settings m
152- settings =
153- Settings
154- { complete = completionFunc
155- , historyFile = Nothing
156- , autoAddHistory = False
157- }
158-
159- completionFunc :: Monad m => CompletionFunc m
160- completionFunc = completeWord' Nothing isSpace completeMnemonicWord
161-
162- completeMnemonicWord :: Monad m => String -> m [Completion ]
163- completeMnemonicWord prefix = return $ map (simpleCompletion . T. unpack . fst ) $ findMnemonicWordsWithPrefix (T. pack prefix)
164-
165- inputT :: (String , String ) -> [Text ] -> InputT IO [Text ]
166- inputT prefill mnemonic = do
167- minput <- getInputLineWithInitial (show (length mnemonic + 1 ) <> " . " ) prefill
147+ inputMnemonic :: [Text ] -> IO [Text ]
148+ inputMnemonic mnemonic = do
149+ minput <- getLineWithPrompt (show (length mnemonic + 1 ) <> " . " )
168150 case minput of
169151 Nothing -> return $ reverse mnemonic
170152 Just " " -> return $ reverse mnemonic
171153 Just input ->
172154 let newWords = map (T. toLower . T. pack) $ filter (not . null ) $ words input
173155 in case span isValidMnemonicWord newWords of
174- (allWords, [] ) -> inputT ( " " , " " ) (reverse allWords ++ mnemonic)
175- (validWords, invalidWord : notValidatedWords ) -> do
156+ (allWords, [] ) -> inputMnemonic (reverse allWords ++ mnemonic)
157+ (validWords, invalidWord : _notValidatedWords ) -> do
176158 liftIO $ putStrLn $ " The word \" " <> T. unpack invalidWord <> " \" is not in the memonic dictionary"
177- let textBeforeCursor = unwords (map T. unpack validWords <> [T. unpack invalidWord])
178- textAfterCursor =
179- if null notValidatedWords
180- then " "
181- else ' ' : unwords (map T. unpack notValidatedWords)
182- inputT (textBeforeCursor, textAfterCursor) mnemonic
159+ inputMnemonic (reverse validWords ++ mnemonic)
183160
184161 isValidMnemonicWord :: Text -> Bool
185162 isValidMnemonicWord word = word `elem` map fst (findMnemonicWordsWithPrefix word)
163+
164+ getLineWithPrompt :: String -> IO (Maybe String )
165+ getLineWithPrompt prompt = do
166+ catch
167+ ( do
168+ putStr prompt
169+ hFlush stdout
170+ Just <$> getLine
171+ )
172+ handleEOF
173+
174+ handleEOF :: IOError -> IO (Maybe String )
175+ handleEOF eofERRor | isEOFError eofERRor = return Nothing
176+ handleEOF otherError = ioError otherError
0 commit comments