Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a property to customize the tab width (fixes #861) #880

Merged
merged 4 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading