support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
3 * Copyright © 2013 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk;
21 import java.io.FileNotFoundException;
22 import java.io.OutputStream;
23 import java.io.PrintWriter;
24 import java.io.UnsupportedEncodingException;
25 import java.io.Writer;
26 import java.util.EnumSet;
30 * @author Ing. František Kučera (frantovo.cz)
32 public class ColorfulPrintWriter extends PrintWriter {
34 public enum TerminalColor {
44 private final int foregroundCode;
45 private final int backgroundCode;
47 private TerminalColor(int foregroundCode, int backgroundCode) {
48 this.foregroundCode = foregroundCode;
49 this.backgroundCode = backgroundCode;
52 public int getForegroundCode() {
53 return foregroundCode;
56 public int getBackgroundCode() {
57 return backgroundCode;
61 public enum TerminalStyle {
72 private TerminalStyle(int code) {
76 public int getCode() {
80 private boolean colorful = true;
82 public void setStyle(EnumSet<TerminalStyle> styles) {
83 printCodes(getStyleCodes(styles));
86 private static int[] getStyleCodes(EnumSet<TerminalStyle> styles) {
87 int[] array = new int[styles.size()];
89 for (TerminalStyle s : styles) {
90 array[i++] = s.getCode();
95 public void setForegroundColor(TerminalColor color) {
96 printCodes(color.getForegroundCode());
99 public void setBackgroundColor(TerminalColor color) {
100 printCodes(color.getBackgroundCode());
103 public void print(TerminalColor foregroundColor, String string) {
104 setForegroundColor(foregroundColor);
109 public void println(TerminalColor foregroundColor, String string) {
110 print(foregroundColor, string);
114 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
115 setForegroundColor(foregroundColor);
116 setBackgroundColor(backgroundColor);
121 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, String string) {
122 print(foregroundColor, backgroundColor, string);
126 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
127 setForegroundColor(foregroundColor);
128 setBackgroundColor(backgroundColor);
134 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, EnumSet<TerminalStyle> styles, String string) {
135 print(foregroundColor, backgroundColor, styles, string);
139 public void print(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
140 print(foregroundColor, backgroundColor, EnumSet.of(style), string);
143 public void println(TerminalColor foregroundColor, TerminalColor backgroundColor, TerminalStyle style, String string) {
144 print(foregroundColor, backgroundColor, style, string);
148 public void print(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
149 setForegroundColor(foregroundColor);
155 public void println(TerminalColor foregroundColor, EnumSet<TerminalStyle> styles, String string) {
156 print(foregroundColor, styles, string);
160 public void print(TerminalColor foregroundColor, TerminalStyle style, String string) {
161 print(foregroundColor, EnumSet.of(style), string);
164 public void println(TerminalColor foregroundColor, TerminalStyle style, String string) {
165 print(foregroundColor, style, string);
169 public void print(EnumSet<TerminalStyle> styles, String string) {
175 public void println(EnumSet<TerminalStyle> styles, String string) {
176 print(styles, string);
180 public void print(TerminalStyle style, String string) {
181 print(EnumSet.of(style), string);
184 public void println(TerminalStyle style, String string) {
185 print(style, string);
189 public void resetAll() {
193 private void printCodes(int... codes) {
196 for (int i = 0; i < codes.length; i++) {
198 if (i < codes.length - 1 && codes.length > 1) {
206 public boolean isColorful() {
210 public void setColorful(boolean colorful) {
211 this.colorful = colorful;
214 public ColorfulPrintWriter(File file) throws FileNotFoundException {
218 public ColorfulPrintWriter(OutputStream out) {
222 public ColorfulPrintWriter(String fileName) throws FileNotFoundException {
226 public ColorfulPrintWriter(Writer out) {
230 public ColorfulPrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
234 public ColorfulPrintWriter(OutputStream out, boolean autoFlush) {
235 super(out, autoFlush);
238 public ColorfulPrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
239 super(fileName, csn);
242 public ColorfulPrintWriter(Writer out, boolean autoFlush) {
243 super(out, autoFlush);