@@ -54,6 +54,10 @@ import type {
5454 DevtoolsProxyOptions ,
5555} from '@mongodb-js/devtools-proxy-support' ;
5656import { useOrCreateAgent } from '@mongodb-js/devtools-proxy-support' ;
57+ import type { TypeSignature } from '@mongosh/shell-api' ;
58+ import { signatures } from '@mongosh/shell-api' ;
59+ import util from 'util' ;
60+ import { formatOutput } from './format-output' ;
5761
5862/**
5963 * Connecting text key.
@@ -488,6 +492,8 @@ export class CliRepl implements MongoshIOProvider {
488492 } ) ;
489493 }
490494
495+ this . setupHistoryCommand ( ) ;
496+
491497 Editor . create ( {
492498 input : this . input ,
493499 vscodeDir : this . shellHomeDirectory . rcPath ( '.vscode' ) ,
@@ -902,6 +908,47 @@ export class CliRepl implements MongoshIOProvider {
902908 return this . shellHomeDirectory . roamingPath ( 'mongosh_repl_history' ) ;
903909 }
904910
911+ /** Returns the history of commands. */
912+ setupHistoryCommand ( ) : void {
913+ const getHistory = ( ) => {
914+ const replHistory : string [ ] = (
915+ this . mongoshRepl . runtimeState ( ) . repl as any
916+ ) . history ;
917+ const formattedHistory =
918+ // Remove the history call from the formatted history
919+ replHistory . slice ( 1 , replHistory . length ) . reverse ( ) ;
920+
921+ return {
922+ ...formattedHistory ,
923+ [ util . inspect . custom ] : ( ) => {
924+ return formatOutput (
925+ {
926+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
927+ value : formattedHistory ,
928+ } ,
929+ { colors : true , maxArrayLength : Infinity }
930+ ) ;
931+ } ,
932+ } ;
933+ } ;
934+
935+ getHistory . isDirectShellCommand = true ;
936+ getHistory . returnsPromise = true ;
937+ getHistory . acceptsRawInput = true ;
938+
939+ this . mongoshRepl . runtimeState ( ) . context . history =
940+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
941+ ( this . mongoshRepl . runtimeState ( ) . instanceState . shellApi as any ) . history =
942+ getHistory ;
943+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
944+ ( signatures . ShellApi . attributes as any ) . history = {
945+ type : 'function' ,
946+ returnsPromise : true ,
947+ isDirectShellCommand : true ,
948+ acceptsRawInput : true ,
949+ } as TypeSignature ;
950+ }
951+
905952 /**
906953 * Implements getConfig from the {@link ConfigProvider} interface.
907954 */
0 commit comments