Skip to content

Commit 58d89c0

Browse files
committed
Allow for auto-detection of terminal width... Only viable
under Unix for now. Include "test" which shows the operation. CLI-166 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@778895 13f79535-47bb-0310-9956-ffa450edef68
1 parent e366a69 commit 58d89c0

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

src/java/org/apache/commons/cli/HelpFormatter.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import java.util.Comparator;
2525
import java.util.Iterator;
2626
import java.util.List;
27+
import java.util.StringTokenizer;
28+
import java.io.InputStream;
29+
import java.io.ByteArrayOutputStream;
2730

2831
/**
2932
* A formatter of help messages for the current command line options
@@ -134,6 +137,11 @@ public class HelpFormatter
134137
*/
135138
protected Comparator optionComparator = new OptionComparator();
136139

140+
/**
141+
* Flag to determine if we try to determine terminal width
142+
*/
143+
public boolean autoWidth = false;
144+
137145
/**
138146
* Sets the 'width'.
139147
*
@@ -321,6 +329,28 @@ public void setOptionComparator(Comparator comparator)
321329
}
322330
}
323331

332+
/**
333+
* Sets the 'autoWidth'.
334+
*
335+
* @param flag the new value of 'autoWidth'
336+
*/
337+
public void setAutoWidth(boolean flag)
338+
{
339+
this.autoWidth = flag;
340+
int newWidth = (flag ? getTerminalWidth() : DEFAULT_WIDTH);
341+
setWidth(newWidth);
342+
}
343+
344+
/**
345+
* Returns the 'autoWidth'.
346+
*
347+
* @return the 'autoWidth'
348+
*/
349+
public boolean getAutoWidth()
350+
{
351+
return autoWidth;
352+
}
353+
324354
/**
325355
* Print the help for <code>options</code> with the specified
326356
* command line syntax. This method prints help information to
@@ -631,6 +661,58 @@ private static void appendOption(final StringBuffer buff, final Option option, f
631661
}
632662
}
633663

664+
/**
665+
* Returns the auto-detected Terminal width as reported by stty -a
666+
*
667+
*/
668+
669+
private static int getTerminalWidth()
670+
{
671+
int ret = DEFAULT_WIDTH;
672+
if (System.getProperty("os.name").toLowerCase().indexOf("windows") == -1) {
673+
String sttya = unixCmdOut("stty -a < /dev/tty");
674+
StringTokenizer stok = new StringTokenizer(sttya, ";");
675+
while (stok.hasMoreTokens()) {
676+
String out = stok.nextToken().trim();
677+
if (out.startsWith("columns")) {
678+
int index = out.lastIndexOf(" ");
679+
ret = Integer.parseInt(out.substring(index).trim());
680+
break;
681+
} else if (out.endsWith("columns")) {
682+
int index = out.indexOf(" ");
683+
ret = Integer.parseInt(out.substring(0, index).trim());
684+
break;
685+
}
686+
}
687+
}
688+
return ret;
689+
}
690+
691+
/**
692+
* Runs the provided Unix command line and returns stdout
693+
*
694+
* @param program the program to run
695+
*/
696+
private static String unixCmdOut(String program)
697+
{
698+
int c;
699+
InputStream in;
700+
String rstr;
701+
ByteArrayOutputStream sout = new ByteArrayOutputStream();
702+
try {
703+
Process p = Runtime.getRuntime().exec(new String[] {"sh","-c",program});
704+
in = p.getInputStream();
705+
while ((c = in.read()) != -1) {
706+
sout.write(c);
707+
}
708+
p.waitFor();
709+
rstr = new String(sout.toString());
710+
} catch (Exception e) {
711+
rstr = new String(DEFAULT_WIDTH + " columns;");
712+
}
713+
return rstr;
714+
}
715+
634716
/**
635717
* Print the cmdLineSyntax to the specified writer, using the
636718
* specified width.
@@ -981,4 +1063,5 @@ public int compare(Object o1, Object o2)
9811063
return opt1.getKey().compareToIgnoreCase(opt2.getKey());
9821064
}
9831065
}
1066+
9841067
}

src/test/org/apache/commons/cli/HelpFormatterTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,4 +450,22 @@ public void testOptionWithoutShortFormat2()
450450
"footer"+EOL
451451
,out.toString());
452452
}
453+
454+
public void testAutoWidth()
455+
{
456+
// related to Bugzilla #19383 (CLI-67)
457+
Options options = new Options();
458+
options.addOption(new Option("a", "aaa", false, "aaaaaaa aaaa aaaaa aaaaaaaa aaaaaa aa aaaaa aaaaaaaaaaaaa"));
459+
options.addOption(new Option(null, "bbb", false, "bbbbbbb bbbb bbbbbb bbbbb bbbbbb bbbb bbbbbbbb bbbbbbbb bbbbbbbbbb"));
460+
options.addOption(new Option("c", null, false, "ccccccc ccccccccccc ccccccccccccc ccc ccccc cccccccc cccccccccccc ccccc ccc ccc ccccccccccccccccc cc"));
461+
462+
HelpFormatter formatter = new HelpFormatter();
463+
formatter.setAutoWidth(true);
464+
StringWriter out = new StringWriter();
465+
formatter.printHelp("foobar", options);
466+
assertEquals("1", "1");
467+
}
468+
469+
470+
453471
}

0 commit comments

Comments
 (0)