@@ -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.
@@ -496,6 +500,8 @@ export class CliRepl implements MongoshIOProvider {
496500 } ) ;
497501 }
498502
503+ this . setupHistoryCommand ( ) ;
504+
499505 Editor . create ( {
500506 input : this . input ,
501507 vscodeDir : this . shellHomeDirectory . rcPath ( '.vscode' ) ,
@@ -910,6 +916,47 @@ export class CliRepl implements MongoshIOProvider {
910916 return this . shellHomeDirectory . roamingPath ( 'mongosh_repl_history' ) ;
911917 }
912918
919+ /** Returns the history of commands. */
920+ setupHistoryCommand ( ) : void {
921+ const getHistory = ( ) => {
922+ const replHistory : string [ ] = (
923+ this . mongoshRepl . runtimeState ( ) . repl as any
924+ ) . history ;
925+ const formattedHistory =
926+ // Remove the history call from the formatted history
927+ replHistory . slice ( 1 , replHistory . length ) . reverse ( ) ;
928+
929+ return {
930+ ...formattedHistory ,
931+ [ util . inspect . custom ] : ( ) => {
932+ return formatOutput (
933+ {
934+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
935+ value : formattedHistory ,
936+ } ,
937+ { colors : true , maxArrayLength : Infinity }
938+ ) ;
939+ } ,
940+ } ;
941+ } ;
942+
943+ getHistory . isDirectShellCommand = true ;
944+ getHistory . returnsPromise = true ;
945+ getHistory . acceptsRawInput = true ;
946+
947+ this . mongoshRepl . runtimeState ( ) . context . history =
948+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
949+ ( this . mongoshRepl . runtimeState ( ) . instanceState . shellApi as any ) . history =
950+ getHistory ;
951+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
952+ ( signatures . ShellApi . attributes as any ) . history = {
953+ type : 'function' ,
954+ returnsPromise : true ,
955+ isDirectShellCommand : true ,
956+ acceptsRawInput : true ,
957+ } as TypeSignature ;
958+ }
959+
913960 /**
914961 * Implements getConfig from the {@link ConfigProvider} interface.
915962 */
0 commit comments