|
24 | 24 | import java.util.Comparator; |
25 | 25 | import java.util.Iterator; |
26 | 26 | import java.util.List; |
| 27 | +import java.util.StringTokenizer; |
| 28 | +import java.io.InputStream; |
| 29 | +import java.io.ByteArrayOutputStream; |
27 | 30 |
|
28 | 31 | /** |
29 | 32 | * A formatter of help messages for the current command line options |
@@ -134,6 +137,11 @@ public class HelpFormatter |
134 | 137 | */ |
135 | 138 | protected Comparator optionComparator = new OptionComparator(); |
136 | 139 |
|
| 140 | + /** |
| 141 | + * Flag to determine if we try to determine terminal width |
| 142 | + */ |
| 143 | + public boolean autoWidth = false; |
| 144 | + |
137 | 145 | /** |
138 | 146 | * Sets the 'width'. |
139 | 147 | * |
@@ -321,6 +329,28 @@ public void setOptionComparator(Comparator comparator) |
321 | 329 | } |
322 | 330 | } |
323 | 331 |
|
| 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 | + |
324 | 354 | /** |
325 | 355 | * Print the help for <code>options</code> with the specified |
326 | 356 | * command line syntax. This method prints help information to |
@@ -631,6 +661,58 @@ private static void appendOption(final StringBuffer buff, final Option option, f |
631 | 661 | } |
632 | 662 | } |
633 | 663 |
|
| 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 | + |
634 | 716 | /** |
635 | 717 | * Print the cmdLineSyntax to the specified writer, using the |
636 | 718 | * specified width. |
@@ -981,4 +1063,5 @@ public int compare(Object o1, Object o2) |
981 | 1063 | return opt1.getKey().compareToIgnoreCase(opt2.getKey()); |
982 | 1064 | } |
983 | 1065 | } |
| 1066 | + |
984 | 1067 | } |
0 commit comments