Skip to content

Commit

Permalink
added a 'copy current path' hotkey and context menu item - fixes #2586
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugeny committed Jan 25, 2021
1 parent ecf5297 commit 5069070
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
4 changes: 4 additions & 0 deletions terminus-ssh/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ export class SSHSession extends BaseSession {
this.kill('TERM')
}

supportsWorkingDirectory (): boolean {
return true
}

async getWorkingDirectory (): Promise<string|null> {
return null
}
Expand Down
19 changes: 18 additions & 1 deletion terminus-terminal/src/api/baseTerminalTab.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions terminus-terminal/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ConfigProvider, Platform } from 'terminus-core'
export class TerminalConfigProvider extends ConfigProvider {
defaults = {
hotkeys: {
'copy-current-path': [],
shell: {
__nonStructural: true,
},
Expand Down
4 changes: 4 additions & 0 deletions terminus-terminal/src/hotkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
11 changes: 11 additions & 0 deletions terminus-terminal/src/services/sessions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
}

Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions terminus-terminal/src/tabContextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit 5069070

Please sign in to comment.