Explorar o código

attempt to detect CWD on classic windows shells

Eugene Pankov %!s(int64=7) %!d(string=hai) anos
pai
achega
21d533c7cf
Modificáronse 1 ficheiros con 18 adicións e 0 borrados
  1. 18 0
      terminus-terminal/src/services/sessions.service.ts

+ 18 - 0
terminus-terminal/src/services/sessions.service.ts

@@ -25,6 +25,8 @@ export interface IChildProcess {
     command: string
 }
 
+const windowsDirectoryRegex = /([a-zA-Z]:[^\:\[\]\?\"\<\>\|]+)/mi // tslint:disable-line
+
 export abstract class BaseSession {
     open: boolean
     name: string
@@ -75,6 +77,7 @@ export abstract class BaseSession {
 export class Session extends BaseSession {
     private pty: any
     private pauseAfterExit = false
+    private guessedCWD: string
 
     constructor (private config: ConfigService) {
         super()
@@ -110,6 +113,8 @@ export class Session extends BaseSession {
             experimentalUseConpty: this.config.store.terminal.useConPTY,
         })
 
+        this.guessedCWD = options.cwd || process.env.HOME
+
         this.truePID = (this.pty as any).pid
 
         setTimeout(async () => {
@@ -125,6 +130,9 @@ export class Session extends BaseSession {
 
         this.pty.on('data-buffered', data => {
             this.emitOutput(data)
+            if (process.platform === 'win32') {
+                this.guessWindowsCWD(data)
+            }
         })
 
         this.pty.on('exit', () => {
@@ -243,8 +251,18 @@ export class Session extends BaseSession {
         if (process.platform === 'linux') {
             return fs.readlink(`/proc/${this.truePID}/cwd`)
         }
+        if (process.platform === 'win32') {
+            return this.guessedCWD
+        }
         return null
     }
+
+    private guessWindowsCWD (data: string) {
+        let match = windowsDirectoryRegex.exec(data)
+        if (match) {
+            this.guessedCWD = match[0]
+        }
+    }
 }
 
 @Injectable({ providedIn: 'root' })