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
8f8c9c98
Commit
8f8c9c98
authored
5 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Added javadoc
parent
20985d40
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/edu/unl/cse/bohn/StringBox.java
+111
-4
111 additions, 4 deletions
src/main/java/edu/unl/cse/bohn/StringBox.java
with
111 additions
and
4 deletions
src/main/java/edu/unl/cse/bohn/StringBox.java
+
111
−
4
View file @
8f8c9c98
...
@@ -4,6 +4,22 @@
...
@@ -4,6 +4,22 @@
package
edu.unl.cse.bohn
;
package
edu.unl.cse.bohn
;
/**
* <p>A very simple terminal-based display. Can be used to place arbitrary strings in arbitrary locations in a terminal
* window. StringBox will <i>not</i> 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.</p>
*
* <p>A typical usage is:</p>
* <pre><code>
* StringBox stringBox = new StringBox();
* String screen = stringBox
* .placeString(...)
* ⋮
* .placeString(...)
* .toString();
* System.out.println(screen);
* </code></pre>
*/
public
class
StringBox
{
public
class
StringBox
{
private
int
maximumWidth
;
private
int
maximumWidth
;
private
int
maximumHeight
;
private
int
maximumHeight
;
...
@@ -11,10 +27,31 @@ public class StringBox {
...
@@ -11,10 +27,31 @@ public class StringBox {
private
StringRow
[]
rows
;
private
StringRow
[]
rows
;
/**
* <p>Produces a StringBox suitably-sized for a standard 24x80 terminal. The StringBox will be 23x80; 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>
*
* <p>Alternatively, you might create a 24x80 StringBox using {@link #StringBox(int, int)}. If the string is printed
* with <code>System.out.print()</code> then it will leave the cursor at the end of the 24th line without scrolling
* the top of the string off of the screen. This particular style would be useful if you place the prompt on the
* 24th line.</p>
*/
public
StringBox
()
{
public
StringBox
()
{
this
(
23
,
80
);
// standard terminal is 24x80, but leave room for the user's input
this
(
23
,
80
);
// standard terminal is 24x80, but leave room for the user's input
}
}
/**
* <p>Produces an arbitrarily-sized StringBox. The StringBox should be no wider than your terminal and should be
* at least one line shorter than your terminal. If the string is printed with <code>System.out.println()</code>
* then it will leave the cursor on the last line, where the user can enter their input without scrolling the top
* of the string off the screen.</p>
*
* <p>Alternatively, you might create a StringBox whose height is the height of your terminal. If the string is
* printed with <code>System.out.print()</code> then it will leave the cursor at the end of the last line without
* scrolling the top of the string off of the screen. This particular style would be useful if you place the
* prompt on the last line.</p>
*/
public
StringBox
(
int
maximumHeight
,
int
maximumWidth
)
{
public
StringBox
(
int
maximumHeight
,
int
maximumWidth
)
{
if
(
maximumHeight
>
0
&&
maximumWidth
>
0
)
{
if
(
maximumHeight
>
0
&&
maximumWidth
>
0
)
{
this
.
maximumHeight
=
maximumHeight
;
this
.
maximumHeight
=
maximumHeight
;
...
@@ -30,10 +67,34 @@ public class StringBox {
...
@@ -30,10 +67,34 @@ public class StringBox {
}
}
}
}
/**
* <p>Places a string in the StringBox with its upper-left corner in the specified location. If the string
* contains multiple lines, each line after the first will be placed in the row subsequent to the previous line,
* and the lines will be left-justified. Any portions of the string that would be placed outside the StringBox's
* defined boundaries will be silently truncated.</p>
*
* <p>Equivalent to {@link #placeStringAlignTopLeft(String, int, int)}</p>
*
* @param string the string to be placed in the StringBox
* @param topRow the row on which the first line of the string should be placed
* @param leftColumn the column in which the first character of each row should be placed
* @return the current StringBox object, suitable for chained calls
*/
public
StringBox
placeString
(
String
string
,
int
topRow
,
int
leftColumn
)
{
public
StringBox
placeString
(
String
string
,
int
topRow
,
int
leftColumn
)
{
return
placeStringAlignTopLeft
(
string
,
topRow
,
leftColumn
);
return
placeStringAlignTopLeft
(
string
,
topRow
,
leftColumn
);
}
}
/**
* Places a string in the StringBox with its upper-left corner in the specified location. If the string
* contains multiple lines, each line after the first will be placed in the row subsequent to the previous line,
* and the lines will be left-justified. Any portions of the string that would be placed outside the StringBox's
* defined boundaries will be silently truncated.
*
* @param string the string to be placed in the StringBox
* @param topRow the row on which the first line of the string should be placed
* @param leftColumn the column in which the first character of each row should be placed
* @return the current StringBox object, suitable for chained calls
*/
public
StringBox
placeStringAlignTopLeft
(
String
string
,
int
topRow
,
int
leftColumn
)
{
public
StringBox
placeStringAlignTopLeft
(
String
string
,
int
topRow
,
int
leftColumn
)
{
String
[]
strings
=
string
.
split
(
"\n"
);
String
[]
strings
=
string
.
split
(
"\n"
);
int
firstRow
=
Math
.
max
(
topRow
,
0
);
int
firstRow
=
Math
.
max
(
topRow
,
0
);
...
@@ -46,26 +107,59 @@ public class StringBox {
...
@@ -46,26 +107,59 @@ public class StringBox {
return
this
;
return
this
;
}
}
/**
* Places a string in the StringBox with its lower-left corner in the specified location. If the string
* contains multiple lines, each line before the last will be placed in the row previous to the subsequent line,
* and the lines will be left-justified. Any portions of the string that would be placed outside the StringBox's
* defined boundaries will be silently truncated.
*
* @param string the string to be placed in the StringBox
* @param bottomRow the row on which the last line of the string should be placed
* @param leftColumn the column in which the first character of each row should be placed
* @return the current StringBox object, suitable for chained calls
*/
public
StringBox
placeStringAlignBottomLeft
(
String
string
,
int
bottomRow
,
int
leftColumn
)
{
public
StringBox
placeStringAlignBottomLeft
(
String
string
,
int
bottomRow
,
int
leftColumn
)
{
String
[]
strings
=
string
.
split
(
"\n"
);
String
[]
strings
=
string
.
split
(
"\n"
);
return
placeStringAlignTopLeft
(
string
,
bottomRow
-
strings
.
length
+
1
,
leftColumn
);
return
placeStringAlignTopLeft
(
string
,
bottomRow
-
strings
.
length
+
1
,
leftColumn
);
}
}
public
StringBox
placeStringAlignTopRight
(
String
string
,
int
topRow
,
int
leftColumn
)
{
/**
* Places a string in the StringBox with its upper-right corner in the specified location. If the string
* contains multiple lines, each line after the first will be placed in the row subsequent to the previous line,
* and the lines will be right-justified. Any portions of the string that would be placed outside the StringBox's
* defined boundaries will be silently truncated.
*
* @param string the string to be placed in the StringBox
* @param topRow the row on which the first line of the string should be placed
* @param rightColumn the column in which the last character of each row should be placed
* @return the current StringBox object, suitable for chained calls
*/
public
StringBox
placeStringAlignTopRight
(
String
string
,
int
topRow
,
int
rightColumn
)
{
String
[]
strings
=
string
.
split
(
"\n"
);
String
[]
strings
=
string
.
split
(
"\n"
);
int
firstRow
=
Math
.
max
(
topRow
,
0
);
int
firstRow
=
Math
.
max
(
topRow
,
0
);
int
lastRow
=
Math
.
min
(
topRow
+
strings
.
length
,
maximumHeight
);
int
lastRow
=
Math
.
min
(
topRow
+
strings
.
length
,
maximumHeight
);
int
offset
=
-
topRow
;
int
offset
=
-
topRow
;
for
(
int
i
=
firstRow
;
i
<
lastRow
;
i
++)
{
for
(
int
i
=
firstRow
;
i
<
lastRow
;
i
++)
{
rows
[
i
].
placeSubstringAlignRight
(
strings
[
i
+
offset
],
lef
tColumn
);
rows
[
i
].
placeSubstringAlignRight
(
strings
[
i
+
offset
],
righ
tColumn
);
}
}
logicalHeight
=
Math
.
max
(
logicalHeight
,
lastRow
);
logicalHeight
=
Math
.
max
(
logicalHeight
,
lastRow
);
return
this
;
return
this
;
}
}
public
StringBox
placeStringAlignBottomRight
(
String
string
,
int
bottomRow
,
int
leftColumn
)
{
/**
* Places a string in the StringBox with its lower-right corner in the specified location. If the string
* contains multiple lines, each line before the last will be placed in the row previous to the subsequent line,
* and the lines will be right-justified. Any portions of the string that would be placed outside the StringBox's
* defined boundaries will be silently truncated.
*
* @param string the string to be placed in the StringBox
* @param bottomRow the row on which the last line of the string should be placed
* @param rightColumn the column in which the first character of each row should be placed
* @return the current StringBox object, suitable for chained calls
*/
public
StringBox
placeStringAlignBottomRight
(
String
string
,
int
bottomRow
,
int
rightColumn
)
{
String
[]
strings
=
string
.
split
(
"\n"
);
String
[]
strings
=
string
.
split
(
"\n"
);
return
placeStringAlignTopRight
(
string
,
bottomRow
-
strings
.
length
+
1
,
lef
tColumn
);
return
placeStringAlignTopRight
(
string
,
bottomRow
-
strings
.
length
+
1
,
righ
tColumn
);
}
}
@Override
@Override
...
@@ -73,6 +167,16 @@ public class StringBox {
...
@@ -73,6 +167,16 @@ public class StringBox {
return
toString
(
false
);
return
toString
(
false
);
}
}
/**
* <p>Generates the string that the client code produced by calling {@link #placeString(String, int, int)} and
* its related methods. If this method's argument is <code>true</code>, then any unused lines between the last line
* of text and the bottom of the StringBox will be filled with newLines so that when the string is printed, the
* previous string will fully scroll off of the screen. If the argument is <code>false</code>, then the returned
* string will stop after the last line of text.</p>
*
* @param padToHeight indicates whether newlines should be placed after the last line of text
* @return the string built by calls to {@link #placeString(String, int, int)} and its related methods
*/
public
String
toString
(
boolean
padToHeight
)
{
public
String
toString
(
boolean
padToHeight
)
{
StringBuilder
returnString
=
new
StringBuilder
(
maximumHeight
*
(
maximumWidth
+
1
));
StringBuilder
returnString
=
new
StringBuilder
(
maximumHeight
*
(
maximumWidth
+
1
));
if
(
logicalHeight
>
0
)
{
if
(
logicalHeight
>
0
)
{
...
@@ -89,6 +193,9 @@ public class StringBox {
...
@@ -89,6 +193,9 @@ public class StringBox {
return
returnString
.
toString
();
return
returnString
.
toString
();
}
}
/**
* A helper inner class. Client code should not directly access StringRow objects.
*/
static
class
StringRow
{
static
class
StringRow
{
private
StringBuilder
stringBuilder
;
private
StringBuilder
stringBuilder
;
private
int
maximumWidth
;
private
int
maximumWidth
;
...
...
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