incubator-blur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amccu...@apache.org
Subject [1/4] git commit: Improving the terms command in the shell.
Date Mon, 07 Apr 2014 23:27:25 GMT
Repository: incubator-blur
Updated Branches:
  refs/heads/apache-blur-0.2 b6474b19d -> b5eb4795d


Improving the terms command in the shell.


Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/a0cbd0c9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/a0cbd0c9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/a0cbd0c9

Branch: refs/heads/apache-blur-0.2
Commit: a0cbd0c98dbed4556e0183623ff873c187bd3e0d
Parents: b6474b1
Author: Aaron McCurry <amccurry@gmail.com>
Authored: Sat Mar 29 15:56:26 2014 -0400
Committer: Aaron McCurry <amccurry@gmail.com>
Committed: Sat Mar 29 15:56:26 2014 -0400

----------------------------------------------------------------------
 .../blur/shell/ColumnBasedPrintWriter.java      | 75 ++++++++++++++++++++
 .../apache/blur/shell/PagingPrintWriter.java    |  5 ++
 .../org/apache/blur/shell/TermsDataCommand.java | 43 +++++++----
 3 files changed, 108 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/a0cbd0c9/blur-shell/src/main/java/org/apache/blur/shell/ColumnBasedPrintWriter.java
----------------------------------------------------------------------
diff --git a/blur-shell/src/main/java/org/apache/blur/shell/ColumnBasedPrintWriter.java b/blur-shell/src/main/java/org/apache/blur/shell/ColumnBasedPrintWriter.java
new file mode 100644
index 0000000..067b65f
--- /dev/null
+++ b/blur-shell/src/main/java/org/apache/blur/shell/ColumnBasedPrintWriter.java
@@ -0,0 +1,75 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.blur.shell;
+
+import java.io.PrintWriter;
+import java.util.Arrays;
+
+public class ColumnBasedPrintWriter extends PagingPrintWriter {
+
+  private static final String SEP = "|";
+  private final int[] _columnWidths;
+
+  public ColumnBasedPrintWriter(PrintWriter out, int numberOfColumns, int maxColumnWidth)
{
+    super(out);
+    int[] cols = new int[numberOfColumns];
+    Arrays.fill(cols, maxColumnWidth);
+    _columnWidths = cols;
+  }
+
+  public ColumnBasedPrintWriter(PrintWriter out, int[] columnWidths) {
+    super(out);
+    _columnWidths = columnWidths;
+  }
+
+  public void println(Object... x) throws FinishedException {
+    for (int i = 0; i < _columnWidths.length; i++) {
+      if (i != 0) {
+        print(SEP);
+      }
+      if (x != null && i < x.length) {
+        Object o = x[i];
+        String s = truncate(getStr(o), _columnWidths[i]);
+        print(s);
+      }
+    }
+    println();
+  }
+
+  private String truncate(String str, int maxColumnWidth) {
+    if (str.length() > maxColumnWidth) {
+      return str.substring(0, maxColumnWidth - 3) + "...";
+    }
+    return buffer(str, maxColumnWidth);
+  }
+
+  private String buffer(String str, int maxColumnWidth) {
+    StringBuilder builder = new StringBuilder(str);
+    while (builder.length() < maxColumnWidth) {
+      builder.append(' ');
+    }
+    return builder.toString();
+  }
+
+  private String getStr(Object o) {
+    if (o == null) {
+      return "NULL";
+    }
+    return o.toString();
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/a0cbd0c9/blur-shell/src/main/java/org/apache/blur/shell/PagingPrintWriter.java
----------------------------------------------------------------------
diff --git a/blur-shell/src/main/java/org/apache/blur/shell/PagingPrintWriter.java b/blur-shell/src/main/java/org/apache/blur/shell/PagingPrintWriter.java
index 03d24a0..ed963c6 100644
--- a/blur-shell/src/main/java/org/apache/blur/shell/PagingPrintWriter.java
+++ b/blur-shell/src/main/java/org/apache/blur/shell/PagingPrintWriter.java
@@ -55,6 +55,11 @@ public class PagingPrintWriter {
     flush();
   }
 
+  public void print(String s) throws FinishedException {
+    _printWriter.print(s);
+    flush();
+  }
+
   public void println() throws FinishedException {
     _printWriter.println();
     _line++;

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/a0cbd0c9/blur-shell/src/main/java/org/apache/blur/shell/TermsDataCommand.java
----------------------------------------------------------------------
diff --git a/blur-shell/src/main/java/org/apache/blur/shell/TermsDataCommand.java b/blur-shell/src/main/java/org/apache/blur/shell/TermsDataCommand.java
index 823b145..6331506 100644
--- a/blur-shell/src/main/java/org/apache/blur/shell/TermsDataCommand.java
+++ b/blur-shell/src/main/java/org/apache/blur/shell/TermsDataCommand.java
@@ -57,7 +57,21 @@ public class TermsDataCommand extends Command implements TableFirstArgCommand
{
 
   private void doitInternal(PrintWriter outPw, Blur.Iface client, String[] args) throws FinishedException,
       BlurException, TException {
-    PagingPrintWriter out = new PagingPrintWriter(outPw);
+
+    ConsoleReader reader = getConsoleReader();
+    int totalWidth = 200;
+    if (reader != null) {
+      totalWidth = reader.getTerminal().getWidth() - 2;
+    }
+
+    int[] cols = new int[2];
+    cols[0] = 20;
+    cols[1] = totalWidth - cols[0];
+    ColumnBasedPrintWriter out = new ColumnBasedPrintWriter(outPw, cols);
+    if (reader != null) {
+      Terminal terminal = reader.getTerminal();
+      out.setLineLimit(terminal.getHeight() - 2);
+    }
     CommandLine cmd = parse(args, outPw);
     if (cmd == null) {
       return;
@@ -89,20 +103,19 @@ public class TermsDataCommand extends Command implements TableFirstArgCommand
{
       checkFreq = true;
     }
 
-    int maxWidth = 100;
-    ConsoleReader reader = getConsoleReader();
-    if (reader != null) {
-      Terminal terminal = reader.getTerminal();
-      maxWidth = terminal.getWidth() - 15;
-      out.setLineLimit(terminal.getHeight() - 2);
-    }
-
-    List<String> terms = client.terms(tablename, family, column, startWith, size);
-    for (int i = 0; i < terms.size(); i++) {
-      if (checkFreq) {
-        out.println(terms.get(i) + "\t" + client.recordFrequency(tablename, family, column,
terms.get(i)));
-      } else {
-        out.println(terms.get(i));
+    while (true) {
+      List<String> terms = client.terms(tablename, family, column, startWith, size);
+      for (int i = 0; i < terms.size(); i++) {
+        String term = terms.get(i);
+        if (term.equals(startWith)) {
+          continue;
+        }
+        if (checkFreq) {
+          out.println(client.recordFrequency(tablename, family, column, term), term);
+        } else {
+          out.println(" - ", term);
+        }
+        startWith = term;
       }
     }
   }


Mime
View raw message