@@ -16,6 +16,8 @@ import ../compiler / [idents, llstream, ast, msgs, syntaxes, options, pathutils,
16
16
17
17
import parseopt, strutils, os, sequtils
18
18
19
+ import std/ tempfiles
20
+
19
21
const
20
22
Version = " 0.2"
21
23
Usage = " nimpretty - Nim Pretty Printer Version " & Version & """
26
28
Options:
27
29
--out:file set the output file (default: overwrite the input file)
28
30
--outDir:dir set the output dir (default: overwrite the input files)
31
+ --stdin read input from stdin and write output to stdout
29
32
--indent:N[=0] set the number of spaces that is used for indentation
30
33
--indent:0 means autodetection (default behaviour)
31
34
--maxLineLen:N set the desired maximum line length (default: 80)
@@ -84,7 +87,7 @@ proc finalCheck(content: string; origAst: PNode): bool {.nimcall.} =
84
87
closeParser (parser)
85
88
result = conf.errorCounter == oldErrors # and goodEnough(newAst, origAst)
86
89
87
- proc prettyPrint * (infile, outfile: string , opt: PrettyOptions ) =
90
+ proc prettyPrint * (infile, outfile: string ; opt: PrettyOptions ) =
88
91
var conf = newConfigRef ()
89
92
let fileIdx = fileInfoIdx (conf, AbsoluteFile infile)
90
93
let f = splitFile (outfile.expandTilde)
@@ -99,20 +102,35 @@ proc prettyPrint*(infile, outfile: string, opt: PrettyOptions) =
99
102
when defined (nimpretty):
100
103
closeEmitter (parser.em, fullAst, finalCheck)
101
104
105
+ proc handleStdinInput (opt: PrettyOptions ) =
106
+ var content = readAll (stdin)
107
+
108
+ var (cfile, path) = createTempFile (" nimpretty_" , " .nim" )
109
+
110
+ writeFile (path, content)
111
+
112
+ prettyPrint (path, path, opt)
113
+
114
+ echo (readAll (cfile))
115
+
116
+ close (cfile)
117
+ removeFile (path)
118
+
102
119
proc main =
103
120
var outfile, outdir: string
104
121
105
122
var infiles = newSeq [string ]()
106
123
var outfiles = newSeq [string ]()
107
124
125
+ var isStdin = false
126
+
108
127
var backup = false
109
128
# when `on`, create a backup file of input in case
110
129
# `prettyPrint` could overwrite it (note that the backup may happen even
111
130
# if input is not actually overwritten, when nimpretty is a noop).
112
131
# --backup was un-documented (rely on git instead).
113
132
var opt = PrettyOptions (indWidth: 0 , maxLineLen: 80 )
114
133
115
-
116
134
for kind, key, val in getopt ():
117
135
case kind
118
136
of cmdArgument:
@@ -132,8 +150,15 @@ proc main =
132
150
of " outDir" , " outdir" : outdir = val
133
151
of " indent" : opt.indWidth = parseInt (val)
134
152
of " maxlinelen" : opt.maxLineLen = parseInt (val)
153
+ # "" is equal to '-' as input
154
+ of " stdin" , " " : isStdin = true
135
155
else : writeHelp ()
136
156
of cmdEnd: assert (false ) # cannot happen
157
+
158
+ if isStdin:
159
+ handleStdinInput (opt)
160
+ return
161
+
137
162
if infiles.len == 0 :
138
163
quit " [Error] no input file."
139
164
0 commit comments