Skip to content

Commit 4b4b03c

Browse files
committed
chore: moved examples to their own package
1 parent ac18d03 commit 4b4b03c

File tree

6 files changed

+179
-60
lines changed

6 files changed

+179
-60
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
<transformers>
8383
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
8484
<manifestEntries>
85-
<Main-Class>org.codejive.context.LowLevel</Main-Class>
85+
<Main-Class>examples.Boxes</Main-Class>
8686
</manifestEntries>
8787
</transformer>
8888
</transformers>

src/main/java/org/codejive/context/LowLevel.java renamed to src/main/java/examples/Boxes.java

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
package org.codejive.context;
1+
package examples;
2+
3+
import static examples.Util.setBorderWidth;
4+
import static examples.Util.setRandomPosSize;
5+
import static examples.Util.setSize;
26

37
import java.io.IOException;
48
import java.time.Instant;
59
import java.util.Arrays;
610
import org.codejive.context.render.BorderRenderer;
711
import org.codejive.context.render.Box;
812
import org.codejive.context.render.BoxRenderer;
9-
import org.codejive.context.styles.Property;
1013
import org.codejive.context.styles.Style;
11-
import org.codejive.context.styles.Unit;
12-
import org.codejive.context.styles.Value;
1314
import org.codejive.context.terminal.Screen;
1415
import org.codejive.context.terminal.Term;
1516
import org.codejive.context.util.ScrollBuffer;
1617
import org.jline.utils.AttributedString;
1718
import org.jline.utils.AttributedStringBuilder;
1819

