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: provide terminal service #236

Merged
merged 3 commits into from
Jan 2, 2021
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
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,62 @@ There are also activity notifications for terminal tabs not in focus.

## Services

For plugin writers, the `x-terminal` package supports two services, `atom-xterm` and `platformioIDETerminal`, which
For plugin writers, the `x-terminal` package supports three services, `terminal`, `atom-xterm`, and `platformioIDETerminal`, which
can be used to easily open terminals. These methods are provided using Atom's [services](http://flight-manual.atom.io/behind-atom/sections/interacting-with-other-packages-via-services/)
API.

To use a service, add a consumer method to consume the service, or
rather a JavaScript object that provides methods to open terminals and run commands.

### 'terminal' service v1.0.0

The `terminal` service provides an object with `updateProcessEnv`, `run`, `getTerminalViews`, and `open` methods.

As an example on how to use the provided `run()` method, your
`package.json` should have the following.

```json
{
"consumedServices": {
"terminal": {
"versions": {
"^1.0.0": "consumeTerminalService"
}
}
}
}
```

Your package's main module should then define a `consumeTerminalService`
method, for example.

```js
import { Disposable } from 'atom'

export default {
terminalService: null,

consumeTerminalService (terminalService) {
this.terminalService = terminalService
return new Disposable(() => {
this.terminalService = null
})
},

// . . .
}
```

Once the service is consumed, use the `run()` method that is provided
by the service, for example.

```js
// Launch `somecommand --foo --bar --baz` in a terminal.
this.terminalService.run([
'somecommand --foo --bar --baz'
])
```

### 'atom-xterm' service v2.0.0

The `atom-xterm` service provides the
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
"versions": {
"1.1.0": "providePlatformIOIDEService"
}
},
"terminal": {
"description": "Run commands and open terminals.",
"versions": {
"1.0.0": "provideTerminalService"
}
}
},
"dependencies": {
Expand Down
35 changes: 35 additions & 0 deletions spec/x-terminal-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,38 @@ describe('x-terminal', () => {
})
})
})

describe('x-terminal services', () => {
beforeEach(async () => {
atom.packages.triggerDeferredActivationHooks()
atom.packages.triggerActivationHook('core:loaded-shell-environment')
await atom.packages.activatePackage('x-terminal')
})

it('terminal.run', async () => {
spyOn(xTerminalInstance, 'runCommands')
const service = await new Promise(resolve => {
atom.packages.serviceHub.consume('terminal', '^1.0.0', resolve)
})
service.run(['test'])
expect(xTerminalInstance.runCommands).toHaveBeenCalledWith(['test'])
})

it('platformioIDETerminal.run', async () => {
spyOn(xTerminalInstance, 'runCommands')
const service = await new Promise(resolve => {
atom.packages.serviceHub.consume('platformioIDETerminal', '^1.1.0', resolve)
})
service.run(['test'])
expect(xTerminalInstance.runCommands).toHaveBeenCalledWith(['test'])
})

it('atom-xterm.openTerminal', async () => {
spyOn(xTerminalInstance, 'openTerminal')
const service = await new Promise(resolve => {
atom.packages.serviceHub.consume('atom-xterm', '^2.0.0', resolve)
})
service.openTerminal({})
expect(xTerminalInstance.openTerminal).toHaveBeenCalledWith({})
})
})
15 changes: 15 additions & 0 deletions src/x-terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,17 @@ class XTerminalSingleton {
}
}

/**
* Function providing service functions offered by 'terminal' service.
*
* @function
* @returns {Object} Object holding service functions.
*/
provideTerminalService () {
// for now it is the same as platformioIDETerminal service
return this.providePlatformIOIDEService()
}

close () {
this.performOnActiveTerminal(t => t.exit())
}
Expand Down Expand Up @@ -724,3 +735,7 @@ export function provideAtomXtermService () {
export function providePlatformIOIDEService () {
return XTerminalSingleton.instance.providePlatformIOIDEService()
}

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