Skip to content

Commit

Permalink
The first version of my CLI that works ;)
Browse files Browse the repository at this point in the history
  • Loading branch information
rezanid committed Feb 27, 2022
1 parent bbd6551 commit 6c50008
Show file tree
Hide file tree
Showing 52 changed files with 4,504 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/CLI/Novo.Docx.Cli.Utils/AnsiColorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Novo.Docx.Cli.Utils
{
public static class AnsiColorExtensions
{
public static string Black(this string text) => "\x1B[30m" + text + "\x1B[39m";

public static string Red(this string text) => "\x1B[31m" + text + "\x1B[39m";

public static string Green(this string text) => "\x1B[32m" + text + "\x1B[39m";

public static string Yellow(this string text) => "\x1B[33m" + text + "\x1B[39m";

public static string Blue(this string text) => "\x1B[34m" + text + "\x1B[39m";

public static string Magenta(this string text) => "\x1B[35m" + text + "\x1B[39m";

public static string Cyan(this string text) => "\x1B[36m" + text + "\x1B[39m";

public static string White(this string text) => "\x1B[37m" + text + "\x1B[39m";

public static string Bold(this string text) => "\x1B[1m" + text + "\x1B[22m";
}
}
147 changes: 147 additions & 0 deletions src/CLI/Novo.Docx.Cli.Utils/AnsiConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;

namespace Novo.Docx.Cli.Utils
{
public class AnsiConsole
{
private const int Light = 0x08;

private AnsiConsole(TextWriter writer)
{
Writer = writer;

OriginalForegroundColor = Console.ForegroundColor;
_boldRecursion = ((int)OriginalForegroundColor & Light) != 0 ? 1 : 0;
}

private int _boldRecursion;

public static AnsiConsole GetOutput()
{
return new AnsiConsole(Console.Out);
}

public static AnsiConsole GetError()
{
return new AnsiConsole(Console.Error);
}

public TextWriter Writer { get; }

public ConsoleColor OriginalForegroundColor { get; }

private void SetColor(ConsoleColor color)
{
int c = (int)color;

Console.ForegroundColor =
c < 0 ? color : // unknown, just use it
_boldRecursion > 0 ? (ConsoleColor)(c | Light) : // ensure color is light
(ConsoleColor)(c & ~Light); // ensure color is dark
}

private void SetBold(bool bold)
{
_boldRecursion += bold ? 1 : -1;
if (_boldRecursion > 1 || (_boldRecursion == 1 && !bold))
{
return;
}

// switches on _boldRecursion to handle boldness
SetColor(Console.ForegroundColor);
}

public void WriteLine(string message)
{
Write(message);
Writer.WriteLine();
}


public void Write(string message)
{
var escapeScan = 0;
for (;;)
{
var escapeIndex = message.IndexOf("\x1b[", escapeScan, StringComparison.Ordinal);
if (escapeIndex == -1)
{
var text = message.Substring(escapeScan);
Writer.Write(text);
break;
}
else
{
var startIndex = escapeIndex + 2;
var endIndex = startIndex;
while (endIndex != message.Length &&
message[endIndex] >= 0x20 &&
message[endIndex] <= 0x3f)
{
endIndex += 1;
}

var text = message.Substring(escapeScan, escapeIndex - escapeScan);
Writer.Write(text);
if (endIndex == message.Length)
{
break;
}

switch (message[endIndex])
{
case 'm':
int value;
if (int.TryParse(message.Substring(startIndex, endIndex - startIndex), out value))
{
switch (value)
{
case 1:
SetBold(true);
break;
case 22:
SetBold(false);
break;
case 30:
SetColor(ConsoleColor.Black);
break;
case 31:
SetColor(ConsoleColor.Red);
break;
case 32:
SetColor(ConsoleColor.Green);
break;
case 33:
SetColor(ConsoleColor.Yellow);
break;
case 34:
SetColor(ConsoleColor.Blue);
break;
case 35:
SetColor(ConsoleColor.Magenta);
break;
case 36:
SetColor(ConsoleColor.Cyan);
break;
case 37:
SetColor(ConsoleColor.Gray);
break;
case 39:
Console.ForegroundColor = OriginalForegroundColor;
break;
}
}
break;
}

escapeScan = endIndex + 1;
}
}
}
}
}
35 changes: 35 additions & 0 deletions src/CLI/Novo.Docx.Cli.Utils/CommandContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace Novo.Docx.Cli.Utils
{
public static class CommandContext
{
public static class Variables
{
private static readonly string Prefix = "DOTNET_CLI_CONTEXT_";
public static readonly string Verbose = Prefix + "VERBOSE";
public static readonly string AnsiPassThru = Prefix + "ANSI_PASS_THRU";
}

private static Lazy<bool> _verbose = new Lazy<bool>(() => Env.GetEnvironmentVariableAsBool(Variables.Verbose));
private static Lazy<bool> _ansiPassThru = new Lazy<bool>(() => Env.GetEnvironmentVariableAsBool(Variables.AnsiPassThru));

public static bool IsVerbose()
{
return _verbose.Value;
}

public static bool ShouldPassAnsiCodesThrough()
{
return _ansiPassThru.Value;
}

public static void SetVerbose(bool value)
{
_verbose = new Lazy<bool>(() => value);
}
}
}
22 changes: 22 additions & 0 deletions src/CLI/Novo.Docx.Cli.Utils/DebugHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Diagnostics;
namespace Novo.Docx.Cli.Utils;
public static class DebugHelper
{
[Conditional("DEBUG")]
public static void HandleDebugSwitch(ref string[] args)
{
if (args.Length > 0 && string.Equals("--debug", args[0], StringComparison.OrdinalIgnoreCase))
{
args = args.Skip(1).ToArray();
WaitForDebugger();
}
}

public static void WaitForDebugger()
{
int processId = Environment.ProcessId;
Console.WriteLine(LocalizableStrings.WaitingForDebuggerToAttach);
Console.WriteLine(string.Format(LocalizableStrings.ProcessId, processId));
Console.ReadLine();
}
}
45 changes: 45 additions & 0 deletions src/CLI/Novo.Docx.Cli.Utils/Env.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;

namespace Novo.Docx.Cli.Utils
{
public static class Env
{
private static readonly IEnvironmentProvider _environment = new EnvironmentProvider();

public static IEnumerable<string> ExecutableExtensions
{
get
{
return _environment.ExecutableExtensions;
}
}

public static string GetCommandPath(string commandName, params string[] extensions)
{
return _environment.GetCommandPath(commandName, extensions);
}

public static string GetCommandPathFromRootPath(string rootPath, string commandName, params string[] extensions)
{
return _environment.GetCommandPathFromRootPath(rootPath, commandName, extensions);
}

public static string GetCommandPathFromRootPath(string rootPath, string commandName, IEnumerable<string> extensions)
{
return _environment.GetCommandPathFromRootPath(rootPath, commandName, extensions);
}

public static bool GetEnvironmentVariableAsBool(string name, bool defaultValue = false)
{
return _environment.GetEnvironmentVariableAsBool(name, defaultValue);
}

public static string GetEnvironmentVariable(string name)
{
return _environment.GetEnvironmentVariable(name);
}
}
}
Loading

0 comments on commit 6c50008

Please sign in to comment.