diff --git a/bindings/java/src/net/hyperic/sigar/cmd/SigarCommandBase.java b/bindings/java/src/net/hyperic/sigar/cmd/SigarCommandBase.java index 77f199df..4ea9f034 100644 --- a/bindings/java/src/net/hyperic/sigar/cmd/SigarCommandBase.java +++ b/bindings/java/src/net/hyperic/sigar/cmd/SigarCommandBase.java @@ -2,8 +2,10 @@ package net.hyperic.sigar.cmd; import java.io.PrintStream; +import java.util.Arrays; import java.util.ArrayList; import java.util.Collection; +import java.util.Iterator; import java.util.List; import net.hyperic.sigar.Sigar; @@ -35,6 +37,7 @@ public abstract class SigarCommandBase private CollectionCompleter completer; private Collection completions = new ArrayList(); private PrintfFormat formatter; + private ArrayList printfItems = new ArrayList(); public SigarCommandBase(Shell shell) { this.shell = shell; @@ -42,7 +45,6 @@ public abstract class SigarCommandBase this.err = shell.getErrStream(); this.sigar = shell.getSigar(); this.proxy = shell.getSigarProxy(); - //provide simple way for handlers to implement tab completion this.completer = new CollectionCompleter(shell); @@ -70,7 +72,14 @@ public abstract class SigarCommandBase } public void printf(Object[] items) { - println(getFormatter().sprintf(items)); + PrintfFormat formatter = getFormatter(); + if (formatter == null) { + //see flushPrintfItems + this.printfItems.add(items); + } + else { + println(formatter.sprintf(items)); + } } public void printf(List items) { @@ -86,7 +95,49 @@ public abstract class SigarCommandBase } } + private void flushPrintfItems() { + if (this.printfItems.size() == 0) { + return; + } + + //no format was specified, just line up the columns + int[] max = null; + + for (Iterator it=this.printfItems.iterator(); + it.hasNext();) + { + Object[] items = (Object[])it.next(); + if (max == null) { + max = new int[items.length]; + Arrays.fill(max, 0); + } + for (int i=0; i max[i]) { + max[i] = len; + } + } + } + + StringBuffer format = new StringBuffer(); + for (int i=0; i