[Util] Colored Console Output :)

Discussion in 'Resources' started by Rockon999, Aug 18, 2013.

?

Does it work?

  1. Windows Console - Yes

    66.7%
  2. Windows Console - No

    16.7%
  3. GNOME Terminal - Yes

    16.7%
  4. GNOME Terminal - No

    0 vote(s)
    0.0%
  5. OS X Terminal - Yes

    16.7%
  6. OS X Terminal - No

    0 vote(s)
    0.0%
  7. Other Terminal (Specify In Comments) - Yes

    0 vote(s)
    0.0%
  8. Other Terminal (Specify In Comments) - No

    0 vote(s)
    0.0%
Multiple votes are allowed.
Thread Status:
Not open for further replies.
  1. Offline

    Rockon999

    This class lets you output to the console using ChatColor.[Color]. It requires CraftBukkit, but won't break between builds as it doesn't use any craftbukkit version dependent packages. (It uses Jansi). I haven't tested this outside of Gnome Terminal, but it should work on OS X and Windows, as Jansi SHOULD provide Windows compatible colors.

    Code:java
    1. package your.package;
    2.  
    3. import java.util.EnumMap;
    4. import java.util.Map;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.fusesource.jansi.Ansi;
    8. import org.fusesource.jansi.AnsiConsole;
    9. import org.fusesource.jansi.Ansi.Attribute;
    10.  
    11. public class ColoredConsole {
    12.  
    13. private static final Map<ChatColor, String> ansicolors = new EnumMap<ChatColor, String>(ChatColor.class);
    14. private static final ChatColor[] colors = ChatColor.values();
    15.  
    16. private static String colorize(String msg) {
    17. ansicolors.put(ChatColor.BLACK, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLACK).boldOff().toString());
    18. ansicolors.put(ChatColor.DARK_BLUE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLUE).boldOff().toString());
    19. ansicolors.put(ChatColor.DARK_GREEN, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.GREEN).boldOff().toString());
    20. ansicolors.put(ChatColor.DARK_AQUA, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.CYAN).boldOff().toString());
    21. ansicolors.put(ChatColor.DARK_RED, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.RED).boldOff().toString());
    22. ansicolors.put(ChatColor.DARK_PURPLE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.MAGENTA).boldOff().toString());
    23. ansicolors.put(ChatColor.GOLD, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.YELLOW).boldOff().toString());
    24. ansicolors.put(ChatColor.GRAY, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.WHITE).boldOff().toString());
    25. ansicolors.put(ChatColor.DARK_GRAY, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLACK).bold().toString());
    26. ansicolors.put(ChatColor.BLUE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLUE).bold().toString());
    27. ansicolors.put(ChatColor.GREEN, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.GREEN).bold().toString());
    28. ansicolors.put(ChatColor.AQUA, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.CYAN).bold().toString());
    29. ansicolors.put(ChatColor.RED, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.RED).bold().toString());
    30. ansicolors.put(ChatColor.LIGHT_PURPLE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.MAGENTA).bold().toString());
    31. ansicolors.put(ChatColor.YELLOW, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.YELLOW).bold().toString());
    32. ansicolors.put(ChatColor.WHITE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.WHITE).bold().toString());
    33. ansicolors.put(ChatColor.MAGIC, Ansi.ansi().a(Attribute.BLINK_SLOW).toString());
    34. ansicolors.put(ChatColor.BOLD, Ansi.ansi().a(Attribute.UNDERLINE_DOUBLE).toString());
    35. ansicolors.put(ChatColor.STRIKETHROUGH, Ansi.ansi().a(Attribute.STRIKETHROUGH_ON).toString());
    36. ansicolors.put(ChatColor.UNDERLINE, Ansi.ansi().a(Attribute.UNDERLINE).toString());
    37. ansicolors.put(ChatColor.ITALIC, Ansi.ansi().a(Attribute.ITALIC).toString());
    38. ansicolors.put(ChatColor.RESET, Ansi.ansi().a(Attribute.RESET).toString());
    39.  
    40. for (ChatColor c : colors) {
    41. if (!ansicolors.containsKey(c)) {
    42. msg = msg.replaceAll(c.toString(), "");
    43. } else {
    44. msg = msg.replaceAll(c.toString(), ansicolors.get(c));
    45. }
    46. }
    47. return msg;
    48. }
    49.  
    50. private static String OS = System.getProperty("os.name").toLowerCase();
    51.  
    52. public static void info(String msg) {
    53. if (OS.indexOf("win") >= 0) {
    54. AnsiConsole.out.print(colorize(msg) + Ansi.ansi().reset().toString());
    55. } else {
    56. System.out.println(colorize(msg) + Ansi.ansi().reset().toString());
    57. }
    58.  
    59. }
    60. }
    61.  
     
    Axe2760 likes this.
  2. Offline

    MTN

    Hmmm... If I use ChatColor for logger messages it did always work without any additional coding... Or is it just me? (coding on windows)
     
    jb_aero likes this.
  3. Offline

    Ultimate_n00b

    Code:
    Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.WHATEVER + "msg");
    Does that not work?
    Also some things like spacebukkit show color in console weirdly.
     
    Skyost likes this.
  4. Offline

    Minecrell

    Rockon999
    Actually it looks like you nearly just copied the CraftBukkit implementation? :confused:
    https://github.com/Bukkit/CraftBukk...raftbukkit/command/ColouredConsoleSender.java
    The colors are working on Windows and on Linux for me without your code...

    EDIT: By the way, there is no need to add the colors to the ansicolors map on every colorize() call... You only have to add them ones because the map is static and so you're adding the colors to the map on every call again..
     
  5. Offline

    timtower Administrator Administrator Moderator

    Minecrell likes this.
  6. Offline

    bloodless2010

    ChatColor.DARK_AQUA for example on a command run on my Linux (Ubuntu) Terminal has always returned colours.
     
Thread Status:
Not open for further replies.

Share This Page