Skip to content

Commit dafd0bf

Browse files
committed
feat(4248): add support for line range w/ browse
Allow using a range of lines when browsing files. For example: `gh browse test.go:10-20` Closes cli#4248
1 parent 4fa984a commit dafd0bf

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

pkg/cmd/browse/browse.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,28 @@ func parseFileArg(fileArg string) (string, error) {
169169
if len(arr) > 2 {
170170
return "", fmt.Errorf("invalid use of colon\nUse 'gh browse --help' for more information about browse\n")
171171
}
172+
172173
if len(arr) > 1 {
173-
if !isNumber(arr[1]) {
174-
return "", fmt.Errorf("invalid line number after colon\nUse 'gh browse --help' for more information about browse\n")
174+
out := arr[0] + "#L"
175+
lineRange := strings.Split(arr[1], "-")
176+
177+
if len(lineRange) > 0 {
178+
if !isNumber(lineRange[0]) {
179+
return "", fmt.Errorf("invalid line number after colon\nUse 'gh browse --help' for more information about browse\n")
180+
}
181+
out += lineRange[0]
182+
}
183+
184+
if len(lineRange) > 1 {
185+
if !isNumber(lineRange[1]) {
186+
return "", fmt.Errorf("invalid line range after colon\nUse 'gh browse --help' for more information about browse\n")
187+
}
188+
out += "-L" + lineRange[1]
175189
}
176-
return arr[0] + "#L" + arr[1], nil
190+
191+
return out, nil
177192
}
193+
178194
return arr[0], nil
179195
}
180196

pkg/cmd/browse/browse_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ func Test_runBrowse(t *testing.T) {
215215
defaultBranch: "trunk",
216216
expectedURL: "https://github.com/ravocean/angur/tree/trunk/path/to/file.txt#L32",
217217
},
218+
{
219+
name: "file with line range",
220+
opts: BrowseOptions{
221+
SelectorArg: "path/to/file.txt:32-40",
222+
},
223+
baseRepo: ghrepo.New("ravocean", "angur"),
224+
defaultBranch: "trunk",
225+
expectedURL: "https://github.com/ravocean/angur/tree/trunk/path/to/file.txt#L32-L40",
226+
},
218227
{
219228
name: "file with invalid line number",
220229
opts: BrowseOptions{
@@ -223,6 +232,14 @@ func Test_runBrowse(t *testing.T) {
223232
baseRepo: ghrepo.New("ttran112", "ttrain211"),
224233
wantsErr: true,
225234
},
235+
{
236+
name: "file with invalid line range",
237+
opts: BrowseOptions{
238+
SelectorArg: "path/to/file.txt:32-abc",
239+
},
240+
baseRepo: ghrepo.New("ttran112", "ttrain211"),
241+
wantsErr: true,
242+
},
226243
{
227244
name: "branch with issue number",
228245
opts: BrowseOptions{

0 commit comments

Comments
 (0)