11import { existsSync , statSync } from "node:fs" ;
22import { Command } from "commander" ;
3- import type { CliInput , CommonOptions , LayoutMode , ParsedCliInput } from "./types" ;
3+ import type { CommonOptions , HelpCommandInput , LayoutMode , PagerCommandInput , ParsedCliInput } from "./types" ;
44
55/** Validate one requested layout mode from CLI input. */
66function parseLayoutMode ( value : string ) : LayoutMode {
@@ -82,6 +82,7 @@ function renderCliHelp() {
8282 " hunk show [ref] [-- <pathspec...>] review the last commit or a given ref" ,
8383 " hunk stash show [ref] review a stash entry" ,
8484 " hunk patch [file] review a patch file or stdin" ,
85+ " hunk pager general Git pager wrapper with diff detection" ,
8586 " hunk difftool <left> <right> [path] review Git difftool file pairs" ,
8687 " hunk git [range] legacy alias for git diff-style review" ,
8788 "" ,
@@ -97,6 +98,7 @@ function renderCliHelp() {
9798 " hunk show HEAD~1" ,
9899 " hunk show abc123 -- README.md" ,
99100 " hunk patch -" ,
101+ " hunk pager" ,
100102 "" ,
101103 ] . join ( "\n" ) ;
102104}
@@ -120,15 +122,14 @@ function areExistingFiles(left: string, right: string) {
120122}
121123
122124/** Parse one standalone command while letting us capture `--help` as plain text. */
123- async function parseStandaloneCommand < T > ( command : Command , tokens : string [ ] ) : Promise < T | null > {
125+ async function parseStandaloneCommand ( command : Command , tokens : string [ ] ) {
124126 command . exitOverride ( ) ;
125127
126128 try {
127129 await command . parseAsync ( [ "bun" , "hunk" , ...tokens ] ) ;
128- return null ;
129130 } catch ( error ) {
130131 if ( error && typeof error === "object" && "code" in error && error . code === "commander.helpDisplayed" ) {
131- return null ;
132+ return ;
132133 }
133134
134135 throw error ;
@@ -282,6 +283,27 @@ async function parsePatchCommand(tokens: string[], argv: string[]): Promise<Pars
282283 } ;
283284}
284285
286+ /** Parse the general pager wrapper command used from Git `core.pager`. */
287+ async function parsePagerCommand ( tokens : string [ ] , argv : string [ ] ) : Promise < PagerCommandInput | HelpCommandInput > {
288+ const command = createCommand ( "pager" , "general Git pager wrapper with diff detection" ) ;
289+ let parsedOptions : Record < string , unknown > = { } ;
290+
291+ command . action ( ( options : Record < string , unknown > ) => {
292+ parsedOptions = options ;
293+ } ) ;
294+
295+ if ( tokens . includes ( "--help" ) || tokens . includes ( "-h" ) ) {
296+ return { kind : "help" , text : `${ command . helpInformation ( ) . trimEnd ( ) } \n` } ;
297+ }
298+
299+ await parseStandaloneCommand ( command , tokens ) ;
300+
301+ return {
302+ kind : "pager" ,
303+ options : buildCommonOptions ( parsedOptions , argv ) ,
304+ } ;
305+ }
306+
285307/** Parse Git difftool-style two-file review commands. */
286308async function parseDifftoolCommand ( tokens : string [ ] , argv : string [ ] ) : Promise < ParsedCliInput > {
287309 const command = createCommand ( "difftool" , "review Git difftool file pairs" )
@@ -379,6 +401,8 @@ export async function parseCli(argv: string[]): Promise<ParsedCliInput> {
379401 return parseGitCommand ( rest , argv ) ;
380402 case "patch" :
381403 return parsePatchCommand ( rest , argv ) ;
404+ case "pager" :
405+ return parsePagerCommand ( rest , argv ) ;
382406 case "difftool" :
383407 return parseDifftoolCommand ( rest , argv ) ;
384408 case "stash" :
0 commit comments