Fix UTF8 bug, create first executable JAR file
[jvcard.git] / src / com / googlecode / lanterna / screen / TabBehaviour.java
... / ...
CommitLineData
1/*
2 * This file is part of lanterna (http://code.google.com/p/lanterna/).
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 * Copyright (C) 2010-2015 Martin
18 */
19package com.googlecode.lanterna.screen;
20
21/**
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.
25 *
26 * @author martin
27 */
28public enum TabBehaviour {
29 /**
30 * Tab characters are not replaced, this will probably have undefined and weird behaviour!
31 */
32 IGNORE(null, null),
33 /**
34 * Tab characters are replaced with a single blank space, no matter where the tab was placed.
35 */
36 CONVERT_TO_ONE_SPACE(1, null),
37 /**
38 * Tab characters are replaced with two blank spaces, no matter where the tab was placed.
39 */
40 CONVERT_TO_TWO_SPACES(2, null),
41 /**
42 * Tab characters are replaced with three blank spaces, no matter where the tab was placed.
43 */
44 CONVERT_TO_THREE_SPACES(3, null),
45 /**
46 * Tab characters are replaced with four blank spaces, no matter where the tab was placed.
47 */
48 CONVERT_TO_FOUR_SPACES(4, null),
49 /**
50 * Tab characters are replaced with eight blank spaces, no matter where the tab was placed.
51 */
52 CONVERT_TO_EIGHT_SPACES(8, null),
53 /**
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.
56 */
57 ALIGN_TO_COLUMN_4(null, 4),
58 /**
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.
61 */
62 ALIGN_TO_COLUMN_8(null, 8),
63 ;
64
65 private final Integer replaceFactor;
66 private final Integer alignFactor;
67
68 TabBehaviour(Integer replaceFactor, Integer alignFactor) {
69 this.replaceFactor = replaceFactor;
70 this.alignFactor = alignFactor;
71 }
72
73 /**
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
79 */
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);
87 }
88 return string;
89 }
90
91 /**
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
95 */
96 public String getTabReplacement(int columnIndex) {
97 int replaceCount;
98 StringBuilder replace = new StringBuilder();
99 if(replaceFactor != null) {
100 replaceCount = replaceFactor;
101 }
102 else if (alignFactor != null) {
103 replaceCount = alignFactor - (columnIndex % alignFactor);
104 }
105 else {
106 return "\t";
107 }
108 for(int i = 0; i < replaceCount; i++) {
109 replace.append(" ");
110 }
111 return replace.toString();
112 }
113}