如何在 Golang 中打印一个 2 列的表格?

问题描述 投票:0回答:1

有点坚持这个。我的想法是有一个打印两列表的函数。第一个是键,它有一个固定的宽度。第二个是给值的,可能是很长的字符串,它的宽度取决于终端当前的宽度。

我想要的例子:

Key1                                  Value1Value1Value1Value1
Key2                                  Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2
                                      Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2
                                      Value2Value2Value2Value2Value2Value2

到目前为止,我取得的最好成绩是使用 lipgloss 为第一列设置固定宽度。

func PrintMetadata(metadata map[string]string, color string) {
    style := lipgloss.NewStyle().Width(32).Foreground(lipgloss.Color(color))
    for k, v := range metadata {
        fmt.Println(style.Render(k) + v)
    }
}

结果是这样的:

Key1                                  Value1Value1Value1Value1
Key2                                  Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2
Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2Value2

那么,我怎样才能按照我想要的方式格式化字符串呢?我可以同时使用标准库和外部库,因此欢迎提出任何建议。

string go format command-line-interface tui
1个回答
0
投票

我为此做了一个函数。此函数有两个参数,第一个用于列的映射变量,第二个参数用于每行要填充多少个字符。它只是将带有间距的键值内容更改为新变量,然后打印该键值。但是,如果您使用未修改的值,则可以使用未修改的变量。

package main

import (
    "fmt"
    "errors"
    "strings"
)

func main() {
    a := map[string]string{
    "Key1": strings.Repeat("Value1", 50), 
    "Key2": strings.Repeat("Value2", 50), 
    "Key3": strings.Repeat("Value3", 50),
    }
    
    err := columner(a, 30)
    
    if err != nil {
        fmt.Println(err)
    }
    
}

func columner(m map[string]string, charAmount int) error{   
    
    for _, value := range m {
        if charAmount > len(value) {
            return errors.New("Error: charAmount can't be greater than key's value")
        }
    }
    
    for key, value := range m {
    
        var value2 string
        
        firstI := 0
        
        lastI := charAmount
        part := lastI
        
        spacingS := strings.Repeat(" ", 20)
        spacing := spacingS

        for i := 0; i < len(value); i += part{
                
            value2 += spacingS + value[firstI:lastI] + "\n" + spacing
            
            spacingS = strings.Repeat(" ", len(key) + 1)
            
            if ((len(value) - lastI) < part) || ((len(value) - lastI) == part) {
                break
            }
    
            firstI = lastI
            
            lastI += part
        }   
        
        value2 += spacingS + value[lastI:]

        fmt.Println(key, value2, "\n")
    }
    
    return nil
}

这是一个示例输出:

Key1                     Value1Value1Value1Value1Value1
                         Value1Value1Value1Value1Value1
                         Value1Value1Value1Value1Value1
                         Value1Value1Value1Value1Value1
                         Value1Value1Value1Value1Value1
                         Value1Value1Value1Value1Value1
                         Value1Value1Value1Value1Value1
                         Value1Value1Value1Value1Value1
                         Value1Value1Value1Value1Value1
                         Value1Value1Value1Value1Value1 

Key2                     Value2Value2Value2Value2Value2
                         Value2Value2Value2Value2Value2
                         Value2Value2Value2Value2Value2
                         Value2Value2Value2Value2Value2
                         Value2Value2Value2Value2Value2
                         Value2Value2Value2Value2Value2
                         Value2Value2Value2Value2Value2
                         Value2Value2Value2Value2Value2
                         Value2Value2Value2Value2Value2
                         Value2Value2Value2Value2Value2 

Key3                     Value3Value3Value3Value3Value3
                         Value3Value3Value3Value3Value3
                         Value3Value3Value3Value3Value3
                         Value3Value3Value3Value3Value3
                         Value3Value3Value3Value3Value3
                         Value3Value3Value3Value3Value3
                         Value3Value3Value3Value3Value3
                         Value3Value3Value3Value3Value3
                         Value3Value3Value3Value3Value3
                         Value3Value3Value3Value3Value3 

但是请注意,每次执行时键和值的顺序可能不同,因为在带有键值对的 for 循环中打印时映射类型是无序的。

© www.soinside.com 2019 - 2024. All rights reserved.