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

🚀 Better console color system #134

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions src/main/java/io/github/syst3ms/skriptparser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import io.github.syst3ms.skriptparser.registration.DefaultRegistration;
import io.github.syst3ms.skriptparser.registration.SkriptAddon;
import io.github.syst3ms.skriptparser.registration.SkriptRegistration;
import io.github.syst3ms.skriptparser.util.ConsoleColors;
import io.github.syst3ms.skriptparser.util.color.ConsoleColors;
import io.github.syst3ms.skriptparser.util.FileUtils;

import java.io.IOException;
Expand Down Expand Up @@ -153,7 +153,7 @@ public static void printLogs(List<LogEntry> logs, Calendar time, boolean tipsEna
for (LogEntry log : logs) {
ConsoleColors color = ConsoleColors.WHITE;
if (log.getType() == LogType.WARNING) {
color = ConsoleColors.YELLOW;
color = ConsoleColors.ORANGE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think change to orange is a good idea since yellow is in general associate with warning so it would imo create bad idea of warning for skript users that would next switch to another language

} else if (log.getType() == LogType.ERROR) {
color = ConsoleColors.RED;
} else if (log.getType() == LogType.INFO) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.github.syst3ms.skriptparser.expressions;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this pull request

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was made basing myself on my other PR.
Not sure how should I do now.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can delete the files locally and push the changes here. It will remove the files from the PR as Github compares with the latest commit in the master branch.


import io.github.syst3ms.skriptparser.Parser;
import io.github.syst3ms.skriptparser.lang.Expression;
import io.github.syst3ms.skriptparser.lang.TriggerContext;
import io.github.syst3ms.skriptparser.lang.base.TaggedExpression;
import io.github.syst3ms.skriptparser.parsing.ParseContext;

import java.util.Scanner;

/**
* Ask for a specific input in the system console and wait for the answer of the user.
* This will therefore return what the user entered into a string.
*
* @name Ask
* @type EXPRESSION
* @pattern ask [for] %string%
* @since ALPHA
* @author ItsTheSky
*/
public class ExprAsk implements Expression<String> {

static {
Parser.getMainRegistration().addExpression(
ExprAsk.class,
String.class,
true,
"ask [for] %string%"
);
}

private Expression<String> message;

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, ParseContext context) {
message = (Expression<String>) expressions[0];
return true;
}

@Override
public String[] getValues(TriggerContext ctx) {
Scanner scanner = new Scanner(System.in);
for (String line : TaggedExpression.apply(message, ctx, "console"))
System.out.println(line);
return new String[]{scanner.nextLine()};
}

@Override
public String toString(TriggerContext ctx, boolean debug) {
return "ask for " + message.toString(ctx, debug);
}
}
43 changes: 43 additions & 0 deletions src/main/java/io/github/syst3ms/skriptparser/tags/TagColor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.github.syst3ms.skriptparser.tags;

import io.github.syst3ms.skriptparser.Parser;
import io.github.syst3ms.skriptparser.registration.tags.ContinuousTag;
import io.github.syst3ms.skriptparser.util.color.ConsoleColors;

import java.util.Locale;
import java.util.Optional;

public class TagColor implements ContinuousTag {

static {
Parser.getMainRegistration().addTag(TagColor.class);
}

private ConsoleColors color;

@Override
public boolean init(String key, String[] parameters) {
final Optional<ConsoleColors> optional;
if (key.equalsIgnoreCase("color") && parameters.length != 0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should check for length 1.

optional = ConsoleColors.search(parameters[0]);
} else {
optional = ConsoleColors.search(key);
}

if (optional.isEmpty())
return false;

color = optional.get();
return color != ConsoleColors.RESET;
}

@Override
public String getValue() {
return color.toString();
}

@Override
public String toString(boolean debug) {
return "<color="+color.name().toLowerCase()+">";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.syst3ms.skriptparser.Parser;
import io.github.syst3ms.skriptparser.registration.tags.Tag;
import io.github.syst3ms.skriptparser.util.color.ConsoleColors;

/**
* A tag that resets all currently ongoing tags.
Expand All @@ -24,7 +25,7 @@ public boolean init(String key, String[] parameters) {
}

public String getValue(String affected) {
return affected;
return ConsoleColors.RESET + affected;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this implementation is correct for now, it reminds me that a small overhaul of the tag system is needed in order to give addons the flexibility of changing the reset tag.

}

public String toString(boolean debug) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package io.github.syst3ms.skriptparser.util.color;

import java.util.Optional;
import java.util.stream.Stream;

public enum ConsoleColors {

// Reset
RESET("0", true),

// Normal Colors
BLACK("30", false),
RED("31", false),
GREEN("32", false, "dark_green"),
ORANGE("33", false, "orange", "amber"),
BLUE("34", false, "dark_blue"),
PURPLE("35", false, "dark_pink"),
CYAN("36", false, "dark_aqua"),
WHITE("37", false, "dark_gray"),

// Bright Colors
BLACK_BRIGHT("90", false, "gray", "grey"),
RED_BRIGHT("91", false, "pink"),
GREEN_BRIGHT("92", false, "lime", "light_green"),
ORANGE_BRIGHT("93", false, "yellow"),
BLUE_BRIGHT("94", false, "light_blue"),
PURPLE_BRIGHT("95", false, "light_purple"),
CYAN_BRIGHT("96", false, "light_aqua", "aqua"),
WHITE_BRIGHT("97", false),

// Colors background
BLACK_BACKGROUND(BLACK, "40"),
RED_BACKGROUND(RED, "41"),
GREEN_BACKGROUND(GREEN, "42"),
ORANGE_BACKGROUND(ORANGE, "43"),
BLUE_BACKGROUND(BLUE, "44"),
PURPLE_BACKGROUND(PURPLE, "45"),
CYAN_BACKGROUND(CYAN, "46"),
WHITE_BACKGROUND(WHITE, "47"),

// Styles
BOLD("1", true, "b"),
ITALIC("3", true, "o", "it", "i"),
UNDERLINE("4", true, "n", "u"),
STRIKETHROUGH("9", true, "m", "strike", "s"),
;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something wrong here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's wrong here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks line 46

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't see what's wrong, is it the constant's name ?
It has been renamed by Mwexim


private static final String ANSI_DELIMITER = "\033[";

private final String code;
private final boolean style;
private final String[] aliases;

/**
* Used for backgrounds colors only.
* It will generate values according to it.
* @param color
*/
ConsoleColors(ConsoleColors color, String code) {
this.code = code;
this.style = false;
this.aliases = Stream.of(color.getAliases())
.map(alias -> alias + "_background")
.toArray(String[]::new);
}

ConsoleColors(String code, boolean style, String... aliases) {
this.code = code;
this.style = style;
this.aliases = aliases;
}

public static Optional<ConsoleColors> search(String input) {
for (ConsoleColors color : values()) {
if (color.match(input))
return Optional.of(color);
}
return Optional.empty();
}

public String format() {
return ANSI_DELIMITER + code + "m";
}

public boolean match(String other) {
if (other.replace(" ", "_").equalsIgnoreCase(name()))
return true;
// Checking for aliases
for (String alias : aliases) {
if (other.replace(" ", "_").equalsIgnoreCase(alias))
return true;
}

return false;
}

public String toString() {
return format();
}

public String[] getAliases() {
return aliases;
}

public String getCode() {
return code;
}

public boolean isStyle() {
return style;
}
}