Skip to content

Commit

Permalink
feat: add platform io terminal service
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Feb 16, 2020
1 parent dfc90e4 commit fbf2b6a
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 114 deletions.
2 changes: 1 addition & 1 deletion dist/lib/x-terminal.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/lib/x-terminal.js.map

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
"versions": {
"2.0.0": "provideAtomXtermService"
}
},
"platformioIDETerminal": {
"description": "Run commands and open terminals.",
"versions": {
"1.1.0": "providePlatformIOIDEService"
}
}
},
"dependencies": {
Expand Down
34 changes: 0 additions & 34 deletions spec/element-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1558,40 +1558,6 @@ describe('XTerminalElement', () => {
expect(this.element.terminalDiv.classList.contains('x-terminal-term-container-has-link')).toBe(false)
})

it('setHoveredLink(\'https://atom.io\')', () => {
const expected = 'https://atom.io'
this.element.setHoveredLink(expected)
expect(this.element.hoveredLink).toBe(expected)
expect(this.element.terminalDiv.classList.contains('x-terminal-term-container-has-link')).toBe(true)
})

it('clearHoveredLink()', () => {
this.element.setHoveredLink('https://atom.io')
this.element.clearHoveredLink()
expect(this.element.hoveredLink).toBeNull()
expect(this.element.terminalDiv.classList.contains('x-terminal-term-container-has-link')).toBe(false)
})

it('openHoveredLink() no hovered link set', () => {
this.element.openHoveredLink()
expect(shell.openExternal).not.toHaveBeenCalled()
})

it('openHoveredLink() hovered link set', () => {
this.element.hoveredLink = 'https://atom.io'
this.element.openHoveredLink()
expect(shell.openExternal.calls.argsFor(0)).toEqual(['https://atom.io'])
})

it('getHoveredLink() no hovered link set', () => {
expect(this.element.getHoveredLink()).toBeFalsy()
})

it('openHoveredLink() hovered link set', () => {
this.element.hoveredLink = 'https://atom.io'
expect(this.element.getHoveredLink()).toBe('https://atom.io')
})

it('on \'data\' handler no custom title on win32 platform', async () => {
Object.defineProperty(process, 'platform', {
value: 'win32',
Expand Down
12 changes: 0 additions & 12 deletions spec/model-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,18 +467,6 @@ describe('XTerminalModel', () => {
expect(this.model.element.toggleProfileMenu).toHaveBeenCalled()
})

it('openHoveredLink()', () => {
this.model.element = jasmine.createSpyObj('element', ['openHoveredLink'])
this.model.openHoveredLink()
expect(this.model.element.openHoveredLink).toHaveBeenCalled()
})

it('getHoveredLink()', () => {
this.model.element = jasmine.createSpyObj('element', ['getHoveredLink'])
this.model.getHoveredLink()
expect(this.model.element.getHoveredLink).toHaveBeenCalled()
})

it('getProfile()', () => {
const mock = jasmine.createSpy('mock')
this.model.profile = mock
Expand Down
24 changes: 1 addition & 23 deletions src/lib/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ class XTerminalElementImpl extends HTMLElement {
this.terminal = new Terminal(this.getXtermOptions())
this.fitAddon = new FitAddon()
this.terminal.loadAddon(this.fitAddon)
this.terminal.loadAddon(new WebLinksAddon())
this.terminal.loadAddon(new WebLinksAddon((e, uri) => { shell.openExternal(uri) }))
this.terminal.open(this.terminalDiv)
this.terminal.loadAddon(new WebglAddon())
this.ptyProcessCols = 80
Expand Down Expand Up @@ -686,28 +686,6 @@ class XTerminalElementImpl extends HTMLElement {
this.terminalDiv.style.visibility = 'visible'
}

setHoveredLink (link) {
this.hoveredLink = link
this.terminalDiv.classList.add('x-terminal-term-container-has-link')
}

clearHoveredLink () {
this.terminalDiv.classList.remove('x-terminal-term-container-has-link')
this.hoveredLink = null
}

openHoveredLink () {
if (this.hoveredLink) {
shell.openExternal(this.hoveredLink)
}
}

getHoveredLink () {
if (this.hoveredLink) {
return this.hoveredLink
}
}

queueNewProfileChanges (profileChanges) {
this.pendingTerminalProfileOptions = Object.assign(this.pendingTerminalProfileOptions, profileChanges)
this.applyPendingTerminalProfileOptions()
Expand Down
8 changes: 0 additions & 8 deletions src/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,6 @@ class XTerminalModel {
this.pane = pane
}

openHoveredLink () {
this.element.openHoveredLink()
}

getHoveredLink () {
return this.element.getHoveredLink()
}

toggleProfileMenu () {
this.element.toggleProfileMenu()
}
Expand Down
96 changes: 61 additions & 35 deletions src/lib/x-terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import '../styles/x-terminal.sass'

import { CompositeDisposable } from 'atom'
import os from 'os'

import { COLORS } from './config'
import { XTerminalElement } from './element'
Expand Down Expand Up @@ -201,8 +202,6 @@ class XTerminalSingleton {
'x-terminal:restart': () => this.restart(),
'x-terminal:copy': () => this.copy(),
'x-terminal:paste': () => this.paste(),
'x-terminal:open-link': () => this.openLink(),
'x-terminal:copy-link': () => this.copyLink(),
}))
}

Expand Down Expand Up @@ -276,16 +275,16 @@ class XTerminalSingleton {
}

/**
* Service function which is a wrapper around 'atom.workspace.open()'. The
* only difference with this function from 'atom.workspace.open()' is that it
* accepts a profile Object as the first argument.
*
* @async
* @function
* @param {Object} profile Profile data to use when opening terminal.
* @param {Object} options Options to pass to call to 'atom.workspace.open()'.
* @return {XTerminalModel} Instance of XTerminalModel.
*/
* Service function which is a wrapper around 'atom.workspace.open()'. The
* only difference with this function from 'atom.workspace.open()' is that it
* accepts a profile Object as the first argument.
*
* @async
* @function
* @param {Object} profile Profile data to use when opening terminal.
* @param {Object} options Options to pass to call to 'atom.workspace.open()'.
* @return {XTerminalModel} Instance of XTerminalModel.
*/
async openTerminal (profile, options = {}) {
return this.open(
XTerminalProfilesSingleton.instance.generateNewUrlFromProfileData(profile),
Expand All @@ -294,11 +293,27 @@ class XTerminalSingleton {
}

/**
* Function providing service functions offered by 'x-terminal' package.
*
* @function
* @returns {Object} Object holding service functions.
*/
* Service function which opens a terminal and runs the commands.
*
* @async
* @function
* @param {string[]} commands Commands to run in the terminal.
* @return {XTerminalModel} Instance of XTerminalModel.
*/
async runCommands (commands) {
const model = await this.open(XTerminalProfilesSingleton.instance.generateNewUri())
await model.element.initializedPromise
for (const command of commands) {
model.pasteToTerminal(command + os.EOL)
}
}

/**
* Function providing service functions offered by 'atom-xterm' service.
*
* @function
* @returns {Object} Object holding service functions.
*/
provideAtomXtermService () {
return {
openTerminal: async (...args) => {
Expand All @@ -307,6 +322,31 @@ class XTerminalSingleton {
}
}

/**
* Function providing service functions offered by 'platformioIDETerminal' service.
*
* @function
* @returns {Object} Object holding service functions.
*/
providePlatformIOIDEService () {
return {
updateProcessEnv (vars) {
for (const name in vars) {
process.env[name] = vars[name]
}
},
run: (commands) => {
return this.runCommands(commands)
},
getTerminalViews: () => {
return this.terminals_set
},
open: () => {
return this.openTerminal()
},
}
}

performOperationOnItem (operation) {
const item = atom.workspace.getActivePaneItem()
if (isXTerminalModel(item)) {
Expand All @@ -323,16 +363,6 @@ class XTerminalSingleton {
case 'paste':
item.pasteToTerminal(atom.clipboard.read())
break
case 'open-link':
item.openHoveredLink()
break
case 'copy-link': {
const link = item.getHoveredLink()
if (link) {
atom.clipboard.write(link)
}
break
}
default:
throw new Error('Unknown operation: ' + operation)
}
Expand All @@ -355,14 +385,6 @@ class XTerminalSingleton {
this.performOperationOnItem('paste')
}

openLink () {
this.performOperationOnItem('open-link')
}

copyLink () {
this.performOperationOnItem('copy-link')
}

toggleProfileMenu () {
const item = atom.workspace.getActivePaneItem()
if (isXTerminalModel(item)) {
Expand Down Expand Up @@ -453,3 +475,7 @@ export function deserializeXTerminalModel (serializedModel, atomEnvironment) {
export function provideAtomXtermService () {
return XTerminalSingleton.instance.provideAtomXtermService()
}

export function providePlatformIOIDEService () {
return XTerminalSingleton.instance.providePlatformIOIDEService()
}

0 comments on commit fbf2b6a

Please sign in to comment.