valepakh commented on code in PR #7420:
URL: https://github.com/apache/ignite-3/pull/7420#discussion_r2707907374


##########
modules/cli/src/main/java/org/apache/ignite/internal/cli/util/TableTruncator.java:
##########
@@ -0,0 +1,238 @@
+/*
+ * 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.ignite.internal.cli.util;
+
+import static 
org.apache.ignite.internal.cli.decorators.TruncationConfig.ELLIPSIS;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.ignite.internal.cli.decorators.TruncationConfig;
+import org.apache.ignite.internal.cli.sql.table.Table;
+
+/**
+ * Truncates table columns to fit terminal width.
+ */
+public class TableTruncator {
+    /** Minimum column width to display at least ellipsis. */
+    private static final int MIN_COLUMN_WIDTH = ELLIPSIS.length();
+
+    /** Characters used for table borders in FlipTables (| and spaces). */
+    private static final int BORDER_OVERHEAD_PER_COLUMN = 3;
+
+    /** Left and right border overhead. */
+    private static final int TABLE_BORDER_OVERHEAD = 2;
+
+    private final TruncationConfig config;
+
+    /**
+     * Creates a new TableTruncator with the given configuration.
+     *
+     * @param config truncation configuration
+     */
+    public TableTruncator(TruncationConfig config) {
+        this.config = config;
+    }
+
+    /**
+     * Truncates table columns based on the truncation configuration.
+     *
+     * @param table the table to truncate
+     * @return a new table with truncated values, or the original table if 
truncation is disabled
+     */
+    public Table<String> truncate(Table<String> table) {
+        if (!config.isTruncateEnabled()) {
+            return table;
+        }
+
+        String[] header = table.header();
+        Object[][] content = table.content();
+
+        if (header.length == 0) {
+            return table;
+        }
+
+        int[] columnWidths = calculateColumnWidths(header, content);
+        String[] truncatedHeader = truncateRow(header, columnWidths);
+        List<String> truncatedContent = truncateContent(content, columnWidths);
+
+        return new Table<>(Arrays.asList(truncatedHeader), truncatedContent);
+    }
+
+    /**
+     * Calculates optimal column widths based on content and terminal 
constraints.
+     *
+     * @param header table header
+     * @param content table content
+     * @return array of column widths
+     */
+    int[] calculateColumnWidths(String[] header, Object[][] content) {
+        int columnCount = header.length;
+        int[] maxContentWidths = new int[columnCount];
+
+        // Calculate maximum content width for each column
+        for (int col = 0; col < columnCount; col++) {
+            maxContentWidths[col] = Math.max(maxContentWidths[col], 
stringLength(header[col]));
+            for (Object[] row : content) {
+                if (col < row.length) {
+                    maxContentWidths[col] = Math.max(maxContentWidths[col], 
stringLength(row[col]));
+                }
+            }
+        }
+
+        int[] columnWidths = new int[columnCount];
+        int maxColumnWidth = config.getMaxColumnWidth();
+        int terminalWidth = config.getTerminalWidth();
+
+        // Apply max column width constraint
+        for (int col = 0; col < columnCount; col++) {
+            columnWidths[col] = Math.min(maxContentWidths[col], 
maxColumnWidth);
+            columnWidths[col] = Math.max(columnWidths[col], MIN_COLUMN_WIDTH);
+        }
+
+        // Apply terminal width constraint if set
+        if (terminalWidth > 0) {
+            distributeWidthsForTerminal(columnWidths, terminalWidth);
+        }
+
+        return columnWidths;
+    }
+
+    /**
+     * Distributes column widths to fit within terminal width.
+     *
+     * @param columnWidths column widths to adjust (modified in place)
+     * @param terminalWidth terminal width constraint
+     */
+    private void distributeWidthsForTerminal(int[] columnWidths, int 
terminalWidth) {

Review Comment:
   This is not fully resolved



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to