added a 'copy current path' hotkey and context menu item - fixes #2586

This commit is contained in:
Eugene Pankov
2021-01-25 17:30:44 +01:00
parent ecf5297bc3
commit 5069070040
6 changed files with 45 additions and 1 deletions

View File

@@ -363,6 +363,10 @@ export class SSHSession extends BaseSession {
this.kill('TERM')
}
supportsWorkingDirectory (): boolean {
return true
}
async getWorkingDirectory (): Promise<string|null> {
return null
}

View File

@@ -18,6 +18,7 @@ import { TerminalDecorator } from './decorator'
/** @hidden */
export interface ToastrServiceProxy {
info: (_: string) => void
error: (_: string) => void
}
/**
* A class to base your custom terminal tabs on
@@ -140,7 +141,7 @@ export class BaseTerminalTabComponent extends BaseTabComponent implements OnInit
this.logger = this.log.create('baseTerminalTab')
this.setTitle('Terminal')
this.hotkeysSubscription = this.hotkeys.matchedHotkey.subscribe(hotkey => {
this.hotkeysSubscription = this.hotkeys.matchedHotkey.subscribe(async hotkey => {
if (!this.hasFocus) {
return
}
@@ -207,6 +208,9 @@ export class BaseTerminalTabComponent extends BaseTabComponent implements OnInit
case 'pane-focus-all':
this.focusAllPanes()
break
case 'copy-current-path':
this.copyCurrentPath()
break
}
})
this.bellPlayer = document.createElement('audio')
@@ -438,6 +442,19 @@ export class BaseTerminalTabComponent extends BaseTabComponent implements OnInit
}
}
async copyCurrentPath (): Promise<void> {
let cwd: string|null = null
if (this.session?.supportsWorkingDirectory()) {
cwd = await this.session.getWorkingDirectory()
}
if (cwd) {
this.electron.clipboard.writeText(cwd)
this.toastr.info('Copied')
} else {
this.toastr.error('Shell does not support current path detection')
}
}
/** @hidden */
ngOnDestroy (): void {
this.frontend?.detach(this.content.nativeElement)

View File

@@ -4,6 +4,7 @@ import { ConfigProvider, Platform } from 'terminus-core'
export class TerminalConfigProvider extends ConfigProvider {
defaults = {
hotkeys: {
'copy-current-path': [],
shell: {
__nonStructural: true,
},

View File

@@ -62,6 +62,10 @@ export class TerminalHotkeyProvider extends HotkeyProvider {
id: 'ctrl-c',
name: 'Intelligent Ctrl-C (copy/abort)',
},
{
id: 'copy-current-path',
name: 'Copy current path',
},
{
id: 'search',
name: 'Search',

View File

@@ -87,6 +87,7 @@ export abstract class BaseSession {
abstract kill (signal?: string): void
abstract async getChildProcesses (): Promise<ChildProcess[]>
abstract async gracefullyKillProcess (): Promise<void>
abstract supportsWorkingDirectory (): boolean
abstract async getWorkingDirectory (): Promise<string|null>
}
@@ -259,6 +260,16 @@ export class Session extends BaseSession {
}
}
supportsWorkingDirectory (): boolean {
if (this.reportedCWD || this.guessedCWD) {
return true
}
if (!this.truePID) {
return false
}
return process.platform !== 'win32'
}
async getWorkingDirectory (): Promise<string|null> {
if (this.reportedCWD) {
return this.reportedCWD

View File

@@ -123,6 +123,13 @@ export class NewTabContextMenu extends TabContextMenuItemProvider {
})
}
if (tab instanceof TerminalTabComponent && tab.session?.supportsWorkingDirectory()) {
items.push({
label: 'Copy current path',
click: () => this.zone.run(() => tab.copyCurrentPath()),
})
}
return items
}
}