{"id":257,"date":"2019-09-23T09:51:27","date_gmt":"2019-09-23T08:51:27","guid":{"rendered":"https:\/\/www.bowlerblue.com\/site\/?p=257"},"modified":"2019-09-23T09:53:45","modified_gmt":"2019-09-23T08:53:45","slug":"colour-console-output-in-java","status":"publish","type":"post","link":"https:\/\/www.bowlerblue.com\/site\/colour-console-output-in-java\/","title":{"rendered":"Colour console output in Java"},"content":{"rendered":"\n<p>Wondering how to colour <code>System.out.println<\/code> console output in Java?  Use standard ANSI escape sequences (just concatenate them with your output, and use <code>ANSI_RESET<\/code> to go back to default).   This works in Linux, MacOS, Windows Powershell and Cygwin on Windows.  It DOESN&#8217;T work in Windows Command Prompt.<\/p>\n\n\n\n<p>Here are some handy constants:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static final String ANSI_RESET  = \"\\u001B[0m\";\n\npublic static final String ANSI_BLACK  = \"\\u001B[30m\";\npublic static final String ANSI_RED    = \"\\u001B[31m\";\npublic static final String ANSI_GREEN  = \"\\u001B[32m\";\npublic static final String ANSI_YELLOW = \"\\u001B[33m\";\npublic static final String ANSI_BLUE   = \"\\u001B[34m\";\npublic static final String ANSI_PURPLE = \"\\u001B[35m\";\npublic static final String ANSI_CYAN   = \"\\u001B[36m\";\npublic static final String ANSI_WHITE  = \"\\u001B[37m\";\n\npublic static final String ANSI_BRIGHT_BLACK  = \"\\u001B[90m\";\npublic static final String ANSI_BRIGHT_RED    = \"\\u001B[91m\";\npublic static final String ANSI_BRIGHT_GREEN  = \"\\u001B[92m\";\npublic static final String ANSI_BRIGHT_YELLOW = \"\\u001B[93m\";\npublic static final String ANSI_BRIGHT_BLUE   = \"\\u001B[94m\";\npublic static final String ANSI_BRIGHT_PURPLE = \"\\u001B[95m\";\npublic static final String ANSI_BRIGHT_CYAN   = \"\\u001B[96m\";\npublic static final String ANSI_BRIGHT_WHITE  = \"\\u001B[97m\";\n\npublic static final String[] FOREGROUNDS = {\n        ANSI_BLACK, ANSI_RED, ANSI_GREEN, ANSI_YELLOW,\n        ANSI_BLUE, ANSI_PURPLE, ANSI_CYAN, ANSI_WHITE,\n        ANSI_BRIGHT_BLACK, ANSI_BRIGHT_RED, ANSI_BRIGHT_GREEN, ANSI_BRIGHT_YELLOW,\n        ANSI_BRIGHT_BLUE, ANSI_BRIGHT_PURPLE, ANSI_BRIGHT_CYAN, ANSI_BRIGHT_WHITE\n};\n\npublic static final String ANSI_BG_BLACK  = \"\\u001B[40m\";\npublic static final String ANSI_BG_RED    = \"\\u001B[41m\";\npublic static final String ANSI_BG_GREEN  = \"\\u001B[42m\";\npublic static final String ANSI_BG_YELLOW = \"\\u001B[43m\";\npublic static final String ANSI_BG_BLUE   = \"\\u001B[44m\";\npublic static final String ANSI_BG_PURPLE = \"\\u001B[45m\";\npublic static final String ANSI_BG_CYAN   = \"\\u001B[46m\";\npublic static final String ANSI_BG_WHITE  = \"\\u001B[47m\";\n\npublic static final String ANSI_BRIGHT_BG_BLACK  = \"\\u001B[100m\";\npublic static final String ANSI_BRIGHT_BG_RED    = \"\\u001B[101m\";\npublic static final String ANSI_BRIGHT_BG_GREEN  = \"\\u001B[102m\";\npublic static final String ANSI_BRIGHT_BG_YELLOW = \"\\u001B[103m\";\npublic static final String ANSI_BRIGHT_BG_BLUE   = \"\\u001B[104m\";\npublic static final String ANSI_BRIGHT_BG_PURPLE = \"\\u001B[105m\";\npublic static final String ANSI_BRIGHT_BG_CYAN   = \"\\u001B[106m\";\npublic static final String ANSI_BRIGHT_BG_WHITE  = \"\\u001B[107m\";\n\npublic static final String[] BACKGROUNDS = {\n        ANSI_BG_BLACK, ANSI_BG_RED, ANSI_BG_GREEN, ANSI_BG_YELLOW,\n        ANSI_BG_BLUE, ANSI_BG_PURPLE, ANSI_BG_CYAN, ANSI_BG_WHITE,\n        ANSI_BRIGHT_BG_BLACK, ANSI_BRIGHT_BG_RED, ANSI_BRIGHT_BG_GREEN, ANSI_BRIGHT_BG_YELLOW,\n        ANSI_BRIGHT_BG_BLUE, ANSI_BRIGHT_BG_PURPLE, ANSI_BRIGHT_BG_CYAN, ANSI_BRIGHT_BG_WHITE };<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Wondering how to colour System.out.println console output in Java? Use standard ANSI escape sequences (just concatenate them with your output, and use ANSI_RESET to go back to default). This works in Linux, MacOS, Windows Powershell and Cygwin on Windows. It DOESN&#8217;T work in Windows Command Prompt. Here are some handy constants:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[9,7],"class_list":["post-257","post","type-post","status-publish","format-standard","hentry","category-code","tag-coding","tag-java"],"_links":{"self":[{"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/posts\/257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/comments?post=257"}],"version-history":[{"count":2,"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/posts\/257\/revisions"}],"predecessor-version":[{"id":259,"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/posts\/257\/revisions\/259"}],"wp:attachment":[{"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/media?parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/categories?post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bowlerblue.com\/site\/wp-json\/wp\/v2\/tags?post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}