19-
public class LowLevel {
20+
public class Boxes {
2021
private final Term term;
2122

22-
public LowLevel(Term term) {
23+
public Boxes(Term term) {
2324
this.term = term;
2425
}
2526

2627
public static void main(String... args) throws IOException {
2728
try (Term terminal = Term.create()) {
28-
new LowLevel(terminal).run();
29+
new Boxes(terminal).run();
2930
}
3031
}
3132

@@ -41,6 +42,7 @@ public int run() throws IOException {
4142
out:
4243
while (true) {
4344
if (refresh) {
45+
screen.clear();
4446
b1 = createColoredBox();
4547
setRandomPosSize(b1, displayWidth, displayHeight);
4648
b2 = createTimerBox(new Style());
@@ -111,36 +113,4 @@ private Box createScrollBox(Style s, ScrollBuffer sb) {
111113
setBorderWidth(b, 1);
112114
return b;
113115
}
114-
115-
private static void setSize(Box b, int w, int h) {
116-
Style s = b.style();
117-
s.put(Property.width, Value.length(w, Unit.em));
118-
s.put(Property.height, Value.length(h, Unit.em));
119-
}
120-
121-
private static void setRandomPosSize(Box b, int totalW, int totalH) {
122-
int minx = -b.width();
123-
int maxx = totalW + b.width();
124-
int miny = -b.height();
125-
int maxy = totalH + b.height();
126-
int x = (int) (Math.random() * (maxx - minx + 1) + minx);
127-
int y = (int) (Math.random() * (maxy - miny + 1) + miny);
128-
setPos(b, x, y);
129-
}
130-
131-
private static void setPos(Box b, int x, int y) {
132-
int w = b.width();
133-
int h = b.height();
134-
Style s = b.style();
135-
s.put(Property.top, Value.length(y, Unit.em));
136-
s.put(Property.bottom, Value.length(y + w - 1, Unit.em));
137-
s.put(Property.left, Value.length(x, Unit.em));
138-
s.put(Property.right, Value.length(x + h - 1, Unit.em));
139-
s.put(Property.width, Value.length(w, Unit.em));
140-
s.put(Property.height, Value.length(h, Unit.em));
141-
}
142-
143-
private static void setBorderWidth(Box b, int w) {
144-
b.style().put(Property.border_width, Value.length(w, Unit.em));
145-
}
146116
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package examples;
2+
3+
import static examples.Util.setBorderWidth;
4+
import static examples.Util.setPos;
5+
import static examples.Util.setSize;
6+
7+
import java.io.IOException;
8+
import java.util.Collections;
9+
import org.codejive.context.render.BorderRenderer;
10+
import org.codejive.context.render.Box;
11+
import org.codejive.context.terminal.Screen;
12+
import org.codejive.context.terminal.Term;
13+
import org.jline.utils.AttributedStringBuilder;
14+
15+
public class FullPanel {
16+
private final Term term;
17+
18+
public FullPanel(Term term) {
19+
this.term = term;
20+
}
21+
22+
public static void main(String... args) throws IOException {
23+
try (Term terminal = Term.create()) {
24+
new FullPanel(terminal).run();
25+
}
26+
}
27+
28+
public int run() throws IOException {
29+
Screen screen = term.fullScreen();
30+
int displayWidth = screen.rect().width();
31+
int displayHeight = screen.rect().height();
32+
33+
Box b = createBox(displayWidth, displayHeight);
34+
BorderRenderer br = new BorderRenderer(screen);
35+
br.render(b);
36+
37+
AttributedStringBuilder cb = new AttributedStringBuilder();
38+
cb.append(displayWidth + "x" + displayHeight);
39+
screen.printAt(
40+
displayWidth / 2 - cb.length() / 2, displayHeight / 2, cb.toAttributedString());
41+
42+
screen.update();
43+
term.input().readChar();
44+
45+
return 0;
46+
}
47+
48+
private Box createBox(int w, int h) {
49+
Box b = new Box(Collections.emptyList());
50+
setSize(b, w - 2, h - 2);
51+
setPos(b, 1, 1);
52+
setBorderWidth(b, 1);
53+
return b;
54+
}
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package examples;
2+
3+
import static examples.Util.setBorderWidth;
4+
import static examples.Util.setPos;
5+
import static examples.Util.setSize;
6+
7+
import java.io.IOException;
8+
import java.util.Collections;
9+
import org.codejive.context.render.BorderRenderer;
10+
import org.codejive.context.render.Box;
11+
import org.codejive.context.terminal.Screen;
12+
import org.codejive.context.terminal.Term;
13+
import org.jline.utils.AttributedStringBuilder;
14+
15+
public class InlinePanel {
16+
private final Term term;
17+
18+
public InlinePanel(Term term) {
19+
this.term = term;
20+
}
21+
22+
public static void main(String... args) throws IOException {
23+
try (Term terminal = Term.create()) {
24+
new InlinePanel(terminal).run();
25+
}
26+
}
27+
28+
public int run() throws IOException {
29+
Screen screen = term.sizedScreen(term.size().width(), 10);
30+
int displayWidth = screen.rect().width();
31+
int displayHeight = screen.rect().height();
32+
33+
Box b = createBox(displayWidth, displayHeight);
34+
BorderRenderer br = new BorderRenderer(screen);
35+
br.render(b);
36+
37+
AttributedStringBuilder cb = new AttributedStringBuilder();
38+
cb.append(displayWidth + "x" + displayHeight);
39+
screen.printAt(
40+
displayWidth / 2 - cb.length() / 2, displayHeight / 2, cb.toAttributedString());
41+
42+
screen.update();
43+
term.input().readChar();
44+
45+
return 0;
46+
}
47+
48+
private Box createBox(int w, int h) {
49+
Box b = new Box(Collections.emptyList());
50+
setSize(b, w - 2, h - 2);
51+
setPos(b, 1, 1);
52+
setBorderWidth(b, 1);
53+
return b;
54+
}
55+
}
Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
package org.codejive.context;
1+
package examples;
2+
3+
import static examples.Util.pos;
4+
import static examples.Util.size;
25

36
import java.io.IOException;
47
import java.util.List;
@@ -8,21 +11,19 @@
811
import org.codejive.context.ciml.layout.DomLayouter;
912
import org.codejive.context.render.Box;
1013
import org.codejive.context.render.BoxRenderer;
11-
import org.codejive.context.styles.Property;
12-
import org.codejive.context.styles.Style;
1314
import org.codejive.context.terminal.Screen;
1415
import org.codejive.context.terminal.Term;
1516

16-
public class DomLevel {
17+
public class SimpleDom {
1718
private final Term term;
1819

19-
public DomLevel(Term term) {
20+
public SimpleDom(Term term) {
2021
this.term = term;
2122
}
2223

2324
public static void main(String... args) throws IOException {
2425
try (Term terminal = Term.create()) {
25-
new LowLevel(terminal).run();
26+
new SimpleDom(terminal).run();
2627
}
2728
}
2829

@@ -46,18 +47,4 @@ public int run() throws IOException {
4647

4748
return 0;
4849
}
49-
50-
private Style pos(int x, int y) {
51-
Style s = new Style();
52-
s.putAsEmInt(Property.left, x);
53-
s.putAsEmInt(Property.top, y);
54-
return s;
55-
}
56-
57-
private Style size(int w, int h) {
58-
Style s = new Style();
59-
s.putAsEmInt(Property.width, w);
60-
s.putAsEmInt(Property.height, h);
61-
return s;
62-
}
6350
}

src/main/java/examples/Util.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package examples;
2+
3+
import org.codejive.context.render.Box;
4+
import org.codejive.context.styles.Property;
5+
import org.codejive.context.styles.Style;
6+
import org.codejive.context.styles.Unit;
7+
import org.codejive.context.styles.Value;
8+
9+
class Util {
10+
11+
static Style pos(int x, int y) {
12+
Style s = new Style();
13+
s.putAsEmInt(Property.left, x);
14+
s.putAsEmInt(Property.top, y);
15+
return s;
16+
}
17+
18+
static Style size(int w, int h) {
19+
Style s = new Style();
20+
s.putAsEmInt(Property.width, w);
21+
s.putAsEmInt(Property.height, h);
22+
return s;
23+
}
24+
25+
static void setSize(Box b, int w, int h) {
26+
b.style().and(size(w, h));
27+
}
28+
29+
static void setRandomPosSize(Box b, int totalW, int totalH) {
30+
int minx = -b.width();
31+
int maxx = totalW + b.width();
32+
int miny = -b.height();
33+
int maxy = totalH + b.height();
34+
int x = (int) (Math.random() * (maxx - minx + 1) + minx);
35+
int y = (int) (Math.random() * (maxy - miny + 1) + miny);
36+
setPos(b, x, y);
37+
}
38+
39+
static void setPos(Box b, int x, int y) {
40+
int w = b.width();
41+
int h = b.height();
42+
Style s = b.style().and(pos(x, y));
43+
s.put(Property.bottom, Value.length(y + w - 1, Unit.em));
44+
s.put(Property.right, Value.length(x + h - 1, Unit.em));
45+
s.put(Property.width, Value.length(w, Unit.em));
46+
s.put(Property.height, Value.length(h, Unit.em));
47+
}
48+
49+
static void setBorderWidth(Box b, int w) {
50+
b.style().put(Property.border_width, Value.length(w, Unit.em));
51+
}
52+
}

0 commit comments

Comments
 (0)