Skip to content

Instantly share code, notes, and snippets.

@joeharrison714
Created February 25, 2017 19:27
Show Gist options
  • Save joeharrison714/c9bea7320e5f9f1bdc7ae0fbfedf7987 to your computer and use it in GitHub Desktop.
Save joeharrison714/c9bea7320e5f9f1bdc7ae0fbfedf7987 to your computer and use it in GitHub Desktop.
Write code to a file
using System.IO;
using System.Text;
//https://gist.github.com/joeharrison714/c9bea7320e5f9f1bdc7ae0fbfedf7987
internal class CodeWriter
{
private readonly StringBuilder _output;
private int _indents;
public CodeWriter()
{
_output = new StringBuilder();
}
public void WriteFile(string filename)
{
File.WriteAllText(filename, _output.ToString());
}
public void IncreaseIndent()
{
_indents++;
}
public void DecreaseIndent()
{
_indents--;
}
public void WriteEmptyLine()
{
WriteLine("");
}
public void WriteLine(string line)
{
WriteLine(line, new object[0]);
}
public void WriteLine(string line, params object[] args)
{
for (int i = 0; i < _indents; i++)
{
_output.Append("\t");
}
if (args.Length > 0)
{
line = string.Format(line, args);
}
_output.AppendLine(line);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment