feat(cli): open a file at path and line from the command line#321
Open
jongio wants to merge 1 commit into
Open
feat(cli): open a file at path and line from the command line#321jongio wants to merge 1 commit into
jongio wants to merge 1 commit into
Conversation
grut only accepted a directory as its positional argument, so jumping to a specific file meant opening grut and then navigating the tree by hand. Now the argument can also name a file, optionally with a ":line" suffix. Passing a file roots grut at that file's directory, selects it in the File Tree, focuses the Preview panel, and scrolls to the line when a ":line" suffix is present. Passing a directory behaves exactly as before. A path that does not exist, including a bogus "name:42", is ignored and grut opens in the current directory, matching the prior behavior. Argument parsing lives in a pure resolveStartupTarget helper with an injected stat function so the file-vs-directory and ":line" cases are unit tested without touching the filesystem. The last colon is used as the line separator so Windows drive letters such as C:\path are not misread. Closes #317 Co-authored-by: Copilot App <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Lets
grutaccept a file path, optionally with a line number, as its positional argument.grut internal/git/client.goopens with that file selected and shown in the Preview panel;grut internal/git/client.go:120also scrolls the preview to line 120. Passing a directory works exactly as before.Closes #317
Why
grut [dir]only took a directory, so opening a specific file meant launching grut and walking the tree by hand. Accepting a file makes grut a usable jump target from shell pipelines and quick edits, for examplegrut $(git rev-parse --show-toplevel)/README.md:1.How
cmd/startup_target.goadds a pureresolveStartupTarget(arg, statFn)helper that classifies the argument. An existing directory roots grut there; an existing file roots grut at the file's directory and selects the file; afile:linereference where the file exists and the line is a positive integer opens that file at the line. Anything else resolves to an empty target and grut opens in the current directory as before. The stat function is injected so the parsing is unit tested without the filesystem, and the last colon is used as the line separator so Windows drive letters likeC:\pathare not misread.cmd/root.gosetsUse: "grut [path]"andArgs: cobra.MaximumNArgs(1)(required because the root command has subcommands, otherwise cobra rejects any positional arg), resolves the argument right after the--demoblock, andos.Chdirs into the target directory before the git client and file tree pick up the working directory. Resolved paths are absolute so the chdir does not invalidate them.internal/tui/app.gogainsWithInitialFile(path, line). When a file is set,Initfocuses the preview panel and emits aRevealFileMsgcarrying the path and line.internal/panels/messages.goadds a 1-basedLinefield toFileSelectedMsgandRevealFileMsg(0 means no line).internal/panels/filetreethreads the line through when revealing the cursor file, andinternal/panels/previewremembers a pending goto-line and applies it once the file content has loaded (the load is async, so the jump runs onfileLoadedMsg, not on selection).Testing
go test ./cmd/... ./internal/panels/... ./internal/tui/... -count=1passes, including new tests for the argument resolver (directory, file,file:line, nonexistent path, trailing colon, non-numeric and zero lines, and a Windows drive-colon case), the cobraArgsvalidation, filetree line threading, preview scroll-on-load, and preview staying at the top when no line is given.go build ./...,go vet,gofumpt -l, andgolangci-lint run ./cmd/... ./internal/panels/... ./internal/tui/...are clean.