Skip to content

Commit

Permalink
feat: add x-terminal:focus (#175)
Browse files Browse the repository at this point in the history
Co-authored-by: jeff-hykin <jeff.hykin@gmail.com>
  • Loading branch information
UziTech and jeff-hykin committed Oct 2, 2020
1 parent 0c10ba8 commit 3c4ef6a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 11 deletions.
12 changes: 12 additions & 0 deletions menus/x-terminal.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"label": "Open New Terminal",
"command": "x-terminal:open"
},
{
"label": "Focus On Terminal",
"command": "x-terminal:focus"
},
{
"label": "Close",
"command": "x-terminal:close"
Expand Down Expand Up @@ -75,6 +79,10 @@
"label": "Open New Terminal",
"command": "x-terminal:open"
},
{
"label": "Focus On Terminal",
"command": "x-terminal:focus"
},
{
"label": "Open New Terminal (Split Up)",
"command": "x-terminal:open-split-up"
Expand Down Expand Up @@ -152,6 +160,10 @@
"label": "Open New Terminal",
"command": "x-terminal:open"
},
{
"label": "Focus On Terminal",
"command": "x-terminal:focus"
},
{
"label": "Open New Terminal (Split Up)",
"command": "x-terminal:open-split-up"
Expand Down
35 changes: 34 additions & 1 deletion spec/x-terminal-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import * as xTerminal from '../src/x-terminal'
const xTerminalInstance = xTerminal.getInstance()

describe('x-terminal', () => {
beforeEach(async () => {
await xTerminalInstance.activate()
})

afterEach(async () => {
await xTerminalInstance.deactivate()
})

describe('getSelectedText()', () => {
it('returns selection', () => {
spyOn(atom.workspace, 'getActiveTextEditor').and.returnValue({
Expand Down Expand Up @@ -49,7 +57,6 @@ describe('x-terminal', () => {
describe('unfocus()', () => {
it('focuses atom-workspace', async () => {
jasmine.attachToDOM(atom.views.getView(atom.workspace))
await xTerminalInstance.activate()
const model = await xTerminalInstance.openInCenterOrDock(atom.workspace)
await model.initializedPromise
await model.element.createTerminal()
Expand All @@ -60,6 +67,32 @@ describe('x-terminal', () => {
})
})

describe('focus()', () => {
it('opens new terminal', async () => {
const workspace = atom.views.getView(atom.workspace)
jasmine.attachToDOM(workspace)
workspace.focus()
spyOn(xTerminalInstance, 'open')

expect(xTerminalInstance.open).not.toHaveBeenCalled()
xTerminalInstance.focus()
expect(xTerminalInstance.open).toHaveBeenCalledTimes(1)
})

it('focuses terminal', async () => {
const workspace = atom.views.getView(atom.workspace)
jasmine.attachToDOM(workspace)
const model = await xTerminalInstance.openInCenterOrDock(atom.workspace)
await model.initializedPromise
await model.element.createTerminal()
workspace.focus()

expect(model.element).not.toHaveFocus()
xTerminalInstance.focus()
expect(model.element).toHaveFocus()
})
})

describe('runCommands()', () => {
let activeTerminal, newTerminal, commands
beforeEach(() => {
Expand Down
20 changes: 10 additions & 10 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,21 +270,21 @@ class XTerminalModel {
/* Public methods are defined below this line. */

/**
* Retrieve profile for this {@link XTerminalModel} instance.
*
* @function
* @return {Object} Profile for {@link XTerminalModel} instance.
*/
* Retrieve profile for this {@link XTerminalModel} instance.
*
* @function
* @return {Object} Profile for {@link XTerminalModel} instance.
*/
getProfile () {
return this.profile
}

/**
* Apply profile changes to {@link XTerminalModel} instance.
*
* @function
* @param {Object} profileChanges Profile changes to apply.
*/
* Apply profile changes to {@link XTerminalModel} instance.
*
* @function
* @param {Object} profileChanges Profile changes to apply.
*/
applyProfileChanges (profileChanges) {
profileChanges = this.profilesSingleton.sanitizeData(profileChanges)
this.profile = this.profilesSingleton.deepClone({
Expand Down
3 changes: 3 additions & 0 deletions src/profiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ class XTerminalProfilesSingleton {
}

sanitizeData (unsanitizedData) {
if (!unsanitizedData) {
return {}
}
const sanitizedData = {}
for (const data of CONFIG_DATA) {
if (!data.profileKey) continue
Expand Down
10 changes: 10 additions & 0 deletions src/x-terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ class XTerminalSingleton {
'x-terminal:copy': () => this.copy(),
'x-terminal:paste': () => this.paste(),
'x-terminal:unfocus': () => this.unfocus(),
'x-terminal:focus': () => this.focus(),
}),
)
}
Expand Down Expand Up @@ -515,6 +516,15 @@ class XTerminalSingleton {
atom.views.getView(atom.workspace).focus()
}

focus () {
if (this.terminals_set.size === 0) {
this.openTerminal()
} else {
const activeTerminal = [...this.terminals_set].find(t => t.activeIndex === 0)
activeTerminal.focusOnTerminal()
}
}

toggleProfileMenu () {
const item = atom.workspace.getActivePaneItem()
if (isXTerminalModel(item)) {
Expand Down

0 comments on commit 3c4ef6a

Please sign in to comment.