Skip to content

Commit

Permalink
feat: add focus-next and focus-previous commands (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Mar 14, 2022
1 parent 9560a3c commit 01d00db
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
66 changes: 66 additions & 0 deletions spec/x-terminal-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,72 @@ describe('x-terminal', () => {
})
})

describe('focus-next', () => {
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(1)
expect(xTerminalInstance.open).toHaveBeenCalledTimes(1)
})

it('focuses next terminal', async () => {
const workspace = atom.views.getView(atom.workspace)
jasmine.attachToDOM(workspace)
const terminals = []
for (let i = 0; i < 3; i++) {
terminals[i] = await xTerminalInstance.openInCenterOrDock(atom.workspace)
await terminals[i].initializedPromise
await terminals[i].element.createTerminal()
}
expect(terminals[2].element).toHaveFocus()
xTerminalInstance.focusNext()
expect(terminals[0].element).toHaveFocus()
xTerminalInstance.focusNext()
expect(terminals[1].element).toHaveFocus()
xTerminalInstance.focusNext()
expect(terminals[2].element).toHaveFocus()
xTerminalInstance.focusNext()
expect(terminals[0].element).toHaveFocus()
})
})

describe('focus-previous', () => {
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(-1)
expect(xTerminalInstance.open).toHaveBeenCalledTimes(1)
})

it('focuses prev terminal', async () => {
const workspace = atom.views.getView(atom.workspace)
jasmine.attachToDOM(workspace)
const terminals = []
for (let i = 0; i < 3; i++) {
terminals[i] = await xTerminalInstance.openInCenterOrDock(atom.workspace)
await terminals[i].initializedPromise
await terminals[i].element.createTerminal()
}
expect(terminals[2].element).toHaveFocus()
xTerminalInstance.focusPrev()
expect(terminals[1].element).toHaveFocus()
xTerminalInstance.focusPrev()
expect(terminals[0].element).toHaveFocus()
xTerminalInstance.focusPrev()
expect(terminals[2].element).toHaveFocus()
xTerminalInstance.focusPrev()
expect(terminals[1].element).toHaveFocus()
})
})

describe('runCommands()', () => {
let activeTerminal, newTerminal, commands
beforeEach(() => {
Expand Down
28 changes: 28 additions & 0 deletions src/x-terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ class XTerminalSingleton {
'x-terminal:insert-selected-text': () => this.insertSelection(),
'x-terminal:run-selected-text': () => this.runSelection(),
'x-terminal:focus': () => this.focus(),
'x-terminal:focus-next': () => this.focusNext(),
'x-terminal:focus-previous': () => this.focusPrev(),
}),
atom.commands.add('atom-text-editor, .tree-view, .tab-bar', {
'x-terminal:open-context-menu': {
Expand Down Expand Up @@ -637,6 +639,32 @@ class XTerminalSingleton {
}
}

focusNext () {
if (this.terminals_set.size === 0) {
this.openTerminal()
} else {
const terminals = [...this.terminals_set]
let i = terminals.findIndex(t => t.activeIndex === 0) + 1
if (i >= terminals.length) {
i -= terminals.length
}
terminals[i].focusOnTerminal(true)
}
}

focusPrev () {
if (this.terminals_set.size === 0) {
this.openTerminal()
} else {
const terminals = [...this.terminals_set]
let i = terminals.findIndex(t => t.activeIndex === 0) - 1
if (i < 0) {
i += terminals.length
}
terminals[i].focusOnTerminal(true)
}
}

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

0 comments on commit 01d00db

Please sign in to comment.