import { gpExecSync } from './exec_sync' ; import { logInfo , logTip } from './splog' ; import chalk from 'chalk' ; import prompts from 'prompts' ; import { KilledError } from '../errors' ; import { userConfig } from '../../lib/config' ; import { DEFAULT_GRAPHITE_EDITOR } from '../../commands/user-commands/editor' ; export async function getDefaultEditorOrPrompt ( ) { await setDefaultEditorOrPrompt ( ) ; return userConfig . getEditor ( ) || DEFAULT_GRAPHITE_EDITOR ; } export function setDefaultEditor ( ) { const editor = gpExecSync ( { command : `echo \${GIT_EDITOR:-$EDITOR}` } ) . toString ( ) . trim ( ) || DEFAULT_GRAPHITE_EDITOR ; userConfig . setEditor ( editor ) ; } async function setDefaultEditorOrPrompt ( ) { if ( ! userConfig . getEditor ( ) ) { const systemEditor = gpExecSync ( { command : `echo \${GIT_EDITOR:-$EDITOR}` } ) . toString ( ) . trim ( ) ; let editorPref ; if ( systemEditor . length ) { editorPref = systemEditor ; logTip ( `Graphite will now use ${ editorPref } as the default editor setting. 
      We infer it from your environment variables ($GIT_EDITOR || $EDITOR). 
      If you wish to change it, use \`gt user editor\` to change this in the future` ) ; } else { logInfo ( chalk . yellow ( `We did not detect an editor preference in your settings. Do you wish to set it? (Graphite will use ${ DEFAULT_GRAPHITE_EDITOR } as default.)` ) ) ; const yesOrNo = await prompts ( { type : 'select' , name : 'editorPrompt' , message : 'Set default editor now?' , choices : [ { title : `Yes` , value : 'yes' } , { title : `Skip and use Graphite default (${ DEFAULT_GRAPHITE_EDITOR })` , value : 'no' , } , ] , } , { onCancel : ( ) => { throw new KilledError ( ) ; } , } ) ; if ( yesOrNo . editorPrompt === 'yes' ) { const response = await prompts ( { type : 'select' , name : 'editor' , message : 'Select an editor:' , choices : [ { title : `vim` , value : 'vim' } , { title : `emacs` , value : 'emacs' } , { title : `nano` , value : 'nano' } , ] , } , { onCancel : ( ) => { throw new KilledError ( ) ; } , } ) ; editorPref = response . editor ; } else { editorPref = DEFAULT_GRAPHITE_EDITOR ; } } userConfig . setEditor ( editorPref ) ; logInfo ( chalk . yellow ( `Graphite editor preference set to ${ editorPref }.` ) ) ; } }