From 50c14deeb95477676e64a2c436078f7fcf036004 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 23 Mar 2017 09:13:01 +0100 Subject: [PATCH] Improve toString() for Attributes, Pty --- .../terminal/impl/jansi/JansiNativePty.java | 4 ++ .../jline/terminal/impl/jna/JnaNativePty.java | 6 +++ .../java/org/jline/terminal/Attributes.java | 41 +++++++++++++++++++ .../jline/terminal/impl/AbstractTerminal.java | 4 ++ .../java/org/jline/terminal/impl/ExecPty.java | 5 +++ 5 files changed, 60 insertions(+) diff --git a/terminal-jansi/src/main/java/org/jline/terminal/impl/jansi/JansiNativePty.java b/terminal-jansi/src/main/java/org/jline/terminal/impl/jansi/JansiNativePty.java index 4006c3682..49f1aa39e 100644 --- a/terminal-jansi/src/main/java/org/jline/terminal/impl/jansi/JansiNativePty.java +++ b/terminal-jansi/src/main/java/org/jline/terminal/impl/jansi/JansiNativePty.java @@ -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() + "]"; + } } diff --git a/terminal-jna/src/main/java/org/jline/terminal/impl/jna/JnaNativePty.java b/terminal-jna/src/main/java/org/jline/terminal/impl/jna/JnaNativePty.java index cc5085208..1570c72c5 100644 --- a/terminal-jna/src/main/java/org/jline/terminal/impl/jna/JnaNativePty.java +++ b/terminal-jna/src/main/java/org/jline/terminal/impl/jna/JnaNativePty.java @@ -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() + "]"; + } + } diff --git a/terminal/src/main/java/org/jline/terminal/Attributes.java b/terminal/src/main/java/org/jline/terminal/Attributes.java index 0ecae5eaf..733a77f6b 100644 --- a/terminal/src/main/java/org/jline/terminal/Attributes.java +++ b/terminal/src/main/java/org/jline/terminal/Attributes.java @@ -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 { @@ -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 = ""; + } 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 > String append(EnumSet set) { + return append(set, e -> e.name().toLowerCase()); + } + + private > String append(EnumSet set, Function toString) { + return set.stream().map(toString).collect(Collectors.joining(" ")); + } + } diff --git a/terminal/src/main/java/org/jline/terminal/impl/AbstractTerminal.java b/terminal/src/main/java/org/jline/terminal/impl/AbstractTerminal.java index 6d9bc6bc7..eaba7d6d7 100644 --- a/terminal/src/main/java/org/jline/terminal/impl/AbstractTerminal.java +++ b/terminal/src/main/java/org/jline/terminal/impl/AbstractTerminal.java @@ -120,6 +120,10 @@ public String getType() { return type; } + public String getKind() { + return getClass().getSimpleName(); + } + public void flush() { writer().flush(); } diff --git a/terminal/src/main/java/org/jline/terminal/impl/ExecPty.java b/terminal/src/main/java/org/jline/terminal/impl/ExecPty.java index 29bba5c4e..d2abf28b7 100644 --- a/terminal/src/main/java/org/jline/terminal/impl/ExecPty.java +++ b/terminal/src/main/java/org/jline/terminal/impl/ExecPty.java @@ -294,4 +294,9 @@ OSUtils.STTY_F_OPTION, getName(), } } + @Override + public String toString() { + return "ExecPty[" + getName() + (system ? ", system]" : "]"); + } + }