Skip to content

Commit 8e17a68

Browse files
committed
fmt-signature: add indent-spaces arg
1 parent 63067f3 commit 8e17a68

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/FmtSignature.hs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,29 @@ import qualified Text.Read as Read
2020

2121

2222
usage :: String
23-
usage = "fmt-signature width"
23+
usage = "fmt-signature indent-spaces width"
2424

2525
main :: IO ()
2626
main = do
2727
args <- System.Environment.getArgs
2828
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)
3137
_ -> IO.hPutStrLn IO.stderr usage >> System.Exit.exitFailure
3238

33-
indentSpaces :: Int
34-
indentSpaces = 4
39+
data Config = Config {
40+
_indent :: !Int
41+
, _width :: !Int
42+
} deriving (Show)
3543

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
3846

3947
toWords :: [Parsed] -> [String]
4048
toWords = map (strip . concat) . splitWith (`elem` ["->", "=>"]) . map unparse
@@ -43,15 +51,17 @@ toWords = map (strip . concat) . splitWith (`elem` ["->", "=>"]) . map unparse
4351
-- I should wrap in there if necessary, but with an extra indent.
4452
-- In fact, this is basically just Util.Format, so I should use that, after
4553
-- 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))
4857
where
49-
indent = replicate indentSpaces ' '
58+
indent = replicate (_indent config) ' '
5059
go (width : widths) words
5160
| null pre = []
5261
| otherwise = unwords pre : go widths post
5362
where (pre, post) = wrap1 width words
5463
go [] _ = [] -- unreachable, widths is infinite
64+
width = _width config
5565

5666
mapTail :: (a -> a) -> [a] -> [a]
5767
mapTail f (x : xs) = x : map f xs

0 commit comments

Comments
 (0)