Skip to content

Commit

Permalink
#95 add comments on expected diffs between Print and Output Streams
Browse files Browse the repository at this point in the history
  • Loading branch information
hboutemy committed Dec 2, 2017
1 parent 40631bd commit 5ac2049
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 52 deletions.
1 change: 1 addition & 0 deletions jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public void close() throws IOException {
* @param ps original PrintStream to wrap
* @param fileno file descriptor
* @return wrapped PrintStream depending on OS and system properties
* @since 1.17
*/
public static PrintStream wrapPrintStream(final PrintStream ps, int fileno) {

Expand Down
39 changes: 20 additions & 19 deletions jansi/src/main/java/org/fusesource/jansi/AnsiOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package org.fusesource.jansi;

import java.io.FilterOutputStream;
import java.io.FilterOutputStream; // expected diff with AnsiPrintStream.java
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStream; // expected diff with AnsiPrintStream.java
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
Expand All @@ -37,16 +37,17 @@
* @author <a href="http://hiramchirino.com">Hiram Chirino</a>
* @author Joris Kuipers
* @since 1.0
* @see AnsiPrintStream
*/
public class AnsiOutputStream extends FilterOutputStream {
public class AnsiOutputStream extends FilterOutputStream { // expected diff with AnsiPrintStream.java

public static final byte[] RESET_CODE = "\033[0m".getBytes();
public static final byte[] RESET_CODE = "\033[0m".getBytes(); // expected diff with AnsiPrintStream.java

@Deprecated
public static final byte[] REST_CODE = RESET_CODE;
public static final byte[] REST_CODE = RESET_CODE; // expected diff with AnsiPrintStream.java

public AnsiOutputStream(OutputStream os) {
super(os);
public AnsiOutputStream(OutputStream os) { // expected diff with AnsiPrintStream.java
super(os); // expected diff with AnsiPrintStream.java
}

private final static int MAX_ESCAPE_SEQUENCE_LENGTH = 100;
Expand Down Expand Up @@ -77,16 +78,16 @@ public AnsiOutputStream(OutputStream os) {
private static final int SECOND_CHARSET1_CHAR = ')';

@Override
public synchronized void write(int data) throws IOException {
public synchronized void write(int data) throws IOException { // expected diff with AnsiPrintStream.java
switch (state) {
case LOOKING_FOR_FIRST_ESC_CHAR:
if (data == FIRST_ESC_CHAR) {
buffer[pos++] = (byte) data;
state = LOOKING_FOR_SECOND_ESC_CHAR;
} else {
out.write(data);
} else { // expected diff with AnsiPrintStream.java
out.write(data); // expected diff with AnsiPrintStream.java
}
break;
break; // expected diff with AnsiPrintStream.java

case LOOKING_FOR_SECOND_ESC_CHAR:
buffer[pos++] = (byte) data;
Expand Down Expand Up @@ -220,9 +221,9 @@ public synchronized void write(int data) throws IOException {
* @param skipBuffer if current buffer should be skipped or written to out
* @throws IOException
*/
private void reset(boolean skipBuffer) throws IOException {
private void reset(boolean skipBuffer) throws IOException { // expected diff with AnsiPrintStream.java
if (!skipBuffer) {
out.write(buffer, 0, pos);
out.write(buffer, 0, pos); // expected diff with AnsiPrintStream.java
}
pos = 0;
startOfValue = 0;
Expand Down Expand Up @@ -251,7 +252,7 @@ private int getNextOptionInt(Iterator<Object> optionsIterator) throws IOExceptio
* @param command
* @return true if the escape command was processed.
*/
private boolean processEscapeCommand(ArrayList<Object> options, int command) throws IOException {
private boolean processEscapeCommand(ArrayList<Object> options, int command) throws IOException { // expected diff with AnsiPrintStream.java
try {
switch (command) {
case 'A':
Expand Down Expand Up @@ -401,7 +402,7 @@ else if (arg2or5 == 5) {
* @param options
* @return true if the operating system command was processed.
*/
private boolean processOperatingSystemCommand(ArrayList<Object> options) throws IOException {
private boolean processOperatingSystemCommand(ArrayList<Object> options) throws IOException { // expected diff with AnsiPrintStream.java
int command = optionInt(options, 0);
String label = (String) options.get(1);
// for command > 2 label could be composed (i.e. contain ';'), but we'll leave
Expand Down Expand Up @@ -670,7 +671,7 @@ protected void processCursorUpLine(int count) throws IOException {
protected void processCursorDownLine(int count) throws IOException {
// Poor mans impl..
for (int i = 0; i < count; i++) {
out.write('\n');
out.write('\n'); // expected diff with AnsiPrintStream.java
}
}

Expand All @@ -690,7 +691,7 @@ protected void processCursorLeft(int count) throws IOException {
protected void processCursorRight(int count) throws IOException {
// Poor mans impl..
for (int i = 0; i < count; i++) {
out.write(' ');
out.write(' '); // expected diff with AnsiPrintStream.java
}
}

Expand Down Expand Up @@ -785,8 +786,8 @@ private int optionInt(ArrayList<Object> options, int index, int defaultValue) {
}

@Override
public void close() throws IOException {
write(RESET_CODE);
public void close() throws IOException { // expected diff with AnsiPrintStream.java
write(RESET_CODE); // expected diff with AnsiPrintStream.java
flush();
super.close();
}
Expand Down
39 changes: 20 additions & 19 deletions jansi/src/main/java/org/fusesource/jansi/AnsiPrintStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.fusesource.jansi;

import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintStream; // expected diff with AnsiOutputStream.java
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
Expand All @@ -36,13 +36,14 @@
* @author <a href="http://hiramchirino.com">Hiram Chirino</a>
* @author Joris Kuipers
* @since 1.7
* @see AnsiOutputStream
*/
public class AnsiPrintStream extends FilterPrintStream {
public class AnsiPrintStream extends FilterPrintStream { // expected diff with AnsiOutputStream.java

public static final String RESET_CODE = "\033[0m";
public static final String RESET_CODE = "\033[0m"; // expected diff with AnsiOutputStream.java

public AnsiPrintStream(PrintStream ps) {
super(ps);
public AnsiPrintStream(PrintStream ps) { // expected diff with AnsiOutputStream.java
super(ps); // expected diff with AnsiOutputStream.java
}

private final static int MAX_ESCAPE_SEQUENCE_LENGTH = 100;
Expand Down Expand Up @@ -70,15 +71,15 @@ public AnsiPrintStream(PrintStream ps) {
private static final int SECOND_ST_CHAR = '\\';

@Override
protected boolean filter(int data) {
protected boolean filter(int data) { // expected diff with AnsiOutputStream.java
switch (state) {
case LOOKING_FOR_FIRST_ESC_CHAR:
if (data == FIRST_ESC_CHAR) {
buffer[pos++] = (byte) data;
state = LOOKING_FOR_SECOND_ESC_CHAR;
return false;
return false; // expected diff with AnsiOutputStream.java
}
return true;
return true; // expected diff with AnsiOutputStream.java

case LOOKING_FOR_SECOND_ESC_CHAR:
buffer[pos++] = (byte) data;
Expand Down Expand Up @@ -194,16 +195,16 @@ protected boolean filter(int data) {
if (pos >= buffer.length) {
reset(false);
}
return false;
return false; // expected diff with AnsiOutputStream.java
}

/**
* Resets all state to continue with regular parsing
* @param skipBuffer if current buffer should be skipped or written to out
*/
private void reset(boolean skipBuffer) {
private void reset(boolean skipBuffer) { // expected diff with AnsiOutputStream.java
if (!skipBuffer) {
ps.write(buffer, 0, pos);
ps.write(buffer, 0, pos); // expected diff with AnsiOutputStream.java
}
pos = 0;
startOfValue = 0;
Expand Down Expand Up @@ -232,7 +233,7 @@ private int getNextOptionInt(Iterator<Object> optionsIterator) throws IOExceptio
* @param command
* @return true if the escape command was processed.
*/
private boolean processEscapeCommand(ArrayList<Object> options, int command) {
private boolean processEscapeCommand(ArrayList<Object> options, int command) { // expected diff with AnsiOutputStream.java
try {
switch (command) {
case 'A':
Expand Down Expand Up @@ -373,8 +374,8 @@ else if (arg2or5 == 5) {
return false;
}
} catch (IllegalArgumentException ignore) {
} catch (IOException ioe) {
setError();
} catch (IOException ioe) { // expected diff with AnsiOutputStream.java
setError(); // expected diff with AnsiOutputStream.java
}
return false;
}
Expand All @@ -384,7 +385,7 @@ else if (arg2or5 == 5) {
* @param options
* @return true if the operating system command was processed.
*/
private boolean processOperatingSystemCommand(ArrayList<Object> options) {
private boolean processOperatingSystemCommand(ArrayList<Object> options) { // expected diff with AnsiOutputStream.java
int command = optionInt(options, 0);
String label = (String) options.get(1);
// for command > 2 label could be composed (i.e. contain ';'), but we'll leave
Expand Down Expand Up @@ -651,7 +652,7 @@ protected void processCursorUpLine(int count) throws IOException {
protected void processCursorDownLine(int count) throws IOException {
// Poor mans impl..
for (int i = 0; i < count; i++) {
print('\n');
print('\n'); // expected diff with AnsiOutputStream.java
}
}

Expand All @@ -671,7 +672,7 @@ protected void processCursorLeft(int count) throws IOException {
protected void processCursorRight(int count) throws IOException {
// Poor mans impl..
for (int i = 0; i < count; i++) {
print(' ');
print(' '); // expected diff with AnsiOutputStream.java
}
}

Expand Down Expand Up @@ -748,8 +749,8 @@ private int optionInt(ArrayList<Object> options, int index, int defaultValue) {
}

@Override
public void close() {
ps.print(RESET_CODE);
public void close() { // expected diff with AnsiOutputStream.java
ps.print(RESET_CODE); // expected diff with AnsiOutputStream.java
flush();
super.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import static org.fusesource.jansi.internal.Kernel32.SetConsoleTitle;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStream; // expected diff with WindowsAnsiPrintStream.java

import org.fusesource.jansi.internal.WindowsSupport;
import org.fusesource.jansi.internal.Kernel32.CONSOLE_SCREEN_BUFFER_INFO;
Expand All @@ -49,8 +49,9 @@
* @since 1.0
* @author <a href="http://hiramchirino.com">Hiram Chirino</a>
* @author Joris Kuipers
* @see WindowsAnsiPrintStream
*/
public final class WindowsAnsiOutputStream extends AnsiOutputStream {
public final class WindowsAnsiOutputStream extends AnsiOutputStream { // expected diff with WindowsAnsiPrintStream.java

private static final long console = GetStdHandle(STD_OUTPUT_HANDLE);

Expand Down Expand Up @@ -95,14 +96,14 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream {
private short savedX = -1;
private short savedY = -1;

public WindowsAnsiOutputStream(OutputStream os) throws IOException {
super(os);
public WindowsAnsiOutputStream(OutputStream os) throws IOException { // expected diff with WindowsAnsiPrintStream.java
super(os); // expected diff with WindowsAnsiPrintStream.java
getConsoleInfo();
originalColors = info.attributes;
}

private void getConsoleInfo() throws IOException {
out.flush();
out.flush(); // expected diff with WindowsAnsiPrintStream.java
if (GetConsoleScreenBufferInfo(console, info) == 0) {
throw new IOException("Could not get the screen info: " + WindowsSupport.getLastErrorMessage());
}
Expand All @@ -112,7 +113,7 @@ private void getConsoleInfo() throws IOException {
}

private void applyAttribute() throws IOException {
out.flush();
out.flush(); // expected diff with WindowsAnsiPrintStream.java
short attributes = info.attributes;
if (negative) {
attributes = invertAttributeColors(attributes);
Expand Down Expand Up @@ -327,7 +328,7 @@ protected void processSaveCursorPosition() throws IOException {
protected void processRestoreCursorPosition() throws IOException {
// restore only if there was a save operation first
if (savedX != -1 && savedY != -1) {
out.flush();
out.flush(); // expected diff with WindowsAnsiPrintStream.java
info.cursorPosition.x = savedX;
info.cursorPosition.y = savedY;
applyCursorPosition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import static org.fusesource.jansi.internal.Kernel32.SetConsoleTitle;

import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintStream; // expected diff with WindowsAnsiOutputStream.java

import org.fusesource.jansi.internal.WindowsSupport;
import org.fusesource.jansi.internal.Kernel32.CONSOLE_SCREEN_BUFFER_INFO;
Expand All @@ -49,8 +49,9 @@
* @since 1.7
* @author <a href="http://hiramchirino.com">Hiram Chirino</a>
* @author Joris Kuipers
* @see WindowsAnsiOutputStream
*/
public final class WindowsAnsiPrintStream extends AnsiPrintStream {
public final class WindowsAnsiPrintStream extends AnsiPrintStream { // expected diff with WindowsAnsiOutputStream.java

private static final long console = GetStdHandle(STD_OUTPUT_HANDLE);

Expand Down Expand Up @@ -95,14 +96,14 @@ public final class WindowsAnsiPrintStream extends AnsiPrintStream {
private short savedX = -1;
private short savedY = -1;

public WindowsAnsiPrintStream(PrintStream ps) throws IOException {
super(ps);
public WindowsAnsiPrintStream(PrintStream ps) throws IOException { // expected diff with WindowsAnsiOutputStream.java
super(ps); // expected diff with WindowsAnsiOutputStream.java
getConsoleInfo();
originalColors = info.attributes;
}

private void getConsoleInfo() throws IOException {
ps.flush();
ps.flush(); // expected diff with WindowsAnsiOutputStream.java
if (GetConsoleScreenBufferInfo(console, info) == 0) {
throw new IOException("Could not get the screen info: " + WindowsSupport.getLastErrorMessage());
}
Expand All @@ -112,7 +113,7 @@ private void getConsoleInfo() throws IOException {
}

private void applyAttribute() throws IOException {
ps.flush();
ps.flush(); // expected diff with WindowsAnsiOutputStream.java
short attributes = info.attributes;
if (negative) {
attributes = invertAttributeColors(attributes);
Expand Down Expand Up @@ -323,7 +324,7 @@ protected void processSaveCursorPosition() throws IOException {
protected void processRestoreCursorPosition() throws IOException {
// restore only if there was a save operation first
if (savedX != -1 && savedY != -1) {
ps.flush();
ps.flush(); // expected diff with WindowsAnsiOutputStream.java
info.cursorPosition.x = savedX;
info.cursorPosition.y = savedY;
applyCursorPosition();
Expand Down

0 comments on commit 5ac2049

Please sign in to comment.