2 * This file is part of lanterna (http://code.google.com/p/lanterna/).
4 * lanterna is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * Copyright (C) 2010-2015 Martin
19 package com
.googlecode
.lanterna
.screen
;
22 * What to do about the tab character when putting on a {@code Screen}. Since tabs are a bit special, their meaning
23 * depends on which column the cursor is in when it's printed, we'll need to have some way to tell the Screen what to
24 * do when encountering a tab character.
28 public enum TabBehaviour
{
30 * Tab characters are not replaced, this will probably have undefined and weird behaviour!
34 * Tab characters are replaced with a single blank space, no matter where the tab was placed.
36 CONVERT_TO_ONE_SPACE(1, null),
38 * Tab characters are replaced with two blank spaces, no matter where the tab was placed.
40 CONVERT_TO_TWO_SPACES(2, null),
42 * Tab characters are replaced with three blank spaces, no matter where the tab was placed.
44 CONVERT_TO_THREE_SPACES(3, null),
46 * Tab characters are replaced with four blank spaces, no matter where the tab was placed.
48 CONVERT_TO_FOUR_SPACES(4, null),
50 * Tab characters are replaced with eight blank spaces, no matter where the tab was placed.
52 CONVERT_TO_EIGHT_SPACES(8, null),
54 * Tab characters are replaced with enough space characters to reach the next column index that is evenly divisible
55 * by 4, simulating a normal tab character when placed inside a text document.
57 ALIGN_TO_COLUMN_4(null, 4),
59 * Tab characters are replaced with enough space characters to reach the next column index that is evenly divisible
60 * by 8, simulating a normal tab character when placed inside a text document.
62 ALIGN_TO_COLUMN_8(null, 8),
65 private final Integer replaceFactor
;
66 private final Integer alignFactor
;
68 TabBehaviour(Integer replaceFactor
, Integer alignFactor
) {
69 this.replaceFactor
= replaceFactor
;
70 this.alignFactor
= alignFactor
;
74 * Given a string, being placed on the screen at column X, returns the same string with all tab characters (\t)
75 * replaced according to this TabBehaviour.
76 * @param string String that is going to be put to the screen, potentially containing tab characters
77 * @param columnIndex Column on the screen where the first character of the string is going to end up
78 * @return The input string with all tab characters replaced with spaces, according to this TabBehaviour
80 public String
replaceTabs(String string
, int columnIndex
) {
81 int tabPosition
= string
.indexOf('\t');
82 while(tabPosition
!= -1) {
83 String tabReplacementHere
= getTabReplacement(columnIndex
+ tabPosition
);
84 string
= string
.substring(0, tabPosition
) + tabReplacementHere
+ string
.substring(tabPosition
+ 1);
85 tabPosition
+= tabReplacementHere
.length();
86 tabPosition
= string
.indexOf('\t', tabPosition
);
92 * Returns the String that can replace a tab at the specified position, according to this TabBehaviour.
93 * @param columnIndex Column index of where the tab character is placed
94 * @return String consisting of 1 or more space character
96 public String
getTabReplacement(int columnIndex
) {
98 StringBuilder replace
= new StringBuilder();
99 if(replaceFactor
!= null) {
100 replaceCount
= replaceFactor
;
102 else if (alignFactor
!= null) {
103 replaceCount
= alignFactor
- (columnIndex
% alignFactor
);
108 for(int i
= 0; i
< replaceCount
; i
++) {
111 return replace
.toString();