improved terminal detection

This commit is contained in:
La Ancapo 2026-03-27 22:53:39 +01:00
parent d70a3c9381
commit 0e20c20e33

View file

@ -36,6 +36,8 @@ encrypt = do
B.writeFile path =<< encryptData encryptedFile B.writeFile path =<< encryptData encryptedFile
sign = do sign = do
-- need to fetch it now since stdin is closed after getContents
isTerm <- hIsTerminalDevice stdin
transactionB64 <- B.getContents transactionB64 <- B.getContents
let transaction = case B64.decode $ BC8.strip transactionB64 of let transaction = case B64.decode $ BC8.strip transactionB64 of
Left err -> error err Left err -> error err
@ -44,7 +46,7 @@ sign = do
Left err -> error err Left err -> error err
Right x -> x Right x -> x
T.hPutStrLn stderr $ pretty parsedTransaction T.hPutStrLn stderr $ pretty parsedTransaction
proceed <- confirm proceed <- confirm isTerm
when proceed $ do when proceed $ do
key <- getPrivateKey key <- getPrivateKey
let sd = S.signWithSecret key parsedTransaction let sd = S.signWithSecret key parsedTransaction
@ -62,9 +64,8 @@ getPrivateKey = do
0x45 -> T.decodeUtf8 <$> decryptData (B.tail file) 0x45 -> T.decodeUtf8 <$> decryptData (B.tail file)
_ -> error "garbled private key file" _ -> error "garbled private key file"
confirm :: IO Bool confirm :: Bool -> IO Bool
confirm = do confirm isTerm = do
isTerm <- hIsTerminalDevice stdout
if isTerm then do if isTerm then do
tty <- openFile "/dev/tty" ReadWriteMode tty <- openFile "/dev/tty" ReadWriteMode
hPutStr tty "Sign this transaction? [y/N]: " hPutStr tty "Sign this transaction? [y/N]: "