@@ -20,21 +20,29 @@ import qualified Text.Read as Read
20
20
21
21
22
22
usage :: String
23
- usage = " fmt-signature width"
23
+ usage = " fmt-signature indent-spaces width"
24
24
25
25
main :: IO ()
26
26
main = do
27
27
args <- System.Environment. getArgs
28
28
case args of
29
- [width] | Just width <- Read. readMaybe width ->
30
- interact $ \ s -> maybe s id (fmt width s)
29
+ [indent, width]
30
+ | Just indent <- Read. readMaybe indent
31
+ , Just width <- Read. readMaybe width ->
32
+ let config = Config
33
+ { _indent = indent
34
+ , _width = width
35
+ }
36
+ in interact $ \ s -> maybe s id (fmt config s)
31
37
_ -> IO. hPutStrLn IO. stderr usage >> System.Exit. exitFailure
32
38
33
- indentSpaces :: Int
34
- indentSpaces = 4
39
+ data Config = Config {
40
+ _indent :: ! Int
41
+ , _width :: ! Int
42
+ } deriving (Show )
35
43
36
- fmt :: Int -> String -> Maybe String
37
- fmt width = fmap (List. intercalate " \n " . wrap width . toWords) . parse
44
+ fmt :: Config -> String -> Maybe String
45
+ fmt config = fmap (List. intercalate " \n " . wrap config . toWords) . parse
38
46
39
47
toWords :: [Parsed ] -> [String ]
40
48
toWords = map (strip . concat ) . splitWith (`elem` [" ->" , " =>" ]) . map unparse
@@ -43,15 +51,17 @@ toWords = map (strip . concat) . splitWith (`elem` ["->", "=>"]) . map unparse
43
51
-- I should wrap in there if necessary, but with an extra indent.
44
52
-- In fact, this is basically just Util.Format, so I should use that, after
45
53
-- I extract it.
46
- wrap :: Int -> [String ] -> [String ]
47
- wrap width = mapTail (indent<> ) . go (width : repeat (width - indentSpaces))
54
+ wrap :: Config -> [String ] -> [String ]
55
+ wrap config =
56
+ mapTail (indent<> ) . go (width : repeat (width - _indent config))
48
57
where
49
- indent = replicate indentSpaces ' '
58
+ indent = replicate (_indent config) ' '
50
59
go (width : widths) words
51
60
| null pre = []
52
61
| otherwise = unwords pre : go widths post
53
62
where (pre, post) = wrap1 width words
54
63
go [] _ = [] -- unreachable, widths is infinite
64
+ width = _width config
55
65
56
66
mapTail :: (a -> a ) -> [a ] -> [a ]
57
67
mapTail f (x : xs) = x : map f xs
0 commit comments