Skip to content

Commit

Permalink
Improve toString() for Attributes, Pty
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Mar 23, 2017
1 parent 769426c commit 50c14de
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,9 @@ public void setSize(Size size) throws IOException {

protected abstract Attributes toAttributes(CLibrary.Termios tios);

@Override
public String toString() {
return "JansiNativePty[" + getName() + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,10 @@ protected static FileDescriptor newDescriptor(int fd) {
throw new RuntimeException("Unable to create FileDescriptor", e);
}
}

@Override
public String toString() {
return "JnaNativePty[" + getName() + "]";
}

}
41 changes: 41 additions & 0 deletions terminal/src/main/java/org/jline/terminal/Attributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import java.util.EnumMap;
import java.util.EnumSet;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Attributes {

Expand Down Expand Up @@ -304,4 +306,43 @@ public void copy(Attributes attributes) {
setOutputFlags(attributes.getOutputFlags());
setControlChars(attributes.getControlChars());
}

@Override
public String toString() {
return "Attributes[" +
"lflags: " + append(lflag) + ", " +
"iflags: " + append(iflag) + ", " +
"oflags: " + append(oflag) + ", " +
"cflags: " + append(cflag) + ", " +
"cchars: " + append(EnumSet.allOf(ControlChar.class), this::display) +
"]";
}

private String display(ControlChar c) {
String value;
int ch = getControlChar(c);
if (c == ControlChar.VMIN || c == ControlChar.VTIME) {
value = Integer.toString(ch);
} else if (ch < 0) {
value = "<undef>";
} else if (ch < 32) {
value = "^" + (char) (ch + 'A' - 1);
} else if (ch == 127) {
value = "^?";
} else if (ch >= 128) {
value = String.format("\\u%04x", ch);
} else {
value = String.valueOf((char) ch);
}
return c.name().toLowerCase().substring(1) + "=" + value;
}

private <T extends Enum<T>> String append(EnumSet<T> set) {
return append(set, e -> e.name().toLowerCase());
}

private <T extends Enum<T>> String append(EnumSet<T> set, Function<T, String> toString) {
return set.stream().map(toString).collect(Collectors.joining(" "));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ public String getType() {
return type;
}

public String getKind() {
return getClass().getSimpleName();
}

public void flush() {
writer().flush();
}
Expand Down
5 changes: 5 additions & 0 deletions terminal/src/main/java/org/jline/terminal/impl/ExecPty.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,9 @@ OSUtils.STTY_F_OPTION, getName(),
}
}

@Override
public String toString() {
return "ExecPty[" + getName() + (system ? ", system]" : "]");
}

}

0 comments on commit 50c14de

Please sign in to comment.