Initial commit
[jvcard.git] / src / be / nikiroo / jvcard / tui / StringUtils.java
1 package be.nikiroo.jvcard.tui;
2
3 import com.googlecode.lanterna.gui2.LinearLayout.Alignment;
4
5 public class StringUtils {
6
7 static public String padString(String text, int width) {
8 return padString(text, width, true, Alignment.Beginning);
9 }
10
11 // TODO: doc it, width of -1 == no change to text
12 static public String padString(String text, int width, boolean cut,
13 Alignment align) {
14
15 if (width >= 0) {
16 if (text == null)
17 text = "";
18
19 int diff = width - text.length();
20
21 if (diff < 0) {
22 if (cut)
23 text = text.substring(0, width);
24 } else if (diff > 0) {
25 if (diff < 2 && align != Alignment.End)
26 align = Alignment.Beginning;
27
28 switch (align) {
29 case Beginning:
30 text = text + new String(new char[diff]).replace('\0', ' ');
31 break;
32 case End:
33 text = new String(new char[diff]).replace('\0', ' ') + text;
34 break;
35 case Center:
36 case Fill:
37 default:
38 int pad1 = (diff) / 2;
39 int pad2 = (diff + 1) / 2;
40 text = new String(new char[pad1]).replace('\0', ' ') + text
41 + new String(new char[pad2]).replace('\0', ' ');
42 break;
43 }
44 }
45 }
46
47 return text;
48 }
49
50 }