Skip to content

Commit

Permalink
feat: add terminal:clear command
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Dec 3, 2020
1 parent 2eadf1c commit 5be215a
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 3 deletions.
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()
})
})
59 changes: 59 additions & 0 deletions spec/terminal-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,63 @@ describe("terminal", () => {
expect(newTerminal.runCommand).toHaveBeenCalledWith("command 2")
})
})

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()
})
})
});
})
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 @@ -216,6 +216,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 @@ -115,6 +115,7 @@ class Terminal {
"terminal:copy": () => this.copy(),
"terminal:paste": () => this.paste(),
"terminal:unfocus": () => this.unfocus(),
'terminal:clear': () => this.clear(),
})
)
}
Expand Down Expand Up @@ -347,6 +348,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

0 comments on commit 5be215a

Please sign in to comment.