Skip to content

Commit 19af3e3

Browse files
buty4649claude
andcommitted
table: ヘッダ下線付きのテーブル/リスト出力に刷新
tabwriter をやめて独自の tablePrinter を導入。go-runewidth で CJK を含むセル幅を正しく計算し、TTY 出力時のみヘッダ行を緑色+下線で 装飾する。列間ギャップと末尾カラムのターミナル幅残り分も下線に 含めて連続した見た目にする。キー/値表示用に newList も追加。 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent fdb17c9 commit 19af3e3

11 files changed

Lines changed: 330 additions & 106 deletions

File tree

cmd/approval.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"os"
66
"strings"
7-
"text/tabwriter"
87

98
"github.com/pepabo/xpoint-cli/internal/xpoint"
109
"github.com/spf13/cobra"
@@ -139,16 +138,14 @@ func runApprovalList(cmd *cobra.Command, args []string) error {
139138
}
140139

141140
return render(res, resolveOutputFormat(approvalListOutput), approvalListJQ, func() error {
142-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
143-
defer w.Flush()
144141
fmt.Fprintf(os.Stdout, "total: %d\n", res.TotalCount)
145-
fmt.Fprintln(w, "DOCID\tSTATUS\tFORM_NAME\tAPPLY_USER\tAPPROVERS\tAPPLY_DATETIME\tTITLE1")
142+
w := newTable(os.Stdout,
143+
"DOCID", "STATUS", "FORM_NAME", "APPLY_USER", "APPROVERS", "APPLY_DATETIME", "TITLE1")
146144
for _, a := range res.ApprovalList {
147-
fmt.Fprintf(w, "%d\t%s\t%s\t%s\t%s\t%s\t%s\n",
148-
a.DocID, a.DisplayStatus, a.FormName, a.ApplyUser,
149-
strings.Join(a.ApprovalUser, ","), a.ApplyDatetime, a.Title1,
150-
)
145+
w.AddRow(a.DocID, a.DisplayStatus, a.FormName, a.ApplyUser,
146+
strings.Join(a.ApprovalUser, ","), a.ApplyDatetime, a.Title1)
151147
}
148+
w.Print()
152149
return nil
153150
})
154151
}
@@ -179,22 +176,20 @@ func runApprovalWait(cmd *cobra.Command, args []string) error {
179176
}
180177

181178
return render(res, resolveOutputFormat(approvalWaitOutput), approvalWaitJQ, func() error {
182-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
183-
fmt.Fprintln(w, "TYPE\tNAME\tCOUNT")
179+
w := newTable(os.Stdout, "TYPE", "NAME", "COUNT")
184180
for _, s := range res.StatusList {
185-
fmt.Fprintf(w, "%d\t%s\t%d\n", s.Type, s.Name, s.Count)
181+
w.AddRow(s.Type, s.Name, s.Count)
186182
}
187-
w.Flush()
183+
w.Print()
188184

189185
if len(res.WaitList) > 0 {
190186
fmt.Fprintln(os.Stdout)
191187
fmt.Fprintln(os.Stdout, "最新の承認待ち書類:")
192-
w2 := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
193-
fmt.Fprintln(w2, "DOCID\tFORM_NAME\tWRITER\tWRITE_DATE\tTITLE")
188+
w2 := newTable(os.Stdout, "DOCID", "FORM_NAME", "WRITER", "WRITE_DATE", "TITLE")
194189
for _, d := range res.WaitList {
195-
fmt.Fprintf(w2, "%d\t%s\t%s\t%s\t%s\n", d.DocID, d.Name, d.WriterName, d.WriteDate, d.Title)
190+
w2.AddRow(d.DocID, d.Name, d.WriterName, d.WriteDate, d.Title)
196191
}
197-
w2.Flush()
192+
w2.Print()
198193
}
199194
return nil
200195
})
@@ -228,12 +223,11 @@ func runApprovalHidden(cmd *cobra.Command, args []string) error {
228223

229224
return render(res, resolveOutputFormat(approvalHiddenOutput), approvalHiddenJQ, func() error {
230225
fmt.Fprintf(os.Stdout, "message: %s\n", res.Message)
231-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
232-
defer w.Flush()
233-
fmt.Fprintln(w, "DOCID")
226+
w := newTable(os.Stdout, "DOCID")
234227
for _, id := range res.DocID {
235-
fmt.Fprintf(w, "%d\n", id)
228+
w.AddRow(id)
236229
}
230+
w.Print()
237231
return nil
238232
})
239233
}

