Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
string_box
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christopher Bohn
string_box
Commits
a0254273
Commit
a0254273
authored
Feb 24, 2020
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Added README
parent
fb57f2ea
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+234
-0
234 additions, 0 deletions
README.md
src/main/java/edu/unl/cse/bohn/StringBox.java
+5
-3
5 additions, 3 deletions
src/main/java/edu/unl/cse/bohn/StringBox.java
src/test/java/edu/unl/cse/bohn/StringBoxTest.java
+2
-1
2 additions, 1 deletion
src/test/java/edu/unl/cse/bohn/StringBoxTest.java
with
241 additions
and
4 deletions
README.md
0 → 100644
+
234
−
0
View file @
a0254273
# String Box
StringBox is very simple terminal-based display. Can be used to place arbitrary strings in arbitrary locations in a
terminal window. StringBox will
*not*
redraw the screen. Instead, it will produce a string that, when printed, will
fill the screen, causing the screen's previous contents to scroll off of the screen. StringBox also is not designed to
respond to events; however, you may create an Observer that aggregates a StringBox, producing a similar result.
The default StringBox is 23×80 which, when printed with
`System.out.println()`
, will fill a standard 24×80 window except
for the last line and will place the cursor at the start of the last line for user input. You may consider creating a
24x80 StringBox with the two-argument constructor and print it with
`System.out.print()`
, which will fill a standard
24x80 window entirely and place the cursor at the end of the last line for user input.
A StringBox is populated with repeated calls to
`placeString()`
and its related methods,
`placeStringAlignTopLeft()`
,
`placeStringAlignTopRight()`
,
`placeStringAlignBottomLeft()`
, and
`placeStringAlignBottomRight()`
. The four latter
methods allow you to specify the top/bottom alignment and left/right justification. Since we expect that anchoring
a string with its upper-left corner to be the common case, the shorter-named method
`placeString()`
defaults to
that behavior. Each of the
`placeString*()`
methods returns its StringBox object, which allows multiple
`placeString*()`
calls to be chained. After you have constructed the screen to be displayed, a call to
`toString()`
will produce a string
suitable for printing.
You may pass multi-line strings to the
`placeString*()`
methods, and the result would be the equivalent of making
several
`placeString*()`
calls with one-line strings with the same left/right alignment column and adjacent rows.
Tab characters are converted to four spaces, which may be problematic if you rely on tabs in the middle of strings to
perform alignment. We recommend that you either use separate calls to
`placeString*()`
to achieve this alignment, or
use other methods that operate on Strings to provide alignment.
## Examples
For conciseness, these examples assume a 12×25 StringBox object called
`stringBox`
. Note that the row/column numbering
and the horizontal/vertical dividers are for illustration only and are not part of the generated string.
### Basic Usage
We can place a single line on the screen:
```
stringBox.placeString("foo",2,10);
```
produces
```
111111111122222
0123456789012345678901234
_________________________
0 |
1 |
2 | foo
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
```
### Multi-line
Multiple-line strings can be placed either as a single multi-line string or as multiple single-line strings:
```
stringBox.placeString("foo\nbar baz",2,10);
```
is equivalent to
```
stringBox.placeString("foo",2,10).placeString("bar baz",3,10);
```
as they both produce
```
111111111122222
0123456789012345678901234
_________________________
0 |
1 |
2 | foo
3 | bar baz
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
```
### Right-Justification
Inserted strings can be right-justified instead of left-justified:
```
stringBox.placeStringAlignTopRight("foo\nbar baz",2,10);
```
produces
```
111111111122222
0123456789012345678901234
_________________________
0 |
1 |
2 | foo
3 | bar baz
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
```
### Bottom-Aligned
You can specify the bottom row for an inserted string instead of its top row:
```
stringBox.placeStringAlignBottomLeft("foo\nbar baz",2,10);
```
produces
```
111111111122222
0123456789012345678901234
_________________________
0 |
1 | foo
2 | bar baz
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
```
### Emojis
Many emojis occupy more than one horizontal space; however, this is not a problem for StringBox because those emojis
are also actually multiple characters:
```
stringBox.placeStringAlignTopLeft("foo\n_😄_",2,10);
```
which is equivalent to
```
stringBox.placeStringAlignTopLeft("foo\n_\uD83D\uDE04_",2,10);
```
or, using the
[
com.vdurmont.emoji-java
](
https://github.com/vdurmont/emoji-java
)
library:
```
stringBox.placeStringAlignTopLeft(EmojiParser.parseToUnicode("foo\n_:smile:_"),2,10);
```
produces
```
111111111122222
0123456789012345678901234
_________________________
0 |
1 |
2 | foo
3 | _😄_
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
```
That these emojis will occupy multiple spaces will only be an issue if you rely on them occupying but one space.
### Text Outside the Borders
Any text that extends beyond the right-most column or the bottom-most row, or occupies a negative row or a negative
column, will be silently truncated:
```
stringBox.placeString("foo\nbar baz",-1,-1);
```
```
111111111122222
0123456789012345678901234
_________________________
0 |ar baz
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
```
### Interaction Among Inserted Strings
If two inserted strings overlap, the string inserted last will overwrite a portion of the string written first:
```
stringBox.placeStringAlignTopLeft("foo\nbar", 2, 10)
.placeStringAlignBottomLeft("larry\ncurly\nmoe", 5, 6)
.placeStringAlignTopRight("quux\nxyzzy", 3, 20)
.placeStringAlignBottomRight("one\ntwo\nthree", 10, 15);
```
```
111111111122222
0123456789012345678901234
_________________________
0 |
1 |
2 | foo
3 | larryar quux
4 | curly xyzzy
5 | moe
6 |
7 |
8 | one
9 | two
10 | three
11 |
```
---
String Box © 2020 Christopher Bohn, bohn@unl.edu
This diff is collapsed.
Click to expand it.
src/main/java/edu/unl/cse/bohn/StringBox.java
+
5
−
3
View file @
a0254273
/*
*
s
tring
_b
ox Copyright (c) 2020 Christopher Bohn, bohn@unl.edu
*
S
tring
B
ox Copyright (c) 2020 Christopher Bohn, bohn@unl.edu
*/
package
edu.unl.cse.bohn
;
...
...
@@ -19,6 +19,8 @@ package edu.unl.cse.bohn;
* .toString();
* System.out.println(screen);
* </code></pre>
*
* <p>Note that StringBox is <i>not</i> robust to hidden characters, such as VT100 escape sequences.</p>
*/
public
class
StringBox
{
private
int
maximumWidth
;
...
...
@@ -28,7 +30,7 @@ public class StringBox {
private
StringRow
[]
rows
;
/**
* <p>Produces a StringBox suitably-sized for a standard 24
x
80 terminal. The StringBox will be 23
x
80; if the string
* <p>Produces a StringBox suitably-sized for a standard 24
×
80 terminal. The StringBox will be 23
×
80; if the string
* is printed with <code>System.out.println()</code> then it will leave the cursor on the 24th line, where the
* user can enter their input without scrolling the top of the string off the screen.</p>
*
...
...
@@ -40,7 +42,7 @@ public class StringBox {
* @see #StringBox(int, int)
*/
public
StringBox
()
{
this
(
23
,
80
);
// standard terminal is 24
x
80, but leave room for the user's input
this
(
23
,
80
);
// standard terminal is 24
×
80, but leave room for the user's input
}
/**
...
...
This diff is collapsed.
Click to expand it.
src/test/java/edu/unl/cse/bohn/StringBoxTest.java
+
2
−
1
View file @
a0254273
/*
*
s
tring
_b
ox Copyright (c) 2020 Christopher Bohn, bohn@unl.edu
*
S
tring
B
ox Copyright (c) 2020 Christopher Bohn, bohn@unl.edu
*/
package
edu.unl.cse.bohn
;
...
...
@@ -116,6 +116,7 @@ public class StringBoxTest {
position2
).
toString
();
// Assert
assertEquals
(
expectedOutput
,
actualOutput
);
assertEquals
(
10
,
actualOutput
.
length
());
}
@Test
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment