Skip to content

Commit 962791b

Browse files
authored
Merge pull request cli#2888 from Ma3oBblu/gist-view-raw-fix
remove gist description from single file raw view (cli#2886)
2 parents de5c04f + 3f7b138 commit 962791b

File tree

2 files changed

+93
-37
lines changed

2 files changed

+93
-37
lines changed

pkg/cmd/gist/view/view.go

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
4949
},
5050
}
5151

52-
cmd.Flags().BoolVarP(&opts.Raw, "raw", "r", false, "do not try and render markdown")
53-
cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "open gist in browser")
54-
cmd.Flags().StringVarP(&opts.Filename, "filename", "f", "", "display a single file of the gist")
52+
cmd.Flags().BoolVarP(&opts.Raw, "raw", "r", false, "Print raw instead of rendered gist contents")
53+
cmd.Flags().BoolVarP(&opts.Web, "web", "w", false, "Open gist in the browser")
54+
cmd.Flags().StringVarP(&opts.Filename, "filename", "f", "", "Display a single file from the gist")
5555

5656
return cmd
5757
}
@@ -89,48 +89,64 @@ func viewRun(opts *ViewOptions) error {
8989
return err
9090
}
9191

92-
cs := opts.IO.ColorScheme()
93-
if gist.Description != "" {
94-
fmt.Fprintf(opts.IO.Out, "%s\n", cs.Bold(gist.Description))
92+
theme := opts.IO.DetectTerminalTheme()
93+
markdownStyle := markdown.GetStyle(theme)
94+
if err := opts.IO.StartPager(); err != nil {
95+
fmt.Fprintf(opts.IO.ErrOut, "starting pager failed: %v\n", err)
96+
}
97+
defer opts.IO.StopPager()
98+
99+
render := func(gf *shared.GistFile) error {
100+
if strings.Contains(gf.Type, "markdown") && !opts.Raw {
101+
rendered, err := markdown.Render(gf.Content, markdownStyle, "")
102+
if err != nil {
103+
return err
104+
}
105+
_, err = fmt.Fprint(opts.IO.Out, rendered)
106+
return err
107+
}
108+
109+
if _, err := fmt.Fprint(opts.IO.Out, gf.Content); err != nil {
110+
return err
111+
}
112+
if !strings.HasSuffix(gf.Content, "\n") {
113+
_, err := fmt.Fprint(opts.IO.Out, "\n")
114+
return err
115+
}
116+
return nil
95117
}
96118

97119
if opts.Filename != "" {
98120
gistFile, ok := gist.Files[opts.Filename]
99121
if !ok {
100-
return fmt.Errorf("gist has no such file %q", opts.Filename)
122+
return fmt.Errorf("gist has no such file: %q", opts.Filename)
101123
}
124+
return render(gistFile)
125+
}
102126

103-
gist.Files = map[string]*shared.GistFile{
104-
opts.Filename: gistFile,
105-
}
127+
cs := opts.IO.ColorScheme()
128+
129+
if gist.Description != "" {
130+
fmt.Fprintf(opts.IO.Out, "%s\n\n", cs.Bold(gist.Description))
106131
}
107132

108133
showFilenames := len(gist.Files) > 1
134+
filenames := make([]string, 0, len(gist.Files))
135+
for fn := range gist.Files {
136+
filenames = append(filenames, fn)
137+
}
138+
sort.Strings(filenames)
109139

110-
outs := []string{} // to ensure consistent ordering
111-
112-
for filename, gistFile := range gist.Files {
113-
out := ""
140+
for i, fn := range filenames {
114141
if showFilenames {
115-
out += fmt.Sprintf("%s\n\n", cs.Gray(filename))
142+
fmt.Fprintf(opts.IO.Out, "%s\n\n", cs.Gray(fn))
116143
}
117-
content := gistFile.Content
118-
if strings.Contains(gistFile.Type, "markdown") && !opts.Raw {
119-
style := markdown.GetStyle(opts.IO.DetectTerminalTheme())
120-
rendered, err := markdown.Render(gistFile.Content, style, "")
121-
if err == nil {
122-
content = rendered
123-
}
144+
if err := render(gist.Files[fn]); err != nil {
145+
return err
146+
}
147+
if i < len(filenames)-1 {
148+
fmt.Fprint(opts.IO.Out, "\n")
124149
}
125-
out += fmt.Sprintf("%s\n\n", content)
126-
127-
outs = append(outs, out)
128-
}
129-
130-
sort.Strings(outs)
131-
132-
for _, out := range outs {
133-
fmt.Fprint(opts.IO.Out, out)
134150
}
135151

136152
return nil

pkg/cmd/gist/view/view_test.go

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func Test_viewRun(t *testing.T) {
109109
},
110110
},
111111
},
112-
wantOut: "bwhiizzzbwhuiiizzzz\n\n",
112+
wantOut: "bwhiizzzbwhuiiizzzz\n",
113113
},
114114
{
115115
name: "filename selected",
@@ -129,7 +129,28 @@ func Test_viewRun(t *testing.T) {
129129
},
130130
},
131131
},
132-
wantOut: "bwhiizzzbwhuiiizzzz\n\n",
132+
wantOut: "bwhiizzzbwhuiiizzzz\n",
133+
},
134+
{
135+
name: "filename selected, raw",
136+
opts: &ViewOptions{
137+
Selector: "1234",
138+
Filename: "cicada.txt",
139+
Raw: true,
140+
},
141+
gist: &shared.Gist{
142+
Files: map[string]*shared.GistFile{
143+
"cicada.txt": {
144+
Content: "bwhiizzzbwhuiiizzzz",
145+
Type: "text/plain",
146+
},
147+
"foo.md": {
148+
Content: "# foo",
149+
Type: "application/markdown",
150+
},
151+
},
152+
},
153+
wantOut: "bwhiizzzbwhuiiizzzz\n",
133154
},
134155
{
135156
name: "multiple files, no description",
@@ -148,7 +169,26 @@ func Test_viewRun(t *testing.T) {
148169
},
149170
},
150171
},
151-
wantOut: "cicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n\n # foo \n\n\n\n",
172+
wantOut: "cicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n\n # foo \n\n",
173+
},
174+
{
175+
name: "multiple files, trailing newlines",
176+
opts: &ViewOptions{
177+
Selector: "1234",
178+
},
179+
gist: &shared.Gist{
180+
Files: map[string]*shared.GistFile{
181+
"cicada.txt": {
182+
Content: "bwhiizzzbwhuiiizzzz\n",
183+
Type: "text/plain",
184+
},
185+
"foo.txt": {
186+
Content: "bar\n",
187+
Type: "text/plain",
188+
},
189+
},
190+
},
191+
wantOut: "cicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.txt\n\nbar\n",
152192
},
153193
{
154194
name: "multiple files, description",
@@ -168,10 +208,10 @@ func Test_viewRun(t *testing.T) {
168208
},
169209
},
170210
},
171-
wantOut: "some files\ncicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n\n \n • foo \n\n\n\n",
211+
wantOut: "some files\n\ncicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n\n \n • foo \n\n",
172212
},
173213
{
174-
name: "raw",
214+
name: "multiple files, raw",
175215
opts: &ViewOptions{
176216
Selector: "1234",
177217
Raw: true,
@@ -189,7 +229,7 @@ func Test_viewRun(t *testing.T) {
189229
},
190230
},
191231
},
192-
wantOut: "some files\ncicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n- foo\n\n",
232+
wantOut: "some files\n\ncicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n- foo\n",
193233
},
194234
}
195235

0 commit comments

Comments
 (0)