Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ANSI coloring to haxelib help command #637

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified run.n
Binary file not shown.
23 changes: 15 additions & 8 deletions src/haxelib/client/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ using StringTools;
using Lambda;
using haxelib.MetaData;
using haxelib.api.RepoReformatter;
using haxelib.client.ansi.Ansi;

@:structInit
class CommandInfo {
Expand Down Expand Up @@ -153,8 +154,12 @@ class Main {
var maxLength = 0;

inline function checkLength(line:String)
if (line.length > maxLength)
maxLength = line.length;
{
var regexp = ~/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -\]*[@-~])/g;
if (regexp.replace(line, '').length > maxLength)
maxLength = regexp.replace(line, '').length;
}


function generateLines(data:List<UsageData>, generate:(UsageData -> String)):Array<{usage:String, description:String}>
return [
Expand All @@ -164,27 +169,29 @@ class Main {
{usage: line, description: item.description};
}];

final switchLines = generateLines(Args.generateSwitchDocs(), (flag) -> '--${flag.name}');
final optionLines = generateLines(Args.generateOptionDocs(), (flag) -> combineAliases(flag.name, flag.aliases) + ' ${flag.parameter}');
final switchLines = generateLines(Args.generateSwitchDocs(), (flag) -> '--${flag.name}'.fg(ORANGE).attr(INTENSITY_BOLD).reset());
final optionLines = generateLines(Args.generateOptionDocs(), (flag) -> combineAliases(flag.name, flag.aliases).fg(ORANGE).attr(INTENSITY_BOLD).reset() + ' ${flag.parameter}'.fg(ORANGE).reset());

final categories = new Map<String, Array<{usage:String, description:String}>>();
for (command in Args.generateCommandDocs()) {
checkLength(command.name);
final categoryName = command.category.getName();
if (!categories.exists(categoryName))
categories[categoryName] = [];
categories[categoryName].push({usage: command.name, description: command.description});
categories[categoryName].push({usage: '${command.name.fg(ORANGE).attr(INTENSITY_BOLD).reset()}', description: command.description});
}

Cli.print('Haxe Library Manager $VERSION - (c)2006-2024 Haxe Foundation');
Cli.print(" Usage: haxelib [command] [options]");
Cli.print(' Usage: '.fg(DARK_ORANGE).attr(INTENSITY_BOLD).reset() + 'haxelib '.fg(ORANGE).attr(INTENSITY_BOLD).reset() + '[command] '.fg(ORANGE) + '[options]'.reset());
Cli.print('');

inline function display(type:String, lines:Array<{usage:String, description:String}>) {
Cli.print(' $type');
Cli.print(' $type'.fg(DARK_ORANGE).attr(INTENSITY_BOLD).reset());
for (line in lines) {
final padded = line.usage.rpad(' ', maxLength);
Cli.print(' $padded : ${line.description}');
Cli.print(' $padded ${line.description}');
}
Cli.print('');
}

for (name => commands in categories)
Expand Down
69 changes: 69 additions & 0 deletions src/haxelib/client/ansi/Ansi.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package haxelib.client.ansi;


/**
* https://en.wikipedia.org/wiki/ANSI_escape_code
* http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/c327.html
* http://ascii-table.com/ansi-escape-sequences.php
*/
class Ansi {

/**
* ANSI escape sequence header
*/
public static inline final ESC = "\x1B[";

inline
public static function reset(str:String):String
return str + ESC + "0m";


/**
* sets the given text attribute
*/
inline
public static function attr(str:String, attr:AnsiTextAttribute):String
return ESC + (attr) + "m" + str;


/**
* set the text background color
*
* <pre><code>
* >>> Ansi.bg(RED) == "\x1B[41m"
* </code></pre>
*/
inline
public static function bg(str: String, color:AnsiColor):String
return ESC + "4" + color + "m" + str;


/**
* Clears the screen and moves the cursor to the home position
*/
inline
public static function clearScreen():String
return ESC + "2J";


/**
* Clear all characters from current position to the end of the line including the character at the current position
*/
inline
public static function clearLine():String
return ESC + "K";


/**
* set the text foreground color
*
* <pre><code>
* >>> Ansi.fg(RED) == "\x1B[31m"
* </code></pre>
*/
inline
public static function fg(str: String, color:AnsiColor):String
return ESC + "38;5;" + color + "m" + str;

}

22 changes: 22 additions & 0 deletions src/haxelib/client/ansi/AnsiColor.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com) and contributors
* SPDX-FileContributor: Sebastian Thomschke, Vegard IT GmbH
* SPDX-License-Identifier: Apache-2.0
*/
package haxelib.client.ansi;

#if (haxe_ver < 4.3) @:enum #else enum #end
abstract AnsiColor(Int) {
final BLACK = 0;
final RED = 1;
final GREEN = 2;
final YELLOW = 3;
final BLUE = 4;
final MAGENTA = 5;
final CYAN = 6;
final WHITE = 7;
final DEFAULT = 9;
final ORANGE = 216;
final DARK_ORANGE = 215;
final ORANGE_BRIGHT = 208;
}
70 changes: 70 additions & 0 deletions src/haxelib/client/ansi/AnsiTextAttribute.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com) and contributors
* SPDX-FileContributor: Sebastian Thomschke, Vegard IT GmbH
* SPDX-License-Identifier: Apache-2.0
*/
package haxelib.client.ansi;

/**
* https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_codes
*/
#if (haxe_ver < 4.3) @:enum #else enum #end
abstract AnsiTextAttribute(Int) {

/**
* All colors/text-attributes off
*/
final RESET = 0;

final INTENSITY_BOLD = 1;

/**
* Not widely supported.
*/
final INTENSITY_FAINT = 2;

/**
* Not widely supported.
*/
final ITALIC = 3;

final UNDERLINE_SINGLE = 4;

final BLINK_SLOW = 5;

/**
* Not widely supported.
*/
final BLINK_FAST = 6;

final NEGATIVE = 7;

/**
* Not widely supported.
*/
final HIDDEN = 8;

/**
* Not widely supported.
*/
final STRIKETHROUGH = 9;

/**
* Not widely supported.
*/
final UNDERLINE_DOUBLE = 21;

final INTENSITY_OFF = 22;

final ITALIC_OFF = 23;

final UNDERLINE_OFF = 24;

final BLINK_OFF = 25;

final NEGATIVE_OFF = 27;

final HIDDEN_OFF = 28;

final STRIKTHROUGH_OFF = 29;
}