cmd/document.go

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"path/filepath"
1010
"strconv"
1111
"strings"
12-
"text/tabwriter"
1312
"time"
1413

1514
"github.com/pepabo/xpoint-cli/internal/xpoint"
@@ -487,15 +486,13 @@ func runDocumentSearch(cmd *cobra.Command, args []string) error {
487486
}
488487

489488
return render(res, resolveOutputFormat(docSearchOutput), docSearchJQ, func() error {
490-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
491-
defer w.Flush()
492489
fmt.Fprintf(os.Stdout, "total: %d\n", res.TotalCount)
493-
fmt.Fprintln(w, "DOCID\tFORM_NAME\tWRITER\tWRITE_DATETIME\tSTEP\tSTAT\tTITLE1")
490+
w := newTable(os.Stdout,
491+
"DOCID", "FORM_NAME", "WRITER", "WRITE_DATETIME", "STEP", "STAT", "TITLE1")
494492
for _, it := range res.Items {
495-
fmt.Fprintf(w, "%d\t%s\t%s\t%s\t%d\t%d\t%s\n",
496-
it.DocID, it.Form.Name, it.Writer, it.WriteDatetime, it.Step, it.Stat, it.Title1,
497-
)
493+
w.AddRow(it.DocID, it.Form.Name, it.Writer, it.WriteDatetime, it.Step, it.Stat, it.Title1)
498494
}
495+
w.Print()
499496
return nil
500497
})
501498
}
@@ -534,10 +531,9 @@ func runDocumentCreate(cmd *cobra.Command, args []string) error {
534531
}
535532

536533
return render(&view, resolveOutputFormat(docCreateOutput), docCreateJQ, func() error {
537-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
538-
defer w.Flush()
539-
fmt.Fprintln(w, "DOCID\tMESSAGE_TYPE\tMESSAGE\tURL")
540-
fmt.Fprintf(w, "%d\t%d\t%s\t%s\n", view.DocID, view.MessageType, view.Message, view.URL)
534+
w := newTable(os.Stdout, "DOCID", "MESSAGE_TYPE", "MESSAGE", "URL")
535+
w.AddRow(view.DocID, view.MessageType, view.Message, view.URL)
536+
w.Print()
541537
return nil
542538
})
543539
}
@@ -586,10 +582,9 @@ func runDocumentEdit(cmd *cobra.Command, args []string) error {
586582
}
587583

588584
return render(res, resolveOutputFormat(docEditOutput), docEditJQ, func() error {
589-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
590-
defer w.Flush()
591-
fmt.Fprintln(w, "DOCID\tMESSAGE_TYPE\tMESSAGE")
592-
fmt.Fprintf(w, "%d\t%d\t%s\n", res.DocID, res.MessageType, res.Message)
585+
w := newTable(os.Stdout, "DOCID", "MESSAGE_TYPE", "MESSAGE")
586+
w.AddRow(res.DocID, res.MessageType, res.Message)
587+
w.Print()
593588
return nil
594589
})
595590
}
@@ -615,10 +610,9 @@ func runDocumentDelete(cmd *cobra.Command, args []string) error {
615610
}
616611

