Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add terminal:clear command #40

Merged
merged 2 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions keymaps/terminal.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@
"ctrl-shift-c": "terminal:copy",
"shift-insert": "terminal:paste",
"ctrl-shift-v": "terminal:paste",
"ctrl-shift-u": "terminal:unfocus"
"ctrl-shift-u": "terminal:unfocus",
"ctrl-l": "terminal:clear"
},
".platform-win32 atom-terminal": {
"ctrl-insert": "terminal:copy",
"ctrl-shift-c": "terminal:copy",
"shift-insert": "terminal:paste",
"ctrl-shift-v": "terminal:paste",
"ctrl-shift-u": "terminal:unfocus"
"ctrl-shift-u": "terminal:unfocus",
"ctrl-l": "terminal:clear"
},
".platform-darwin atom-terminal": {
"ctrl-shift-c": "terminal:copy",
Expand All @@ -62,6 +64,7 @@
"shift-insert": "terminal:paste",
"cmd-c": "terminal:copy",
"cmd-v": "terminal:paste",
"ctrl-shift-u": "terminal:unfocus"
"ctrl-shift-u": "terminal:unfocus",
"ctrl-l": "terminal:clear"
}
}
4 changes: 4 additions & 0 deletions menus/terminal.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"label": "Restart",
"command": "terminal:restart"
},
{
"label": "Clear",
"command": "terminal:clear"
},
{
"type": "separator"
},
Expand Down
6 changes: 6 additions & 0 deletions spec/element-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,4 +768,10 @@ describe("TerminalElement", () => {
}
expect(element.getXtermOptions()).toEqual(expected)
})

it('clear terminal', () => {
spyOn(element.terminal, 'clear')
element.clear()
expect(element.terminal.clear).toHaveBeenCalled()
})
})
58 changes: 58 additions & 0 deletions spec/terminal-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,64 @@ describe("terminal", () => {
})
})

describe("performOnTerminal", () => {
let activeTerminal
beforeEach(() => {
activeTerminal = {
element: {
initializedPromise: Promise.resolve(),
},
exit: jasmine.createSpy('activeTerminal.exit'),
restartPtyProcess: jasmine.createSpy('activeTerminal.restartPtyProcess'),
copyFromTerminal: jasmine.createSpy('activeTerminal.copy').and.returnValue('copied'),
pasteToTerminal: jasmine.createSpy('activeTerminal.paste'),
clear: jasmine.createSpy('activeTerminal.clear'),
}
spyOn(terminal, 'getActiveTerminal').and.returnValue(activeTerminal)
})

describe('close()', () => {
it('closes terminal', async () => {
await terminal.close()

expect(activeTerminal.exit).toHaveBeenCalled()
})
})

describe('restart()', () => {
it('restarts terminal', async () => {
await terminal.restart()

expect(activeTerminal.restartPtyProcess).toHaveBeenCalled()
})
})

describe('copy()', () => {
it('copys terminal', async () => {
spyOn(atom.clipboard, 'write')
await terminal.copy()

expect(atom.clipboard.write).toHaveBeenCalledWith('copied')
})
})

describe('paste()', () => {
it('pastes terminal', async () => {
spyOn(atom.clipboard, 'read').and.returnValue('copied')
await terminal.paste()

expect(activeTerminal.pasteToTerminal).toHaveBeenCalledWith('copied')
})
})

describe('clear()', () => {
it('clears terminal', async () => {
await terminal.clear()

expect(activeTerminal.clear).toHaveBeenCalled()
})
})
});
describe('open()', () => {
let uri
beforeEach(() => {
Expand Down
6 changes: 6 additions & 0 deletions src/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@ export class AtomTerminal extends HTMLElement {
this.terminal.focus()
}
}

clear () {
if (this.terminal) {
return this.terminal.clear()
}
}
}

// @ts-ignore // TODO This should be fixed soon https://developer.mozilla.org/en-US/docs/Web/API/Document/registerElement
Expand Down
6 changes: 6 additions & 0 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ export class TerminalModel {
}
}

clear () {
if (this.element) {
return this.element.clear()
}
}

setActive() {
TerminalModel.recalculateActive(this.terminalsSet, this)
}
Expand Down
8 changes: 8 additions & 0 deletions src/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class Terminal {
"terminal:copy": () => this.copy(),
"terminal:paste": () => this.paste(),
"terminal:unfocus": () => this.unfocus(),
'terminal:clear': () => this.clear(),
})
)
}
Expand Down Expand Up @@ -469,6 +470,13 @@ class Terminal {
unfocus() {
atom.views.getView(atom.workspace).focus()
}

clear() {
const terminal = this.getActiveTerminal()
if (terminal) {
terminal.clear()
}
}
}

let terminalInstance: Terminal | null = null
Expand Down