Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add command line arg delimiter for custom delimiter instead of tab. #662

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/cmd/issue/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ $ jira issue list --plain --columns key,assignee,status
# List issues in a plain table view and show all fields
$ jira issue list --plain --no-truncate

# List issues in a plain table view using custom delimiter (default is "\t")
$ jira issue list --plain --delimeter "|"

# List issues of type "Epic" in status "Done"
$ jira issue list -tEpic -sDone

Expand Down Expand Up @@ -110,6 +113,9 @@ func loadList(cmd *cobra.Command) {
plain, err := cmd.Flags().GetBool("plain")
cmdutil.ExitIfError(err)

delimiter, err := cmd.Flags().GetString("delimiter")
cmdutil.ExitIfError(err)

noHeaders, err := cmd.Flags().GetBool("no-headers")
cmdutil.ExitIfError(err)

Expand All @@ -132,6 +138,7 @@ func loadList(cmd *cobra.Command) {
},
Display: view.DisplayFormat{
Plain: plain,
Delimiter: delimiter,
NoHeaders: noHeaders,
NoTruncate: noTruncate,
FixedColumns: fixedColumns,
Expand Down Expand Up @@ -183,6 +190,7 @@ func SetFlags(cmd *cobra.Command) {
cmd.Flags().Bool("plain", false, "Display output in plain mode")
cmd.Flags().Bool("no-headers", false, "Don't display table headers in plain mode. Works only with --plain")
cmd.Flags().Bool("no-truncate", false, "Show all available columns in plain mode. Works only with --plain")
cmd.Flags().String("delimiter", "\t", "Custom delimeter for columns in plain mode. Works only with --plain")

if cmd.HasParent() && cmd.Parent().Name() != "sprint" {
cmd.Flags().String("columns", "", "Comma separated list of columns to display in the plain mode.\n"+
Expand Down
4 changes: 2 additions & 2 deletions internal/view/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ func copyKey() tui.CopyKeyFunc {
}
}

func renderPlain(w io.Writer, data tui.TableData) error {
func renderPlain(w io.Writer, data tui.TableData, delimiter string) error {
for _, items := range data {
n := len(items)
for j, v := range items {
fmt.Fprintf(w, "%s", v)
if j != n-1 {
fmt.Fprintf(w, "\t")
fmt.Fprintf(w, "%s", delimiter)
}
}
fmt.Fprintln(w)
Expand Down
12 changes: 9 additions & 3 deletions internal/view/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// DisplayFormat is a issue display type.
type DisplayFormat struct {
Plain bool
Delimiter string
NoHeaders bool
NoTruncate bool
Columns []string
Expand All @@ -38,8 +39,13 @@ type IssueList struct {
// Render renders the view.
func (l *IssueList) Render() error {
if l.Display.Plain || tui.IsDumbTerminal() || tui.IsNotTTY() {
// custom delimiter is used only in plain mode, otherwise \t is used
delimeter := "\t"
if l.Display.Plain {
delimeter = l.Display.Delimiter
}
w := tabwriter.NewWriter(os.Stdout, 0, tabWidth, 1, '\t', 0)
return l.renderPlain(w)
return l.renderPlain(w, delimeter)
}

renderer, err := MDRenderer()
Expand Down Expand Up @@ -123,8 +129,8 @@ func (l *IssueList) Render() error {
}

// renderPlain renders the issue in plain view.
func (l *IssueList) renderPlain(w io.Writer) error {
return renderPlain(w, l.data())
func (l *IssueList) renderPlain(w io.Writer, delimeter string) error {
return renderPlain(w, l.data(), delimeter)
}

func (*IssueList) validColumnsMap() map[string]struct{} {
Expand Down
31 changes: 27 additions & 4 deletions internal/view/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestIssueRenderInPlainView(t *testing.T) {
NoTruncate: false,
},
}
assert.NoError(t, issue.renderPlain(&b))
assert.NoError(t, issue.renderPlain(&b, "\t"))

expected := `TYPE KEY SUMMARY STATUS
Bug TEST-1 This is a test Done
Expand All @@ -61,6 +61,29 @@ Story TEST-2 This is another test Open
assert.Equal(t, expected, b.String())
}

func TestIssueRenderInPlainViewWithCustomDelimiter(t *testing.T) {
var b bytes.Buffer

issue := IssueList{
Total: 2,
Project: "TEST",
Server: "https://test.local",
Data: getIssues(),
Display: DisplayFormat{
Plain: true,
NoHeaders: false,
NoTruncate: false,
},
}
assert.NoError(t, issue.renderPlain(&b, "|"))

expected := `TYPE|KEY|SUMMARY|STATUS
Bug|TEST-1|This is a test|Done
Story|TEST-2|This is another test|Open
`
assert.Equal(t, expected, b.String())
}

func TestIssueRenderInPlainViewAndNoTruncate(t *testing.T) {
var b bytes.Buffer

Expand All @@ -75,7 +98,7 @@ func TestIssueRenderInPlainViewAndNoTruncate(t *testing.T) {
NoTruncate: true,
},
}
assert.NoError(t, issue.renderPlain(&b))
assert.NoError(t, issue.renderPlain(&b, "\t"))

expected := `TYPE KEY SUMMARY STATUS ASSIGNEE REPORTER PRIORITY RESOLUTION CREATED UPDATED LABELS
Bug TEST-1 This is a test Done Person A Person Z High Fixed 2020-12-13 14:05:20 2020-12-13 14:07:20 krakatit
Expand All @@ -98,7 +121,7 @@ func TestIssueRenderInPlainViewWithoutHeaders(t *testing.T) {
NoTruncate: true,
},
}
assert.NoError(t, issue.renderPlain(&b))
assert.NoError(t, issue.renderPlain(&b, "\t"))

expected := `Bug TEST-1 This is a test Done Person A Person Z High Fixed 2020-12-13 14:05:20 2020-12-13 14:07:20 krakatit
Story TEST-2 This is another test Open Person A Normal 2020-12-13 14:05:20 2020-12-13 14:07:20 pat,mat
Expand All @@ -122,7 +145,7 @@ func TestIssueRenderInPlainViewWithFewColumns(t *testing.T) {
Columns: []string{"key", "type", "status", "created"},
},
}
assert.NoError(t, issue.renderPlain(&b))
assert.NoError(t, issue.renderPlain(&b, "\t"))

expected := `KEY TYPE STATUS CREATED
TEST-1 Bug Done 2020-12-13 14:05:20
Expand Down
3 changes: 2 additions & 1 deletion internal/view/sprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ func (sl *SprintList) RenderInTable() error {

// renderPlain renders the issue in plain view.
func (sl *SprintList) renderPlain(w io.Writer) error {
return renderPlain(w, sl.tableData())
// sprint view supports only \t as delimiter, not custom.
return renderPlain(w, sl.tableData(), "\t")
}

func (sl *SprintList) data() []tui.PreviewData {
Expand Down