Author: jim
Date: Tue May 26 21:39:52 2009
New Revision: 778895

URL: http://svn.apache.org/viewvc?rev=778895&view=rev
Log:
Allow for auto-detection of terminal width... Only viable
under Unix for now. Include "test" which  shows the operation.
CLI-166

Modified:
    commons/proper/cli/trunk/src/java/org/apache/commons/cli/HelpFormatter.java
    
commons/proper/cli/trunk/src/test/org/apache/commons/cli/HelpFormatterTest.java

Modified: 
commons/proper/cli/trunk/src/java/org/apache/commons/cli/HelpFormatter.java
URL: 
http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli/HelpFormatter.java?rev=778895&r1=778894&r2=778895&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli/HelpFormatter.java 
(original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli/HelpFormatter.java 
Tue May 26 21:39:52 2009
@@ -24,6 +24,9 @@
 import java.util.Comparator;
 import java.util.Iterator;
 import java.util.List;
+import java.util.StringTokenizer;
+import java.io.InputStream;
+import java.io.ByteArrayOutputStream;
 
 /** 
  * A formatter of help messages for the current command line options
@@ -135,6 +138,11 @@
     protected Comparator optionComparator = new OptionComparator();
 
     /**
+     * Flag to determine if we try to determine terminal width
+     */
+    public boolean autoWidth = false;
+    
+    /**
      * Sets the 'width'.
      *
      * @param width the new value of 'width'
@@ -322,6 +330,28 @@
     }
 
     /**
+     * Sets the 'autoWidth'.
+     *
+     * @param flag the new value of 'autoWidth'
+     */
+    public void setAutoWidth(boolean flag)
+    {
+        this.autoWidth = flag;
+        int newWidth = (flag ? getTerminalWidth() : DEFAULT_WIDTH);
+        setWidth(newWidth);
+    }
+    
+    /**
+     * Returns the 'autoWidth'.
+     *
+     * @return the 'autoWidth'
+     */
+    public boolean getAutoWidth()
+    {
+        return autoWidth;
+    }
+    
+    /**
      * Print the help for <code>options</code> with the specified
      * command line syntax.  This method prints help information to
      * System.out.
@@ -632,6 +662,58 @@
     }
 
     /**
+     * Returns the auto-detected Terminal width as reported by stty -a  
+     *
+     */
+    
+    private static int getTerminalWidth()
+    {
+        int ret = DEFAULT_WIDTH;
+        if (System.getProperty("os.name").toLowerCase().indexOf("windows") == 
-1) {
+            String sttya = unixCmdOut("stty -a < /dev/tty");
+            StringTokenizer stok = new StringTokenizer(sttya, ";");
+            while (stok.hasMoreTokens()) {
+                String out = stok.nextToken().trim();
+                if (out.startsWith("columns")) {
+                    int index = out.lastIndexOf(" ");
+                    ret = Integer.parseInt(out.substring(index).trim());
+                    break;
+                } else if (out.endsWith("columns")) {
+                    int index = out.indexOf(" ");
+                    ret = Integer.parseInt(out.substring(0, index).trim());
+                    break;
+                }
+            }
+        }
+        return ret;
+    }
+    
+    /**
+     * Runs the provided Unix command line and returns stdout  
+     *
+     * @param program the program to run
+     */
+    private static String unixCmdOut(String program)
+    {
+        int c;
+        InputStream in;
+        String rstr;
+        ByteArrayOutputStream sout = new ByteArrayOutputStream();
+        try {
+            Process p = Runtime.getRuntime().exec(new String[] 
{"sh","-c",program});
+            in = p.getInputStream();
+            while ((c = in.read()) != -1) {
+                sout.write(c);
+            }
+            p.waitFor();
+            rstr = new String(sout.toString());
+        } catch (Exception e) {
+            rstr = new String(DEFAULT_WIDTH + " columns;");
+        }
+        return rstr;
+    }
+    
+    /**
      * Print the cmdLineSyntax to the specified writer, using the
      * specified width.
      *
@@ -981,4 +1063,5 @@
             return opt1.getKey().compareToIgnoreCase(opt2.getKey());
         }
     }
+
 }

Modified: 
commons/proper/cli/trunk/src/test/org/apache/commons/cli/HelpFormatterTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli/HelpFormatterTest.java?rev=778895&r1=778894&r2=778895&view=diff
==============================================================================
--- 
commons/proper/cli/trunk/src/test/org/apache/commons/cli/HelpFormatterTest.java 
(original)
+++ 
commons/proper/cli/trunk/src/test/org/apache/commons/cli/HelpFormatterTest.java 
Tue May 26 21:39:52 2009
@@ -450,4 +450,22 @@
                 "footer"+EOL
                 ,out.toString());
     }
+
+    public void testAutoWidth()
+    {
+        // related to Bugzilla #19383 (CLI-67)
+        Options options = new Options();
+        options.addOption(new Option("a", "aaa", false, "aaaaaaa aaaa aaaaa 
aaaaaaaa aaaaaa aa aaaaa aaaaaaaaaaaaa"));
+        options.addOption(new Option(null, "bbb", false, "bbbbbbb  bbbb bbbbbb 
bbbbb bbbbbb bbbb bbbbbbbb bbbbbbbb bbbbbbbbbb"));
+        options.addOption(new Option("c", null, false, "ccccccc ccccccccccc 
ccccccccccccc ccc ccccc cccccccc cccccccccccc ccccc ccc ccc ccccccccccccccccc 
cc"));
+        
+        HelpFormatter formatter = new HelpFormatter();
+        formatter.setAutoWidth(true);
+        StringWriter out = new StringWriter();
+        formatter.printHelp("foobar", options);
+        assertEquals("1", "1");
+    }
+    
+    
+    
 }


Reply via email to