import { Arguments , ParsedOptions , RadioTable , Registrar } from '@kui-shell/core' ; import { doExecWithStdoutViaPty , doExecWithPty } from '@kui-shell/plugin-bash-like' ; const stripAnsi = require ( 'strip-ansi' ) ; interface GitBranchOptions extends ParsedOptions { a : boolean ; all : boolean ; f : boolean ; force : boolean ; i : boolean ; 'ignore-case' : boolean ; d : boolean ; delete : boolean ; D : boolean ; m : boolean ; move : boolean ; c : boolean ; copy : boolean ; C : boolean ; l : boolean ; list : boolean ; 'create-reflog' : boolean ; 'edit-description' : boolean ; v : boolean ; verbose : boolean ; q : boolean ; quiet : boolean ; t : boolean ; track : boolean ; u : boolean ; 'set-upstream-to' : boolean ; 'unset-upstream' : boolean ; r : boolean ; remotes : boolean ; } function noOptions ( parsedOptions ) { return Object . keys ( parsedOptions ) . length === 1 ; } function isList ( { argvNoOptions , parsedOptions } ) { return ( argvNoOptions . length === 2 && ! ! ( parsedOptions . a || parsedOptions . all || parsedOptions . l || parsedOptions . list || noOptions ( parsedOptions ) ) ) ; } async function doListBranches ( args ) { args . argvNoOptions . push ( '--no-pager' ) ; args . argvNoOptions . push ( '--color=never' ) ; args . command = args . command . replace ( 'git' , 'git --no-pager' ) ; args . command = `${ args . command } --color=never` ; const response = stripAnsi ( await doExecWithStdoutViaPty ( args ) ) ; let defaultSelectedIdx = 0 ; const body = response . trim ( ) . split ( /\n/ ) . map ( ( line , idx ) => { const isSelected = line . charAt ( 0 ) === '*' ; const branchName = line . replace ( /^\*?\s*/ , '' ) . trim ( ) ; if ( isSelected ) { defaultSelectedIdx = idx ; } return { nameIdx : 0 , cells : [ branchName ] , onSelect : `git checkout ${ branchName }` } ; } ) ; const radio = { apiVersion : 'kui-shell/v1' , kind : 'RadioTable' , title : 'Git Branches' , defaultSelectedIdx , header : { cells : [ 'Branch' ] } , body } ; return radio ; } function gitBranch ( args ) { if ( isList ( args ) ) { return doListBranches ( args ) ; } else { return doExecWithPty ( args ) ; } } export default function ( registrar ) { registrar . listen ( '/git/branch' , gitBranch , { flags : { boolean : [ 'a' , 'all' , 'f' , 'force' , 'i' , 'ignore-case' , 'd' , 'delete' , 'D' , 'm' , 'move' , 'c' , 'copy' , 'C' , 'l' , 'list' , 'create-reflog' , 'edit-description' , 'v' , 'verbose' , 'q' , 'quiet' , 't' , 'track' , 'u' , 'set-upstream-to' , 'unset-upstream' , 'r' , 'remotes' ] } } ) ; }