617612
return render(res, resolveOutputFormat(docDeleteOutput), docDeleteJQ, func() error {
618-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
619-
defer w.Flush()
620-
fmt.Fprintln(w, "MESSAGE_TYPE\tMESSAGE")
621-
fmt.Fprintf(w, "%d\t%s\n", res.MessageType, res.Message)
613+
w := newTable(os.Stdout, "MESSAGE_TYPE", "MESSAGE")
614+
w.AddRow(res.MessageType, res.Message)
615+
w.Print()
622616
return nil
623617
})
624618
}
@@ -916,10 +910,9 @@ func runDocumentCommentAdd(cmd *cobra.Command, args []string) error {
916910
return err
917911
}
918912
return render(res, resolveOutputFormat(docCommentAddOutput), docCommentAddJQ, func() error {
919-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
920-
defer w.Flush()
921-
fmt.Fprintln(w, "DOCID\tSEQ\tMESSAGE_TYPE\tMESSAGE")
922-
fmt.Fprintf(w, "%d\t%d\t%d\t%s\n", res.DocID, res.Seq, res.MessageType, res.Message)
913+
w := newTable(os.Stdout, "DOCID", "SEQ", "MESSAGE_TYPE", "MESSAGE")
914+
w.AddRow(res.DocID, res.Seq, res.MessageType, res.Message)
915+
w.Print()
923916
return nil
924917
})
925918
}
@@ -938,16 +931,15 @@ func runDocumentCommentGet(cmd *cobra.Command, args []string) error {
938931
return err
939932
}
940933
return render(res, resolveOutputFormat(docCommentGetOutput), docCommentGetJQ, func() error {
941-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
942-
defer w.Flush()
943-
fmt.Fprintln(w, "SEQ\tATTENTION\tWRITER\tWRITE_DATE\tCONTENT")
934+
w := newTable(os.Stdout, "SEQ", "ATTENTION", "WRITER", "WRITE_DATE", "CONTENT")
944935
for _, cm := range res.CommentList {
945936
attention := "-"
946937
if cm.AttentionFlg {
947938
attention = "*"
948939
}
949-
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", cm.SeqNo, attention, cm.WriterName, cm.WriteDate, cm.Content)
940+
w.AddRow(cm.SeqNo, attention, cm.WriterName, cm.WriteDate, cm.Content)
950941
}
942+
w.Print()
951943
return nil
952944
})
953945
}
@@ -990,10 +982,9 @@ func runDocumentCommentEdit(cmd *cobra.Command, args []string) error {
990982
return err
991983
}
992984
return render(res, resolveOutputFormat(docCommentEditOutput), docCommentEditJQ, func() error {
993-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
994-
defer w.Flush()
995-
fmt.Fprintln(w, "DOCID\tSEQ\tMESSAGE_TYPE\tMESSAGE")
996-
fmt.Fprintf(w, "%d\t%d\t%d\t%s\n", res.DocID, res.Seq, res.MessageType, res.Message)
985+
w := newTable(os.Stdout, "DOCID", "SEQ", "MESSAGE_TYPE", "MESSAGE")
986+
w.AddRow(res.DocID, res.Seq, res.MessageType, res.Message)
987+
w.Print()
997988
return nil
998989
})
999990
}
@@ -1019,10 +1010,9 @@ func runDocumentCommentDelete(cmd *cobra.Command, args []string) error {
10191010
return err
10201011
}
10211012
return render(res, resolveOutputFormat(docCommentDeleteOutput), docCommentDeleteJQ, func() error {
1022-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
1023-
defer w.Flush()
1024-
fmt.Fprintln(w, "DOCID\tSEQ\tMESSAGE_TYPE\tMESSAGE")
1025-
fmt.Fprintf(w, "%d\t%d\t%d\t%s\n", res.DocID, res.Seq, res.MessageType, res.Message)
1013+
w := newTable(os.Stdout, "DOCID", "SEQ", "MESSAGE_TYPE", "MESSAGE")
1014+
w.AddRow(res.DocID, res.Seq, res.MessageType, res.Message)
1015+
w.Print()
10261016
return nil
10271017
})
10281018
}

cmd/document_attachment.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"path/filepath"
88
"strconv"
99
"strings"
10-
"text/tabwriter"
1110

1211
"github.com/pepabo/xpoint-cli/internal/xpoint"
1312
"github.com/spf13/cobra"
@@ -219,12 +218,11 @@ func runDocumentAttachmentList(cmd *cobra.Command, args []string) error {
219218
return err
220219
}
221220
return render(res, resolveOutputFormat(docAttachListOutput), docAttachListJQ, func() error {
222-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
223-
defer w.Flush()
224-
fmt.Fprintln(w, "SEQ\tNAME\tSIZE\tCONTENT_TYPE\tREMARKS")
221+
w := newTable(os.Stdout, "SEQ", "NAME", "SIZE", "CONTENT_TYPE", "REMARKS")
225222
for _, a := range res.Attachments {
226-
fmt.Fprintf(w, "%d\t%s\t%d\t%s\t%s\n", a.Seq, a.Name, a.Size, a.ContentType, a.Remarks)
223+
w.AddRow(a.Seq, a.Name, a.Size, a.ContentType, a.Remarks)
227224
}
225+
w.Print()
228226
return nil
229227
})
230228
}
@@ -338,10 +336,9 @@ func runDocumentAttachmentDelete(cmd *cobra.Command, args []string) error {
338336
}
339337

