Skip to content

Conversation

@cnsnyder
Copy link

@cnsnyder cnsnyder commented Jan 7, 2026

  • Implemented displayWidth and truncateString functions to handle UTF-8 characters correctly.
  • Enhanced PrintTable function to support proper column alignment and truncation based on terminal width, ensuring better display of strings in tables.

At the moment this does not change any used functionality in rhc, hence why it is in draft. After upcoming work to read from the config files for user preferences the functions herein (along with a few extra bits to tie things together) will implement the "rhc configure features show" command.

Opening this PR to get early feedback on the approach and results of this approach so as to make best use of time.

The following script in a single go file can be used to demonstrate the additions to ui.go:

package main

import (
	"fmt"
	"strings"
	"unicode/utf8"
)

func displayWidth(s string) int {
	return utf8.RuneCountInString(s)
}

func truncateString(s string, maxWidth int) string {
	if displayWidth(s) <= maxWidth {
		return s
	}
	targetWidth := maxWidth - 3
	if targetWidth < 0 {
		targetWidth = 0
	}
	width := 0
	truncated := strings.Builder{}
	for _, r := range s {
		runeWidth := displayWidth(string(r))
		if width+runeWidth > targetWidth {
			break
		}
		truncated.WriteRune(r)
		width += runeWidth
	}
	return truncated.String() + "..."
}

func PrintTable(table [][]string, sep string, termWidth int) {
	if len(table) == 0 || len(table[0]) == 0 {
		return
	}
	sepWidth := displayWidth(sep)
	columnWidths := make([]int, len(table[0]))
	for _, row := range table {
		for col, cell := range row {
			cellWidth := displayWidth(cell)
			if cellWidth > columnWidths[col] {
				columnWidths[col] = cellWidth
			}
		}
	}
	tableOutput := ""
	for _, row := range table {
		rowString := ""
		for col, cell := range row {
			rowString += cell
			if col < len(row)-1 {
				cellWidth := displayWidth(cell)
				padding := columnWidths[col] - cellWidth + sepWidth
				if padding > 0 {
					rowString += strings.Repeat(" ", padding)
				}
				rowString += sep
			}
		}
		if displayWidth(rowString) > termWidth {
			rowString = truncateString(rowString, termWidth)
		}
		tableOutput += rowString + "\n"
	}
	fmt.Printf("%s", tableOutput)
}

func main() {
	fmt.Println("Test 1: Basic table with UTF-8")
	table1 := [][]string{
		{"FEATURE", "CONFIG", "STATE", "DESCRIPTION"},
		{"content", "✓", "✓", "Access to package repositories"},
	}
	PrintTable(table1, "  ", 80)
	
	fmt.Println("\nTest 2: UTF-8 characters")
	table2 := [][]string{
		{"Status", "Icon"},
		{"OK", "✓"},
		{"Info", "●"},
		{"Error", "𐄂"},
	}
	PrintTable(table2, "  ", 80)
	
	fmt.Println("\nTest 3: Truncation")
	table3 := [][]string{
		{"VERY_LONG_COLUMN_NAME", "ANOTHER_LONG_COLUMN"},
		{"this is a very long value that will be truncated", "short"},
	}
	PrintTable(table3, "  ", 30)
}

- Implemented displayWidth and truncateString functions to handle UTF-8 characters correctly.
- Enhanced PrintTable function to support proper column alignment and truncation based on terminal width, ensuring better display of strings in tables.
This also updates dependencies for the new modules used.

Card: CCT-1807
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant