Skip to content

Commit

Permalink
Add a property to customize the tab width (fixes #861) (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Oct 24, 2023
1 parent 77283f8 commit 2af16d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions reader/src/main/java/org/jline/reader/LineReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ public interface LineReader {
*/
String MAX_REPEAT_COUNT = "max-repeat-count";

/**
* Number of spaces to display a tabulation, the default is 4.
*/
String TAB_WIDTH = "tab-width";

Map<String, KeyMap<Binding>> defaultKeyMaps();

enum Option {
Expand Down
18 changes: 14 additions & 4 deletions reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@
public class LineReaderImpl implements LineReader, Flushable {
public static final char NULL_MASK = 0;

/**
* @deprecated use {@link #DEFAULT_TAB_WIDTH} and {@link #getTabWidth()}
*/
@Deprecated
public static final int TAB_WIDTH = 4;

public static final int DEFAULT_TAB_WIDTH = 4;

public static final String DEFAULT_WORDCHARS = "*?_-.[]~=/&;!#$%^(){}<>";
public static final String DEFAULT_REMOVE_SUFFIX_CHARS = " \t\n;&|";
public static final String DEFAULT_COMMENT_BEGIN = "#";
Expand Down Expand Up @@ -1111,6 +1117,10 @@ public void editAndAddInBuffer(File file) throws Exception {
}
}

protected int getTabWidth() {
return getInt(LineReader.TAB_WIDTH, DEFAULT_TAB_WIDTH);
}

//
// Widget implementation
//
Expand Down Expand Up @@ -3844,7 +3854,7 @@ protected void redisplay(boolean flush) {
}

if (size.getRows() > 0 && size.getRows() < MIN_ROWS) {
AttributedStringBuilder sb = new AttributedStringBuilder().tabs(TAB_WIDTH);
AttributedStringBuilder sb = new AttributedStringBuilder().tabs(getTabWidth());

sb.append(prompt);
concat(getHighlightedBuffer(buf.toString()).columnSplitLength(Integer.MAX_VALUE), sb);
Expand Down Expand Up @@ -3917,7 +3927,7 @@ protected void redisplay(boolean flush) {
int cursorNewLinesId = -1;
int cursorColPos = -1;
if (size.getColumns() > 0) {
AttributedStringBuilder sb = new AttributedStringBuilder().tabs(TAB_WIDTH);
AttributedStringBuilder sb = new AttributedStringBuilder().tabs(getTabWidth());
sb.append(prompt);
String buffer = buf.upToCursor();
if (maskingCallback != null) {
Expand Down Expand Up @@ -4024,7 +4034,7 @@ public AttributedString getDisplayedBufferWithPrompts(List<AttributedString> sec
AttributedString attBuf = getHighlightedBuffer(buf.toString());

AttributedString tNewBuf = insertSecondaryPrompts(attBuf, secondaryPrompts);
AttributedStringBuilder full = new AttributedStringBuilder().tabs(TAB_WIDTH);
AttributedStringBuilder full = new AttributedStringBuilder().tabs(getTabWidth());
full.append(prompt);
full.append(tNewBuf);
if (doAutosuggestion && !isTerminalDumb()) {
Expand Down Expand Up @@ -5835,7 +5845,7 @@ public boolean mouse() {
List<AttributedString> secondaryPrompts = new ArrayList<>();
getDisplayedBufferWithPrompts(secondaryPrompts);

AttributedStringBuilder sb = new AttributedStringBuilder().tabs(TAB_WIDTH);
AttributedStringBuilder sb = new AttributedStringBuilder().tabs(getTabWidth());
sb.append(prompt);
sb.append(insertSecondaryPrompts(new AttributedString(buf.upToCursor()), secondaryPrompts, false));
List<AttributedString> promptLines =
Expand Down

0 comments on commit 2af16d8

Please sign in to comment.