src/main/java/de/unixwork/uwproj/Feature.java

Sun, 28 Jan 2024 13:22:02 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 28 Jan 2024 13:22:02 +0100
changeset 111
cb128bae1161
parent 98
4bf47ea3fc64
child 113
24f32dbd88cd
permissions
-rw-r--r--

add shortcut for installation to home

0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 package de.unixwork.uwproj;
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 import org.w3c.dom.Element;
97
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
4 import org.w3c.dom.Node;
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
5 import org.w3c.dom.NodeList;
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6
40
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
7 import java.util.List;
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
8
41
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
9 import static de.unixwork.uwproj.Util.isNotNullOrBlank;
45
f4ec2feccebb add missing shId() in feature var name
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
10 import static de.unixwork.uwproj.Util.shId;
41
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
11
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 public class Feature {
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 private String name;
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 private String arg;
79
ef0635b78b7b remove explicit calls of getters in velocity template
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
15 private boolean auto;
97
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
16 private String desc;
41
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
17
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 private TargetData targetData;
41
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
19
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 public static Feature parse(Project project, Element e) throws Exception {
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 Feature feature = new Feature();
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 String name = e.getAttribute("name");
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 String arg = e.getAttribute("arg");
41
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
24
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
25 if (name.isBlank()) {
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 throw new Exception("feature element requires name attribute");
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 }
41
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
28
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 feature.setName(name);
41
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
30 if (arg.isBlank()) {
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 feature.setArg(name);
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 } else {
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 feature.setArg(arg);
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 }
41
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
35
97
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
36 feature.setAuto(Boolean.parseBoolean(e.getAttribute("default")));
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 feature.setTargetData(TargetData.parse(e));
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 project.addFeature(feature);
97
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
39
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
40 // TODO: when Option also receives this feature, we might move this to TargetData.parse()
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
41 NodeList nodes = e.getChildNodes();
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
42 for (int i = 0; i < nodes.getLength(); i++) {
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
43 Node node = nodes.item(i);
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
44 if (node.getNodeType() == Node.ELEMENT_NODE) {
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
45 Element elm = (Element) node;
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
46 String n = elm.getNodeName();
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
47 if (n.equals("desc")) {
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
48 feature.setDesc(Util.getContent(elm));
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
49 }
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
50 }
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
51 }
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
52
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 return feature;
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 }
41
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
55
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 public String getVarName() {
45
f4ec2feccebb add missing shId() in feature var name
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
57 return shId("FEATURE_" + name.toUpperCase());
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 }
41
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
59
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 public List<String> getDependencies() {
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 return getTargetData().getDependencies();
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 }
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 public List<Define> getDefines() {
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 return getTargetData().getDefines();
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 }
41
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
67
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 public String getMake() {
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 return targetData.getMake();
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 }
41
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
71
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 public boolean hasMake() {
41
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
73 return isNotNullOrBlank(targetData.getMake());
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 }
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 public String getName() {
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 return name;
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 }
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 public void setName(String name) {
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 this.name = name;
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 }
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 public String getArg() {
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 return arg;
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 }
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 public void setArg(String arg) {
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 this.arg = arg;
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 }
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91
97
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
92 public String getDesc() {
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
93 return desc;
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
94 }
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
95
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
96 public void setDesc(String desc) {
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
97 this.desc = desc;
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
98 }
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
99
79
ef0635b78b7b remove explicit calls of getters in velocity template
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
100 public boolean isAuto() {
ef0635b78b7b remove explicit calls of getters in velocity template
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
101 return auto;
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 }
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103
79
ef0635b78b7b remove explicit calls of getters in velocity template
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
104 public void setAuto(boolean auto) {
ef0635b78b7b remove explicit calls of getters in velocity template
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
105 this.auto = auto;
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 }
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 public TargetData getTargetData() {
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 return targetData;
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 }
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 public void setTargetData(TargetData targetData) {
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 this.targetData = targetData;
41
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
114 }
97
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
115
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
116 /**
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
117 * Generates help text for the feature option.
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
118 * <p>
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
119 * If {@link #isAuto()} returns true, the option name will be --disable-${@link #getArg()}, otherwise
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
120 * it will be --enable-${@link #getArg()}, preceded by two spaces, respectively.
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
121 * <p>
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
122 * If no description is available via {@link #getDesc()}, only the option name is generated. Otherwise,
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
123 * description is added according to the following rules:
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
124 * <p>
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
125 * When the option name does not consume more than 25 characters, the description starts in the same line.
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
126 * Otherwise, a line break is added, first. The description will be placed in a block starting from column
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
127 * 27 to 80 and automatically break when necessary. The description must not contain a single word
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
128 * that is longer than 54 characters, or it will break the layout.
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
129 *
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
130 * @return a help text for terminal output
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
131 */
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
132 public String getHelpText() {
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
133 final var builder = new StringBuilder();
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
134
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
135 // Compute option name
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
136 final var opt = (auto ? " --disable-" : " --enable-")+arg;
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
137
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
138 // Add option name
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
139 builder.append(opt);
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
140
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
141 // Stop, if there is no description
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
142 if (desc == null || desc.isBlank()) return builder.toString();
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
143
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
144 // Prepare the description by replacing some unwanted spaces
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
145 final var hdesc = desc.trim()
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
146 .replaceAll("\\r", "")
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
147 .replaceAll("\\t", " ");
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
148
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
149 // Declare our frame where the description shall be placed
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
150 final int startx = 26;
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
151 final int endx = 80;
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
152
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
153 // Move to startx (break, if we already past that)
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
154 if (opt.length() >= startx) {
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
155 builder.append("\n");
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
156 builder.append(" ".repeat(startx));
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
157 } else {
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
158 builder.append(" ".repeat(startx-opt.length()));
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
159 }
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
160
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
161 // Append the description keeping the layout intact
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
162 int x = startx;
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
163 for (int i = 0 ; i < hdesc.length() ;) {
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
164 // get the next word and see where we would end
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
165 int s = hdesc.indexOf(' ', i);
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
166 if (s < 0) s = hdesc.length();
98
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
167 int n = hdesc.indexOf('\n', i);
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
168 if (n < 0) n = hdesc.length();
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
169 s = Math.min(s, n);
97
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
170 int l = s-i;
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
171 if (x + l > endx) {
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
172 // does not fit, break into next line
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
173 builder.append('\n');
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
174 builder.append(" ".repeat(startx));
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
175 x = startx;
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
176 }
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
177 // append the word
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
178 builder.append(hdesc, i, s);
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
179 x += l;
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
180 i += l;
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
181 // append the next spaces
98
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
182 while (i < hdesc.length()) {
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
183 int c = hdesc.charAt(i);
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
184 if (c == ' ') {
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
185 // as long as we don't need to break, add the spaces
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
186 i++;
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
187 x++;
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
188 if (x < endx) builder.append(' ');
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
189 } else if (c == '\n') {
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
190 // if user wants us to break, comply
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
191 i++;
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
192 // if we have still space, just move to the end of the line
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
193 if (x < endx) {
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
194 x = endx;
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
195 } else {
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
196 // otherwise, we need to add an extra blank line
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
197 builder.append('\n');
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
198 }
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
199 } else {
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
200 // we have found the next word, so continue with the outer loop
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
201 break;
4bf47ea3fc64 allow custom line breaks in help text
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
202 }
97
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
203 }
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
204 // break to new line, when spaces moved us outside the frame
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
205 if (x > endx) {
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
206 builder.append('\n');
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
207 builder.append(" ".repeat(startx));
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
208 x = startx;
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
209 }
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
210 }
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
211
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
212 return builder.toString();
1af1bfacd25a add helptext descriptions for features
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
213 }
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 }

mercurial