Skip to content
This repository has been archived by the owner on Mar 13, 2022. It is now read-only.

xterm #212

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

xterm #212

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
2 changes: 1 addition & 1 deletion lib/status-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ class StatusBar extends View {
const view = this.terminalViews[i]
if (view) {
view.ptyProcess.terminate()
view.terminal.destroy()
view.terminal.dispose()
}
}
this.detach()
Expand Down
181 changes: 86 additions & 95 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { Task, CompositeDisposable, Emitter } = require('atom')
const { $, View } = require('atom-space-pen-views')

const Pty = require.resolve('./process')
const Terminal = require('term.js')
const { Terminal } = require('xterm')
let InputDialog = null

const path = require('path')
Expand Down Expand Up @@ -32,7 +32,8 @@ class TerminusView extends View {
}

static getFocusedTerminal () {
return Terminal.Terminal.focus
// TODO:
return null
}

initialize (id, pwd, statusIcon, statusBar, shell, args = [], env = {}, autoRun = []) {
Expand Down Expand Up @@ -191,28 +192,27 @@ class TerminusView extends View {
}
})

this.terminal.end = () => this.destroy()
// TODO: port this to xterm
// this.terminal.end = () => this.destroy()

this.terminal.on('data', data => {
this.terminal.onData(data => {
this.input(data)
})

this.ptyProcess.on('terminus:title', title => {
this.process = title
})
this.terminal.on('title', title => {
this.terminal.onTitleChange(title => {
this.title = title
})

this.terminal.once('open', () => {
this.applyStyle()
this.resizeTerminalToView()
this.applyStyle()
this.resizeTerminalToView()

if (!this.ptyProcess.childProcess) { return }
const autoRunCommand = atom.config.get('terminus.core.autoRunCommand')
if (autoRunCommand) { this.input(`${autoRunCommand}${os.EOL}`) }
this.autoRun.forEach(command => this.input(`${command}${os.EOL}`))
})
if (!this.ptyProcess.childProcess) { return }
const autoRunCommand = atom.config.get('terminus.core.autoRunCommand')
if (autoRunCommand) { this.input(`${autoRunCommand}${os.EOL}`) }
this.autoRun.forEach(command => this.input(`${command}${os.EOL}`))
}

destroy () {
Expand All @@ -237,7 +237,7 @@ class TerminusView extends View {
this.ptyProcess.terminate()
}
if (this.terminal) {
this.terminal.destroy()
this.terminal.dispose()
}
}

Expand Down Expand Up @@ -341,7 +341,8 @@ class TerminusView extends View {
input (data) {
if (!this.ptyProcess.childProcess) { return }

this.terminal.stopScrolling()
// TODO:
// this.terminal.stopScrolling()
this.ptyProcess.send({ event: 'input', text: data })
}

Expand Down Expand Up @@ -380,64 +381,65 @@ class TerminusView extends View {
}

applyStyle () {
const config = atom.config.get('terminus')

this.xterm.addClass(config.style.theme)

this.subscriptions.add(atom.config.onDidChange('terminus.style.theme', event => {
this.xterm.removeClass(event.oldValue)
this.xterm.addClass(event.newValue)
}))

if (config.toggles.cursorBlink) { this.xterm.addClass('cursor-blink') }

let editorFont = atom.config.get('editor.fontFamily')
const defaultFont = "Menlo, Consolas, 'DejaVu Sans Mono', monospace"
let overrideFont = config.style.fontFamily
this.terminal.element.style.fontFamily = overrideFont || editorFont || defaultFont

this.subscriptions.add(atom.config.onDidChange('editor.fontFamily', event => {
editorFont = event.newValue
this.terminal.element.style.fontFamily = overrideFont || editorFont || defaultFont
}))
this.subscriptions.add(atom.config.onDidChange('terminus.style.fontFamily', event => {
overrideFont = event.newValue
this.terminal.element.style.fontFamily = overrideFont || editorFont || defaultFont
}))

let editorFontSize = atom.config.get('editor.fontSize')
let overrideFontSize = config.style.fontSize
this.terminal.element.style.fontSize = `${overrideFontSize || editorFontSize}px`

this.subscriptions.add(atom.config.onDidChange('editor.fontSize', event => {
editorFontSize = event.newValue
this.terminal.element.style.fontSize = `${overrideFontSize || editorFontSize}px`
this.resizeTerminalToView()
}))
this.subscriptions.add(atom.config.onDidChange('terminus.style.fontSize', event => {
overrideFontSize = event.newValue
this.terminal.element.style.fontSize = `${overrideFontSize || editorFontSize}px`
this.resizeTerminalToView()
}))

// first 8 colors i.e. 'dark' colors
this.terminal.colors[0] = config.ansiColors.normal.black.toHexString()
this.terminal.colors[1] = config.ansiColors.normal.red.toHexString()
this.terminal.colors[2] = config.ansiColors.normal.green.toHexString()
this.terminal.colors[3] = config.ansiColors.normal.yellow.toHexString()
this.terminal.colors[4] = config.ansiColors.normal.blue.toHexString()
this.terminal.colors[5] = config.ansiColors.normal.magenta.toHexString()
this.terminal.colors[6] = config.ansiColors.normal.cyan.toHexString()
this.terminal.colors[7] = config.ansiColors.normal.white.toHexString()
// 'bright' colors
this.terminal.colors[8] = config.ansiColors.zBright.brightBlack.toHexString()
this.terminal.colors[9] = config.ansiColors.zBright.brightRed.toHexString()
this.terminal.colors[10] = config.ansiColors.zBright.brightGreen.toHexString()
this.terminal.colors[11] = config.ansiColors.zBright.brightYellow.toHexString()
this.terminal.colors[12] = config.ansiColors.zBright.brightBlue.toHexString()
this.terminal.colors[13] = config.ansiColors.zBright.brightMagenta.toHexString()
this.terminal.colors[14] = config.ansiColors.zBright.brightCyan.toHexString()
this.terminal.colors[15] = config.ansiColors.zBright.brightWhite.toHexString()
// TODO: new theme api
// const config = atom.config.get('terminus')
//
// this.xterm.addClass(config.style.theme)
//
// this.subscriptions.add(atom.config.onDidChange('terminus.style.theme', event => {
// this.xterm.removeClass(event.oldValue)
// this.xterm.addClass(event.newValue)
// }))
//
// if (config.toggles.cursorBlink) { this.xterm.addClass('cursor-blink') }
//
// let editorFont = atom.config.get('editor.fontFamily')
// const defaultFont = "Menlo, Consolas, 'DejaVu Sans Mono', monospace"
// let overrideFont = config.style.fontFamily
// this.terminal.element.style.fontFamily = overrideFont || editorFont || defaultFont
//
// this.subscriptions.add(atom.config.onDidChange('editor.fontFamily', event => {
// editorFont = event.newValue
// this.terminal.element.style.fontFamily = overrideFont || editorFont || defaultFont
// }))
// this.subscriptions.add(atom.config.onDidChange('terminus.style.fontFamily', event => {
// overrideFont = event.newValue
// this.terminal.element.style.fontFamily = overrideFont || editorFont || defaultFont
// }))
//
// let editorFontSize = atom.config.get('editor.fontSize')
// let overrideFontSize = config.style.fontSize
// this.terminal.element.style.fontSize = `${overrideFontSize || editorFontSize}px`
//
// this.subscriptions.add(atom.config.onDidChange('editor.fontSize', event => {
// editorFontSize = event.newValue
// this.terminal.element.style.fontSize = `${overrideFontSize || editorFontSize}px`
// this.resizeTerminalToView()
// }))
// this.subscriptions.add(atom.config.onDidChange('terminus.style.fontSize', event => {
// overrideFontSize = event.newValue
// this.terminal.element.style.fontSize = `${overrideFontSize || editorFontSize}px`
// this.resizeTerminalToView()
// }))
//
// // first 8 colors i.e. 'dark' colors
// this.terminal.colors[0] = config.ansiColors.normal.black.toHexString()
// this.terminal.colors[1] = config.ansiColors.normal.red.toHexString()
// this.terminal.colors[2] = config.ansiColors.normal.green.toHexString()
// this.terminal.colors[3] = config.ansiColors.normal.yellow.toHexString()
// this.terminal.colors[4] = config.ansiColors.normal.blue.toHexString()
// this.terminal.colors[5] = config.ansiColors.normal.magenta.toHexString()
// this.terminal.colors[6] = config.ansiColors.normal.cyan.toHexString()
// this.terminal.colors[7] = config.ansiColors.normal.white.toHexString()
// // 'bright' colors
// this.terminal.colors[8] = config.ansiColors.zBright.brightBlack.toHexString()
// this.terminal.colors[9] = config.ansiColors.zBright.brightRed.toHexString()
// this.terminal.colors[10] = config.ansiColors.zBright.brightGreen.toHexString()
// this.terminal.colors[11] = config.ansiColors.zBright.brightYellow.toHexString()
// this.terminal.colors[12] = config.ansiColors.zBright.brightBlue.toHexString()
// this.terminal.colors[13] = config.ansiColors.zBright.brightMagenta.toHexString()
// this.terminal.colors[14] = config.ansiColors.zBright.brightCyan.toHexString()
// this.terminal.colors[15] = config.ansiColors.zBright.brightWhite.toHexString()
}

attachWindowEvents () {
Expand Down Expand Up @@ -505,6 +507,7 @@ class TerminusView extends View {
}

resizePanel (event) {
// TODO: use fit addon?
if (event.which !== 1) {
this.resizeStopped()
return
Expand All @@ -530,19 +533,13 @@ class TerminusView extends View {
}

copy () {
let text
if (this.terminal._selected) {
// const textarea = this.terminal.getCopyTextarea();
text = this.terminal.grabText(
this.terminal._selected.x1, this.terminal._selected.x2,
this.terminal._selected.y1, this.terminal._selected.y2)
} else {
const rawText = this.terminal.context.getSelection().toString()
const rawLines = rawText.split(/\r?\n/g)
let text = this.terminal.getSelection()
if (text) {
const rawLines = text.split(/\r?\n/g)
const lines = rawLines.map(line => line.replace(/\s/g, ' ').trimRight())
text = lines.join('\n')
atom.clipboard.write(text)
}
atom.clipboard.write(text)
}

paste () {
Expand All @@ -556,12 +553,14 @@ class TerminusView extends View {
const selection = editor.getSelectedText()
let selectionText = ''
if (selection) {
// TODO:
this.terminal.stopScrolling()
selectionText = selection
} else {
const cursor = editor.getCursorBufferPosition()
if (cursor) {
const line = editor.lineTextForBufferRow(cursor.row)
// TODO:
this.terminal.stopScrolling()
selectionText = line
editor.moveDown(1)
Expand Down Expand Up @@ -591,26 +590,17 @@ class TerminusView extends View {
focusTerminal (fromWindowEvent) {
if (!this.terminal) { return }

lastActiveElement = $(document.activeElement)
if (fromWindowEvent && !(lastActiveElement.is('div.terminal') || lastActiveElement.parents('div.terminal').length)) { return }
// TODO:
// lastActiveElement = $(document.activeElement)
// if (fromWindowEvent && !(lastActiveElement.is('div.terminal') || lastActiveElement.parents('div.terminal').length)) { return }

this.terminal.focus()
if (this.terminal._textarea) {
this.terminal._textarea.focus()
} else {
this.terminal.element.focus()
}
}

blurTerminal () {
if (!this.terminal) { return }

this.terminal.blur()
this.terminal.element.blur()

if (lastActiveElement) {
lastActiveElement.focus()
}
}

resizeTerminalToView () {
Expand All @@ -626,6 +616,7 @@ class TerminusView extends View {
}

getDimensions () {
// TODO: use fit addon?
const fakeRow = $('<div><span>&nbsp;</span></div>')

let cols, rows
Expand Down
Loading