Skip to content

Commit 911bc5b

Browse files
committed
feat: format the current directory by default
The common case is to format folders, not stdin. This changes the default to format the current directory, and introduces a `--stdin` flag for users that want to wrap `nixfmt` into editors and other tools. See also NixOS/nix#11438
1 parent d0c0cda commit 911bc5b

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

main/Main.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ data Nixfmt = Nixfmt
4747
width :: Width,
4848
check :: Bool,
4949
quiet :: Bool,
50+
stdin :: Bool,
5051
strict :: Bool,
5152
verify :: Bool,
5253
ast :: Bool
@@ -68,6 +69,7 @@ options =
6869
&= help (addDefaultHint defaultWidth "Maximum width in characters"),
6970
check = False &= help "Check whether files are formatted without modifying them",
7071
quiet = False &= help "Do not report errors",
72+
stdin = False &= help "Format the content passed to stdin",
7173
strict = False &= help "Enable a stricter formatting mode that isn't influenced as much by how the input is formatted",
7274
verify =
7375
False
@@ -148,10 +150,10 @@ checkFileTarget :: FilePath -> Target
148150
checkFileTarget path = Target (readFileUtf8 path) path (const $ const $ pure ())
149151

150152
toTargets :: Nixfmt -> IO [Target]
151-
toTargets Nixfmt{files = []} = pure [stdioTarget]
152-
toTargets Nixfmt{files = ["-"]} = pure [stdioTarget]
153-
toTargets Nixfmt{check = False, files = paths} = map fileTarget <$> collectAllNixFiles paths
154-
toTargets Nixfmt{check = True, files = paths} = map checkFileTarget <$> collectAllNixFiles paths
153+
toTargets Nixfmt{stdin = True, files = []} = pure [stdioTarget]
154+
toTargets Nixfmt{stdin = True, files = ["-"]} = pure [stdioTarget]
155+
toTargets Nixfmt{stdin = False, check = False, files = paths} = map fileTarget <$> collectAllNixFiles paths
156+
toTargets Nixfmt{stdin = False, check = True, files = paths} = map checkFileTarget <$> collectAllNixFiles paths
155157

156158
type Formatter = FilePath -> Text -> Either String Text
157159

0 commit comments

Comments
 (0)