From 3091bb196580c020bd4c118f4f1ac75021e9732d Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Tue, 10 Dec 2024 16:42:50 +0100 Subject: [PATCH] Enhance DefaultHighlighter to support command styling --- .../java/org/jline/reader/impl/DefaultHighlighter.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/reader/src/main/java/org/jline/reader/impl/DefaultHighlighter.java b/reader/src/main/java/org/jline/reader/impl/DefaultHighlighter.java index b57b4ff08..6f08ea9e8 100644 --- a/reader/src/main/java/org/jline/reader/impl/DefaultHighlighter.java +++ b/reader/src/main/java/org/jline/reader/impl/DefaultHighlighter.java @@ -38,6 +38,7 @@ public AttributedString highlight(LineReader reader, String buffer) { int underlineEnd = -1; int negativeStart = -1; int negativeEnd = -1; + boolean first = true; String search = reader.getSearchTerm(); if (search != null && search.length() > 0) { underlineStart = buffer.indexOf(search); @@ -65,6 +66,7 @@ public AttributedString highlight(LineReader reader, String buffer) { } AttributedStringBuilder sb = new AttributedStringBuilder(); + commandStyle(reader, sb, true); for (int i = 0; i < buffer.length(); i++) { if (i == underlineStart) { sb.style(AttributedStyle::underline); @@ -77,6 +79,10 @@ public AttributedString highlight(LineReader reader, String buffer) { } char c = buffer.charAt(i); + if (first && Character.isSpaceChar(c)) { + first = false; + commandStyle(reader, sb, false); + } if (c == '\t' || c == '\n') { sb.append(c); } else if (c < 32) { @@ -105,4 +111,6 @@ public AttributedString highlight(LineReader reader, String buffer) { } return sb.toAttributedString(); } + + protected void commandStyle(LineReader reader, AttributedStringBuilder sb, boolean enable) {} }