340338
func renderAttachmentMutation(res *xpoint.AttachmentMutationResponse) error {
341-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
342-
defer w.Flush()
343-
fmt.Fprintln(w, "DOCID\tSEQ\tMESSAGE_TYPE\tMESSAGE\tDETAIL")
344-
fmt.Fprintf(w, "%d\t%d\t%d\t%s\t%s\n", res.DocID, res.Seq, res.MessageType, res.Message, res.Detail)
339+
w := newTable(os.Stdout, "DOCID", "SEQ", "MESSAGE_TYPE", "MESSAGE", "DETAIL")
340+
w.AddRow(res.DocID, res.Seq, res.MessageType, res.Message, res.Detail)
341+
w.Print()
345342
return nil
346343
}
347344

cmd/form.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"os"
88
"strconv"
9-
"text/tabwriter"
109

1110
"github.com/pepabo/xpoint-cli/internal/xpoint"
1211
"github.com/spf13/cobra"
@@ -65,18 +64,17 @@ func runFormList(cmd *cobra.Command, args []string) error {
6564
}
6665

6766
return render(res, resolveOutputFormat(formListOutput), formListJQ, func() error {
68-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
69-
defer w.Flush()
70-
fmt.Fprintln(w, "GROUP_ID\tGROUP_NAME\tFORM_ID\tFORM_CODE\tFORM_NAME")
67+
w := newTable(os.Stdout, "GROUP_ID", "GROUP_NAME", "FORM_ID", "FORM_CODE", "FORM_NAME")
7168
for _, g := range res.FormGroup {
7269
if len(g.Form) == 0 {
73-
fmt.Fprintf(w, "%d\t%s\t-\t-\t-\n", g.ID, g.Name)
70+
w.AddRow(g.ID, g.Name, "-", "-", "-")
7471
continue
7572
}
7673
for _, f := range g.Form {
77-
fmt.Fprintf(w, "%d\t%s\t%d\t%s\t%s\n", g.ID, g.Name, f.ID, f.Code, f.Name)
74+
w.AddRow(g.ID, g.Name, f.ID, f.Code, f.Name)
7875
}
7976
}
77+
w.Print()
8078
return nil
8179
})
8280
}
@@ -100,16 +98,13 @@ func runFormShow(cmd *cobra.Command, args []string) error {
10098
return render(res, resolveOutputFormat(formShowOutput), formShowJQ, func() error {
10199
form := res.Form
102100
fmt.Fprintf(os.Stdout, "FORM: %s %s MAX_STEP: %d\n", form.Code, form.Name, form.MaxStep)
103-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
104-
defer w.Flush()
105-
fmt.Fprintln(w, "PAGE\tFIELD_ID\tTYPE\tREQUIRED\tUNIQUE\tARRAYSIZE\tLABEL")
101+
w := newTable(os.Stdout, "PAGE", "FIELD_ID", "TYPE", "REQUIRED", "UNIQUE", "ARRAYSIZE", "LABEL")
106102
for _, p := range form.Pages {
107103
for _, f := range p.Fields {
108-
fmt.Fprintf(w, "%d\t%s\t%d\t%t\t%t\t%d\t%s\n",
109-
p.PageNo, f.FieldID, f.FieldType, f.Required, f.Unique, f.ArraySize, f.Label,
110-
)
104+
w.AddRow(p.PageNo, f.FieldID, f.FieldType, f.Required, f.Unique, f.ArraySize, f.Label)
111105
}
112106
}
107+
w.Print()
113108
return nil
114109
})
115110
}

cmd/me.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"fmt"
55
"os"
6-
"text/tabwriter"
76

87
"github.com/spf13/cobra"
98
)
@@ -48,12 +47,12 @@ func runMe(cmd *cobra.Command, args []string) error {
4847
}
4948

5049
return render(info, resolveOutputFormat(meOutput), meJQ, func() error {
51-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
52-
defer w.Flush()
53-
fmt.Fprintf(w, "user_code:\t%s\n", info.AtledExt.UserCode)
54-
fmt.Fprintf(w, "user_name:\t%s\n", info.UserName)
55-
fmt.Fprintf(w, "display_name:\t%s\n", info.DisplayName)
56-
fmt.Fprintf(w, "scim_id:\t%s\n", info.ID)
50+
w := newList(os.Stdout)
51+
w.AddRow("user_code:", info.AtledExt.UserCode)
52+
w.AddRow("user_name:", info.UserName)
53+
w.AddRow("display_name:", info.DisplayName)
54+
w.AddRow("scim_id:", info.ID)
55+
w.Print()
5756
return nil
5857
})
5958
}

0 commit comments

Comments
 (0)