update functions

This commit is contained in:
千住柱間 2025-08-26 21:16:02 -04:00
commit d4cca7b703
Signed by: hashirama
GPG key ID: 53E62470A86BC185
2 changed files with 120 additions and 179 deletions

View file

@ -1,52 +1,21 @@
{-# LANGUAGE OverloadedStrings #-}
module Main where
import MDict
import System.Environment (getArgs)
import System.IO (hSetBuffering, stdout, BufferMode(..))
import Control.Exception (try, SomeException)
import Control.Monad (unless)
import System.Exit (exitSuccess)
import qualified Data.Text.IO as TIO
import qualified MDict as M
-- Highest Priority: Safe dictionary initialization with error handling
loadDictionary :: FilePath -> IO (Either String MDict)
loadDictionary path = do
result <- try $ withMDict path (return . Right)
case result of
Left (e :: SomeException) -> return $ Left $ "Error loading dictionary: " ++ show e
Right dict -> return dict
-- Medium Priority: Interactive lookup loop with graceful exit
lookupLoop :: MDict -> IO ()
lookupLoop dict = do
putStr "Enter word to lookup (or :quit to exit): "
hSetBuffering stdout NoBuffering
input <- getLine
unless (input == ":quit") $ do
result <- lookupWord dict input
case result of
Just definition -> putStrLn $ "\nDefinition:\n" ++ definition ++ "\n"
Nothing -> putStrLn $ "\n'" ++ input ++ "' not found in dictionary.\n"
lookupLoop dict
putStrLn "Exiting..."
exitSuccess
-- Critical: Command-line argument validation
main :: IO ()
main = do
hSetBuffering stdout NoBuffering
args <- getArgs
case args of
[mdxPath] -> do
putStrLn $ "Loading dictionary: " ++ mdxPath
eitherDict <- loadDictionary mdxPath
case eitherDict of
Left err -> putStrLn err
Right dict -> do
putStrLn "Dictionary loaded successfully!\n"
lookupLoop dict
_ -> putStrLn $ unlines
[ "Invalid arguments!"
, "Usage: mdict-lookup <path-to-mdict.mdx>"
, "Example: mdict-lookup /dictionaries/cantonese.mdx"
]
[dictPath, word] ->
M.withMDict dictPath $ \dict -> do
result <- M.lookupWord dict word
case result of
Left err -> putStrLn $ "Error: " ++ err
Right txt -> do
putStrLn "Definition:"
TIO.putStrLn txt
_ -> putStrLn "Usage: my_program <dictionary.mdx> <word>"