瀏覽代碼

Merge pull request #2866 from blink1073/terminal-command

Allow an initial command to be passed to a terminal
Brian E. Granger 7 年之前
父節點
當前提交
e9a2db41a5
共有 2 個文件被更改,包括 18 次插入3 次删除
  1. 3 2
      packages/terminal-extension/src/index.ts
  2. 15 1
      packages/terminal/src/widget.ts

+ 3 - 2
packages/terminal-extension/src/index.ts

@@ -164,8 +164,9 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Instanc
     label: 'New Terminal',
     caption: 'Start a new terminal session',
     execute: args => {
-      let name = args ? args['name'] as string : '';
-      let term = new Terminal();
+      let name = args['name'] as string;
+      let initialCommand = args['initialCommand'] as string;
+      let term = new Terminal({ initialCommand });
       term.title.closable = true;
       term.title.icon = TERMINAL_ICON_CLASS;
       term.title.label = '...';

+ 15 - 1
packages/terminal/src/widget.ts

@@ -74,6 +74,7 @@ class Terminal extends Widget {
     // Initialize settings.
     let defaults = Terminal.defaultOptions;
     this._fontSize = options.fontSize || defaults.fontSize;
+    this._initialCommand = options.initialCommand || defaults.initialCommand;
     this.theme = options.theme || defaults.theme;
     this.id = `jp-Terminal-${Private.id++}`;
     this.title.label = 'Terminal';
@@ -100,6 +101,12 @@ class Terminal extends Widget {
       value.messageReceived.connect(this._onMessage, this);
       this.title.label = `Terminal ${value.name}`;
       this._setSessionSize();
+      if (this._initialCommand) {
+        this._session.send({
+          type: 'stdin',
+          content: [this._initialCommand + '\n']
+        });
+      }
     });
   }
 
@@ -341,6 +348,7 @@ class Terminal extends Widget {
   private _theme: Terminal.Theme = 'dark';
   private _box: ElementExt.IBoxSizing | null = null;
   private _session: TerminalSession.ISession | null = null;
+  private _initialCommand: string;
 }
 
 
@@ -368,6 +376,11 @@ namespace Terminal {
      * Whether to blink the cursor.  Can only be set at startup.
      */
     cursorBlink: boolean;
+
+    /**
+     * An optional command to run when the session starts.
+     */
+    initialCommand: string;
   }
 
   /**
@@ -377,7 +390,8 @@ namespace Terminal {
   const defaultOptions: IOptions = {
     theme: 'dark',
     fontSize: 13,
-    cursorBlink: true
+    cursorBlink: true,
+    initialCommand: ''
   };
 
   /**