From 8e86129e2eb37830fd88bb2e484a4c4d225e986c Mon Sep 17 00:00:00 2001 From: Ruslan Gilyazetdinov Date: Mon, 1 Feb 2021 18:49:26 +0300 Subject: [PATCH 1/4] remove gist description from single file raw view (#2886) --- pkg/cmd/gist/view/view.go | 10 +++++----- pkg/cmd/gist/view/view_test.go | 23 ++++++++++++++++++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/gist/view/view.go b/pkg/cmd/gist/view/view.go index 9d8b41110..31b48325f 100644 --- a/pkg/cmd/gist/view/view.go +++ b/pkg/cmd/gist/view/view.go @@ -89,11 +89,6 @@ func viewRun(opts *ViewOptions) error { return err } - cs := opts.IO.ColorScheme() - if gist.Description != "" { - fmt.Fprintf(opts.IO.Out, "%s\n", cs.Bold(gist.Description)) - } - if opts.Filename != "" { gistFile, ok := gist.Files[opts.Filename] if !ok { @@ -107,6 +102,11 @@ func viewRun(opts *ViewOptions) error { showFilenames := len(gist.Files) > 1 + cs := opts.IO.ColorScheme() + if gist.Description != "" && showFilenames { + fmt.Fprintf(opts.IO.Out, "%s\n", cs.Bold(gist.Description)) + } + outs := []string{} // to ensure consistent ordering for filename, gistFile := range gist.Files { diff --git a/pkg/cmd/gist/view/view_test.go b/pkg/cmd/gist/view/view_test.go index 0ddf55f49..832a9ae01 100644 --- a/pkg/cmd/gist/view/view_test.go +++ b/pkg/cmd/gist/view/view_test.go @@ -131,6 +131,27 @@ func Test_viewRun(t *testing.T) { }, wantOut: "bwhiizzzbwhuiiizzzz\n\n", }, + { + name: "filename selected, raw", + opts: &ViewOptions{ + Selector: "1234", + Filename: "cicada.txt", + Raw: true, + }, + gist: &shared.Gist{ + Files: map[string]*shared.GistFile{ + "cicada.txt": { + Content: "bwhiizzzbwhuiiizzzz", + Type: "text/plain", + }, + "foo.md": { + Content: "# foo", + Type: "application/markdown", + }, + }, + }, + wantOut: "bwhiizzzbwhuiiizzzz\n\n", + }, { name: "multiple files, no description", opts: &ViewOptions{ @@ -171,7 +192,7 @@ func Test_viewRun(t *testing.T) { wantOut: "some files\ncicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n\n \n • foo \n\n\n\n", }, { - name: "raw", + name: "multiple files, raw", opts: &ViewOptions{ Selector: "1234", Raw: true, From 232dc7b7fa6559de74bd6b4b5902fd7985a772fe Mon Sep 17 00:00:00 2001 From: Ruslan Gilyazetdinov Date: Tue, 2 Feb 2021 10:54:07 +0300 Subject: [PATCH 2/4] change condition for single file, remove empty lines in single file mode --- pkg/cmd/gist/view/view.go | 15 +++++++++------ pkg/cmd/gist/view/view_test.go | 4 ++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pkg/cmd/gist/view/view.go b/pkg/cmd/gist/view/view.go index 31b48325f..4f95af803 100644 --- a/pkg/cmd/gist/view/view.go +++ b/pkg/cmd/gist/view/view.go @@ -89,6 +89,11 @@ func viewRun(opts *ViewOptions) error { return err } + cs := opts.IO.ColorScheme() + if gist.Description != "" && opts.Filename == "" { + fmt.Fprintf(opts.IO.Out, "%s\n", cs.Bold(gist.Description)) + } + if opts.Filename != "" { gistFile, ok := gist.Files[opts.Filename] if !ok { @@ -102,11 +107,6 @@ func viewRun(opts *ViewOptions) error { showFilenames := len(gist.Files) > 1 - cs := opts.IO.ColorScheme() - if gist.Description != "" && showFilenames { - fmt.Fprintf(opts.IO.Out, "%s\n", cs.Bold(gist.Description)) - } - outs := []string{} // to ensure consistent ordering for filename, gistFile := range gist.Files { @@ -122,7 +122,10 @@ func viewRun(opts *ViewOptions) error { content = rendered } } - out += fmt.Sprintf("%s\n\n", content) + out += fmt.Sprintf("%s", content) + if opts.Filename == "" { + out += fmt.Sprintf("\n\n") + } outs = append(outs, out) } diff --git a/pkg/cmd/gist/view/view_test.go b/pkg/cmd/gist/view/view_test.go index 832a9ae01..4ecd86538 100644 --- a/pkg/cmd/gist/view/view_test.go +++ b/pkg/cmd/gist/view/view_test.go @@ -129,7 +129,7 @@ func Test_viewRun(t *testing.T) { }, }, }, - wantOut: "bwhiizzzbwhuiiizzzz\n\n", + wantOut: "bwhiizzzbwhuiiizzzz", }, { name: "filename selected, raw", @@ -150,7 +150,7 @@ func Test_viewRun(t *testing.T) { }, }, }, - wantOut: "bwhiizzzbwhuiiizzzz\n\n", + wantOut: "bwhiizzzbwhuiiizzzz", }, { name: "multiple files, no description", From bf4370bc3ac8498f238c2d788773c650c0125ae3 Mon Sep 17 00:00:00 2001 From: Ruslan Gilyazetdinov Date: Tue, 2 Feb 2021 10:56:29 +0300 Subject: [PATCH 3/4] linter fixes --- pkg/cmd/gist/view/view.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/gist/view/view.go b/pkg/cmd/gist/view/view.go index 4f95af803..69186ae59 100644 --- a/pkg/cmd/gist/view/view.go +++ b/pkg/cmd/gist/view/view.go @@ -122,9 +122,9 @@ func viewRun(opts *ViewOptions) error { content = rendered } } - out += fmt.Sprintf("%s", content) + out += content if opts.Filename == "" { - out += fmt.Sprintf("\n\n") + out += "\n\n" } outs = append(outs, out) From 3f7b1387e55377d1644e5f606bc18a9d057b9d49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 2 Feb 2021 13:18:23 +0100 Subject: [PATCH 4/4] Improve `gist view` rendering - Separate out logic to render a single file - Render directly to stdout instead of to string slice - Normalize whitespace between files; ensure no excessive trailing whitespace - Add terminal pager support - Sentence-case for flags --- pkg/cmd/gist/view/view.go | 85 ++++++++++++++++++++-------------- pkg/cmd/gist/view/view_test.go | 31 ++++++++++--- 2 files changed, 74 insertions(+), 42 deletions(-) diff --git a/pkg/cmd/gist/view/view.go b/pkg/cmd/gist/view/view.go index 69186ae59..e81bbeba6 100644 --- a/pkg/cmd/gist/view/view.go +++ b/pkg/cmd/gist/view/view.go @@ -49,9 +49,9 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman }, } - cmd.Flags().BoolVarP(&opts.Raw, "raw", "r", false, "do not try and render markdown") - cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "open gist in browser") - cmd.Flags().StringVarP(&opts.Filename, "filename", "f", "", "display a single file of the gist") + cmd.Flags().BoolVarP(&opts.Raw, "raw", "r", false, "Print raw instead of rendered gist contents") + cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "Open gist in the browser") + cmd.Flags().StringVarP(&opts.Filename, "filename", "f", "", "Display a single file from the gist") return cmd } @@ -89,51 +89,64 @@ func viewRun(opts *ViewOptions) error { return err } - cs := opts.IO.ColorScheme() - if gist.Description != "" && opts.Filename == "" { - fmt.Fprintf(opts.IO.Out, "%s\n", cs.Bold(gist.Description)) + theme := opts.IO.DetectTerminalTheme() + markdownStyle := markdown.GetStyle(theme) + if err := opts.IO.StartPager(); err != nil { + fmt.Fprintf(opts.IO.ErrOut, "starting pager failed: %v\n", err) + } + defer opts.IO.StopPager() + + render := func(gf *shared.GistFile) error { + if strings.Contains(gf.Type, "markdown") && !opts.Raw { + rendered, err := markdown.Render(gf.Content, markdownStyle, "") + if err != nil { + return err + } + _, err = fmt.Fprint(opts.IO.Out, rendered) + return err + } + + if _, err := fmt.Fprint(opts.IO.Out, gf.Content); err != nil { + return err + } + if !strings.HasSuffix(gf.Content, "\n") { + _, err := fmt.Fprint(opts.IO.Out, "\n") + return err + } + return nil } if opts.Filename != "" { gistFile, ok := gist.Files[opts.Filename] if !ok { - return fmt.Errorf("gist has no such file %q", opts.Filename) + return fmt.Errorf("gist has no such file: %q", opts.Filename) } + return render(gistFile) + } - gist.Files = map[string]*shared.GistFile{ - opts.Filename: gistFile, - } + cs := opts.IO.ColorScheme() + + if gist.Description != "" { + fmt.Fprintf(opts.IO.Out, "%s\n\n", cs.Bold(gist.Description)) } showFilenames := len(gist.Files) > 1 - - outs := []string{} // to ensure consistent ordering - - for filename, gistFile := range gist.Files { - out := "" - if showFilenames { - out += fmt.Sprintf("%s\n\n", cs.Gray(filename)) - } - content := gistFile.Content - if strings.Contains(gistFile.Type, "markdown") && !opts.Raw { - style := markdown.GetStyle(opts.IO.DetectTerminalTheme()) - rendered, err := markdown.Render(gistFile.Content, style, "") - if err == nil { - content = rendered - } - } - out += content - if opts.Filename == "" { - out += "\n\n" - } - - outs = append(outs, out) + filenames := make([]string, 0, len(gist.Files)) + for fn := range gist.Files { + filenames = append(filenames, fn) } + sort.Strings(filenames) - sort.Strings(outs) - - for _, out := range outs { - fmt.Fprint(opts.IO.Out, out) + for i, fn := range filenames { + if showFilenames { + fmt.Fprintf(opts.IO.Out, "%s\n\n", cs.Gray(fn)) + } + if err := render(gist.Files[fn]); err != nil { + return err + } + if i < len(filenames)-1 { + fmt.Fprint(opts.IO.Out, "\n") + } } return nil diff --git a/pkg/cmd/gist/view/view_test.go b/pkg/cmd/gist/view/view_test.go index 4ecd86538..a296a10fb 100644 --- a/pkg/cmd/gist/view/view_test.go +++ b/pkg/cmd/gist/view/view_test.go @@ -109,7 +109,7 @@ func Test_viewRun(t *testing.T) { }, }, }, - wantOut: "bwhiizzzbwhuiiizzzz\n\n", + wantOut: "bwhiizzzbwhuiiizzzz\n", }, { name: "filename selected", @@ -129,7 +129,7 @@ func Test_viewRun(t *testing.T) { }, }, }, - wantOut: "bwhiizzzbwhuiiizzzz", + wantOut: "bwhiizzzbwhuiiizzzz\n", }, { name: "filename selected, raw", @@ -150,7 +150,7 @@ func Test_viewRun(t *testing.T) { }, }, }, - wantOut: "bwhiizzzbwhuiiizzzz", + wantOut: "bwhiizzzbwhuiiizzzz\n", }, { name: "multiple files, no description", @@ -169,7 +169,26 @@ func Test_viewRun(t *testing.T) { }, }, }, - wantOut: "cicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n\n # foo \n\n\n\n", + wantOut: "cicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n\n # foo \n\n", + }, + { + name: "multiple files, trailing newlines", + opts: &ViewOptions{ + Selector: "1234", + }, + gist: &shared.Gist{ + Files: map[string]*shared.GistFile{ + "cicada.txt": { + Content: "bwhiizzzbwhuiiizzzz\n", + Type: "text/plain", + }, + "foo.txt": { + Content: "bar\n", + Type: "text/plain", + }, + }, + }, + wantOut: "cicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.txt\n\nbar\n", }, { name: "multiple files, description", @@ -189,7 +208,7 @@ func Test_viewRun(t *testing.T) { }, }, }, - wantOut: "some files\ncicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n\n \n • foo \n\n\n\n", + wantOut: "some files\n\ncicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n\n \n • foo \n\n", }, { name: "multiple files, raw", @@ -210,7 +229,7 @@ func Test_viewRun(t *testing.T) { }, }, }, - wantOut: "some files\ncicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n- foo\n\n", + wantOut: "some files\n\ncicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n- foo\n", }, }