@@ -1,31 +1,82 @@
1module Main where
1module Main where
3+import Control.Monad (when)
4+import Data.Maybe (fromMaybe)
5+import System.Environment (getArgs, getProgName)
3import System.Exit (exitFailure)
6import System.Exit (exitFailure)
7+import System.Console.GetOpt
9+import Graphics.Vty.Config (terminalWidthTablePath)
5import Graphics.Vty.UnicodeWidthTable.Types
10import Graphics.Vty.UnicodeWidthTable.Types
6import Graphics.Vty.UnicodeWidthTable.IO
11import Graphics.Vty.UnicodeWidthTable.IO
7import Graphics.Vty.UnicodeWidthTable.Query
12import Graphics.Vty.UnicodeWidthTable.Query
18+options :: Config -> [OptDescr Arg]
20+ [ Option "h" ["help"] (NoArg Help)
22+ , Option "p" ["path"] (ReqArg OutputPath "PATH")
23+ ("The output path to write to (default: " <> fromMaybe "<none>" (configOutputPath config) <> ")")
27+ Config { configOutputPath :: Maybe FilePath
31+mkDefaultConfig :: IO Config
33+ Config <$> terminalWidthTablePath
37+ config <- mkDefaultConfig
39+ putStrLn $ "Usage: " <> pn <> " [options]"
41+ putStrLn "This tool queries the terminal on stdout to determine the widths"
42+ putStrLn "of Unicode characters rendered to the terminal. The resulting data"
43+ putStrLn "is written to a table at the specified output path for later"
44+ putStrLn "loading by Vty-based applications."
47+ putStrLn $ usageInfo pn (options config)
49+configFromArgs :: [Arg] -> Config -> Config
52+configFromArgs (Help:as) c =
54+configFromArgs (OutputPath p:as) c =
55+ configFromArgs as (c { configOutputPath = Just p })
9main :: IO ()
57main :: IO ()
11− let r1 = WidthTableRange 0 100 1
59+ defConfig <- mkDefaultConfig
12− r2 = WidthTableRange 101 200 2
60+ strArgs <- getArgs
13− t = UnicodeWidthTable { unicodeWidthTableRanges = [r1, r2]
61+ let (args, unused, errors) = getOpt Permute (options defConfig) strArgs
63+ when (not $ null errors) $ do
64+ mapM_ putStrLn errors
67+ when ((not $ null unused) || (Help `elem` args)) $ do
16− writeUnicodeWidthTable "testfile.dat" t
71+ let config = configFromArgs args defConfig
17− result <- readUnicodeWidthTable "testfile.dat"
19− case result of
73+ outputPath <- case configOutputPath config of
20− Left msg -> do
74+ Nothing -> do
21− putStrLn $ "Error reading table: " <> msg
75+ putStrLn "Error: could not obtain terminal width table path"
22 exitFailure
76 exitFailure
23− Right t2 -> do
77+ Just path -> return path
24− putStrLn "Original table:"
26− putStrLn "New table:"
79+ putStrLn "Querying terminal:"
29 builtTable <- buildUnicodeWidthTable
80 builtTable <- buildUnicodeWidthTable
30− print builtTable
81+ writeUnicodeWidthTable outputPath builtTable
31− writeUnicodeWidthTable "terminal_widths.dat" builtTable
82+ putStrLn $ "\nOutput table written to " <> outputPath