From 81b17c8f31aa14888e1e3b4764fc7edccc1bf45c Mon Sep 17 00:00:00 2001 From: Jonas Drewsen Date: Sat, 17 Jan 2015 21:29:28 +0100 Subject: [PATCH] Allow for outputting to anything supporting the write() method. Make it possible to exclude main. --- src/dfmt.d | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/dfmt.d b/src/dfmt.d index 83bd5dc..f5f5674 100644 --- a/src/dfmt.d +++ b/src/dfmt.d @@ -42,6 +42,9 @@ Formats D code. -h, --help display this help and exit "; +version (NoMain) +{ } +else int main(string[] args) { import std.getopt; @@ -103,7 +106,8 @@ int main(string[] args) return 0; } -void format(string source_desc, ubyte[] buffer, File output) + +void format(Output)(string source_desc, ubyte[] buffer, Output output) { LexerConfig config; config.stringBehavior = StringBehavior.source; @@ -120,14 +124,14 @@ void format(string source_desc, ubyte[] buffer, File output) visitor.visit(mod); astInformation.cleanup(); auto tokens = byToken(buffer, config, &cache).array(); - auto tokenFormatter = TokenFormatter(tokens, output, &astInformation, + auto tokenFormatter = TokenFormatter!Output(tokens, output, &astInformation, &formatterConfig); tokenFormatter.format(); } -struct TokenFormatter +struct TokenFormatter(Output) { - this(const(Token)[] tokens, File output, ASTInformation* astInformation, + this(const(Token)[] tokens, Output output, ASTInformation* astInformation, FormatterConfig* config) { this.tokens = tokens; @@ -745,8 +749,8 @@ private: /// Length of the current line (so far) uint currentLineLength = 0; - /// File to output to - File output; + /// Output to write output to + Output output; /// Tokens being formatted const(Token)[] tokens;