Skip to content

Commit 2af16d8

Browse files
authoredOct 24, 2023
Add a property to customize the tab width (fixes #861) (#880)
1 parent 77283f8 commit 2af16d8

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed
 

‎reader/src/main/java/org/jline/reader/LineReader.java

+5
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ public interface LineReader {
401401
*/
402402
String MAX_REPEAT_COUNT = "max-repeat-count";
403403

404+
/**
405+
* Number of spaces to display a tabulation, the default is 4.
406+
*/
407+
String TAB_WIDTH = "tab-width";
408+
404409
Map<String, KeyMap<Binding>> defaultKeyMaps();
405410

406411
enum Option {

‎reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,14 @@
7070
public class LineReaderImpl implements LineReader, Flushable {
7171
public static final char NULL_MASK = 0;
7272

73+
/**
74+
* @deprecated use {@link #DEFAULT_TAB_WIDTH} and {@link #getTabWidth()}
75+
*/
76+
@Deprecated
7377
public static final int TAB_WIDTH = 4;
7478

79+
public static final int DEFAULT_TAB_WIDTH = 4;
80+
7581
public static final String DEFAULT_WORDCHARS = "*?_-.[]~=/&;!#$%^(){}<>";
7682
public static final String DEFAULT_REMOVE_SUFFIX_CHARS = " \t\n;&|";
7783
public static final String DEFAULT_COMMENT_BEGIN = "#";
@@ -1111,6 +1117,10 @@ public void editAndAddInBuffer(File file) throws Exception {
11111117
}
11121118
}
11131119

1120+
protected int getTabWidth() {
1121+
return getInt(LineReader.TAB_WIDTH, DEFAULT_TAB_WIDTH);
1122+
}
1123+
11141124
//
11151125
// Widget implementation
11161126
//
@@ -3844,7 +3854,7 @@ protected void redisplay(boolean flush) {
38443854
}
38453855

38463856
if (size.getRows() > 0 && size.getRows() < MIN_ROWS) {
3847-
AttributedStringBuilder sb = new AttributedStringBuilder().tabs(TAB_WIDTH);
3857+
AttributedStringBuilder sb = new AttributedStringBuilder().tabs(getTabWidth());
38483858

38493859
sb.append(prompt);
38503860
concat(getHighlightedBuffer(buf.toString()).columnSplitLength(Integer.MAX_VALUE), sb);
@@ -3917,7 +3927,7 @@ protected void redisplay(boolean flush) {
39173927
int cursorNewLinesId = -1;
39183928
int cursorColPos = -1;
39193929
if (size.getColumns() > 0) {
3920-
AttributedStringBuilder sb = new AttributedStringBuilder().tabs(TAB_WIDTH);
3930+
AttributedStringBuilder sb = new AttributedStringBuilder().tabs(getTabWidth());
39213931
sb.append(prompt);
39223932
String buffer = buf.upToCursor();
39233933
if (maskingCallback != null) {
@@ -4024,7 +4034,7 @@ public AttributedString getDisplayedBufferWithPrompts(List<AttributedString> sec
40244034
AttributedString attBuf = getHighlightedBuffer(buf.toString());
40254035

40264036
AttributedString tNewBuf = insertSecondaryPrompts(attBuf, secondaryPrompts);
4027-
AttributedStringBuilder full = new AttributedStringBuilder().tabs(TAB_WIDTH);
4037+
AttributedStringBuilder full = new AttributedStringBuilder().tabs(getTabWidth());
40284038
full.append(prompt);
40294039
full.append(tNewBuf);
40304040
if (doAutosuggestion && !isTerminalDumb()) {
@@ -5835,7 +5845,7 @@ public boolean mouse() {
58355845
List<AttributedString> secondaryPrompts = new ArrayList<>();
58365846
getDisplayedBufferWithPrompts(secondaryPrompts);
58375847

5838-
AttributedStringBuilder sb = new AttributedStringBuilder().tabs(TAB_WIDTH);
5848+
AttributedStringBuilder sb = new AttributedStringBuilder().tabs(getTabWidth());
58395849
sb.append(prompt);
58405850
sb.append(insertSecondaryPrompts(new AttributedString(buf.upToCursor()), secondaryPrompts, false));
58415851
List<AttributedString> promptLines =

0 commit comments

Comments
 (0)
Please sign in to comment.