Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit89dcb39

Browse files
committed
feat(cli): preserve order of table columns
1 parentb16275b commit89dcb39

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

‎cli/cliui/table.go‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,33 @@ func Table() table.Writer {
3131
// e.g. `[]any{someRow, TableSeparator, someRow}`
3232
typeTableSeparatorstruct{}
3333

34-
// filterTableColumns returns configurations to hide columns
34+
// filterHeaders filters the headers to only include the columns
35+
// that are provided in the array. If the array is empty, all
36+
// headers are included.
37+
funcfilterHeaders(header table.Row,columns []string) table.Row {
38+
iflen(columns)==0 {
39+
returnheader
40+
}
41+
42+
filteredHeaders:=make(table.Row,len(columns))
43+
fori,column:=rangecolumns {
44+
column=strings.ReplaceAll(column,"_"," ")
45+
46+
for_,headerTextRaw:=rangeheader {
47+
headerText,_:=headerTextRaw.(string)
48+
ifstrings.EqualFold(column,headerText) {
49+
filteredHeaders[i]=headerText
50+
break
51+
}
52+
}
53+
}
54+
returnfilteredHeaders
55+
}
56+
57+
// createColumnConfigs returns configuration to hide columns
3558
// that are not provided in the array. If the array is empty,
3659
// no filtering will occur!
37-
funcfilterTableColumns(header table.Row,columns []string) []table.ColumnConfig {
60+
funccreateColumnConfigs(header table.Row,columns []string) []table.ColumnConfig {
3861
iflen(columns)==0 {
3962
returnnil
4063
}
@@ -157,10 +180,13 @@ func DisplayTable(out any, sort string, filterColumns []string) (string, error)
157180
funcrenderTable(outany,sortstring,headers table.Row,filterColumns []string) (string,error) {
158181
v:=reflect.Indirect(reflect.ValueOf(out))
159182

183+
headers=filterHeaders(headers,filterColumns)
184+
columnConfigs:=createColumnConfigs(headers,filterColumns)
185+
160186
// Setup the table formatter.
161187
tw:=Table()
162188
tw.AppendHeader(headers)
163-
tw.SetColumnConfigs(filterTableColumns(headers,filterColumns))
189+
tw.SetColumnConfigs(columnConfigs)
164190
ifsort!="" {
165191
tw.SortBy([]table.SortBy{{
166192
Name:sort,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp