| ... | ... | @@ -35,15 +35,12 @@ const Fmt = struct { |
| 35 | 35 | gpa: Allocator, |
| 36 | 36 | arena: Allocator, |
| 37 | 37 | out_buffer: std.Io.Writer.Allocating, |
| 38 | stdout_writer: *fs.File.Writer, |
| 38 | 39 | |
| 39 | 40 | const SeenMap = std.AutoHashMap(fs.File.INode, void); |
| 40 | 41 | }; |
| 41 | 42 | |
| 42 | | pub fn run( |
| 43 | | gpa: Allocator, |
| 44 | | arena: Allocator, |
| 45 | | args: []const []const u8, |
| 46 | | ) !void { |
| 43 | pub fn run(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { |
| 47 | 44 | var color: Color = .auto; |
| 48 | 45 | var stdin_flag = false; |
| 49 | 46 | var check_flag = false; |
| ... | ... | @@ -60,8 +57,7 @@ pub fn run( |
| 60 | 57 | const arg = args[i]; |
| 61 | 58 | if (mem.startsWith(u8, arg, "-")) { |
| 62 | 59 | if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) { |
| 63 | | const stdout = std.fs.File.stdout().deprecatedWriter(); |
| 64 | | try stdout.writeAll(usage_fmt); |
| 60 | try fs.File.stdout().writeAll(usage_fmt); |
| 65 | 61 | return process.cleanExit(); |
| 66 | 62 | } else if (mem.eql(u8, arg, "--color")) { |
| 67 | 63 | if (i + 1 >= args.len) { |
| ... | ... | @@ -156,13 +152,16 @@ pub fn run( |
| 156 | 152 | process.exit(code); |
| 157 | 153 | } |
| 158 | 154 | |
| 159 | | return std.fs.File.stdout().writeAll(formatted); |
| 155 | return fs.File.stdout().writeAll(formatted); |
| 160 | 156 | } |
| 161 | 157 | |
| 162 | 158 | if (input_files.items.len == 0) { |
| 163 | 159 | fatal("expected at least one source file argument", .{}); |
| 164 | 160 | } |
| 165 | 161 | |
| 162 | var stdout_buffer: [4096]u8 = undefined; |
| 163 | var stdout_writer = fs.File.stdout().writer(&stdout_buffer); |
| 164 | |
| 166 | 165 | var fmt: Fmt = .{ |
| 167 | 166 | .gpa = gpa, |
| 168 | 167 | .arena = arena, |
| ... | ... | @@ -172,6 +171,7 @@ pub fn run( |
| 172 | 171 | .force_zon = force_zon, |
| 173 | 172 | .color = color, |
| 174 | 173 | .out_buffer = .init(gpa), |
| 174 | .stdout_writer = &stdout_writer, |
| 175 | 175 | }; |
| 176 | 176 | defer fmt.seen.deinit(); |
| 177 | 177 | defer fmt.out_buffer.deinit(); |
| ... | ... | @@ -198,46 +198,10 @@ pub fn run( |
| 198 | 198 | if (fmt.any_error) { |
| 199 | 199 | process.exit(1); |
| 200 | 200 | } |
| 201 | try fmt.stdout_writer.interface.flush(); |
| 201 | 202 | } |
| 202 | 203 | |
| 203 | | const FmtError = error{ |
| 204 | | SystemResources, |
| 205 | | OperationAborted, |
| 206 | | IoPending, |
| 207 | | BrokenPipe, |
| 208 | | Unexpected, |
| 209 | | WouldBlock, |
| 210 | | Canceled, |
| 211 | | FileClosed, |
| 212 | | DestinationAddressRequired, |
| 213 | | DiskQuota, |
| 214 | | FileTooBig, |
| 215 | | MessageTooBig, |
| 216 | | InputOutput, |
| 217 | | NoSpaceLeft, |
| 218 | | AccessDenied, |
| 219 | | OutOfMemory, |
| 220 | | RenameAcrossMountPoints, |
| 221 | | ReadOnlyFileSystem, |
| 222 | | LinkQuotaExceeded, |
| 223 | | FileBusy, |
| 224 | | EndOfStream, |
| 225 | | Unseekable, |
| 226 | | NotOpenForWriting, |
| 227 | | UnsupportedEncoding, |
| 228 | | InvalidEncoding, |
| 229 | | ConnectionResetByPeer, |
| 230 | | SocketNotConnected, |
| 231 | | LockViolation, |
| 232 | | NetNameDeleted, |
| 233 | | InvalidArgument, |
| 234 | | ProcessNotFound, |
| 235 | | ConnectionTimedOut, |
| 236 | | NotOpenForReading, |
| 237 | | StreamTooLong, |
| 238 | | } || fs.File.OpenError; |
| 239 | | |
| 240 | | fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void { |
| 204 | fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) !void { |
| 241 | 205 | fmtPathFile(fmt, file_path, check_mode, dir, sub_path) catch |err| switch (err) { |
| 242 | 206 | error.IsDir, error.AccessDenied => return fmtPathDir(fmt, file_path, check_mode, dir, sub_path), |
| 243 | 207 | else => { |
| ... | ... | @@ -254,7 +218,7 @@ fn fmtPathDir( |
| 254 | 218 | check_mode: bool, |
| 255 | 219 | parent_dir: fs.Dir, |
| 256 | 220 | parent_sub_path: []const u8, |
| 257 | | ) FmtError!void { |
| 221 | ) !void { |
| 258 | 222 | var dir = try parent_dir.openDir(parent_sub_path, .{ .iterate = true }); |
| 259 | 223 | defer dir.close(); |
| 260 | 224 | |
| ... | ... | @@ -290,7 +254,7 @@ fn fmtPathFile( |
| 290 | 254 | check_mode: bool, |
| 291 | 255 | dir: fs.Dir, |
| 292 | 256 | sub_path: []const u8, |
| 293 | | ) FmtError!void { |
| 257 | ) !void { |
| 294 | 258 | const source_file = try dir.openFile(sub_path, .{}); |
| 295 | 259 | var file_closed = false; |
| 296 | 260 | errdefer if (!file_closed) source_file.close(); |
| ... | ... | @@ -381,8 +345,7 @@ fn fmtPathFile( |
| 381 | 345 | return; |
| 382 | 346 | |
| 383 | 347 | if (check_mode) { |
| 384 | | const stdout = std.fs.File.stdout().deprecatedWriter(); |
| 385 | | try stdout.print("{s}\n", .{file_path}); |
| 348 | try fmt.stdout_writer.interface.print("{s}\n", .{file_path}); |
| 386 | 349 | fmt.any_error = true; |
| 387 | 350 | } else { |
| 388 | 351 | var af = try dir.atomicFile(sub_path, .{ .mode = stat.mode }); |
| ... | ... | @@ -390,8 +353,7 @@ fn fmtPathFile( |
| 390 | 353 | |
| 391 | 354 | try af.file.writeAll(fmt.out_buffer.getWritten()); |
| 392 | 355 | try af.finish(); |
| 393 | | const stdout = std.fs.File.stdout().deprecatedWriter(); |
| 394 | | try stdout.print("{s}\n", .{file_path}); |
| 356 | try fmt.stdout_writer.interface.print("{s}\n", .{file_path}); |
| 395 | 357 | } |
| 396 | 358 | } |
| 397 | 359 | |