From af2e22b90f4239c233e13750b4f3a7da9990548b Mon Sep 17 00:00:00 2001 From: lewohy Date: Tue, 15 Jun 2021 13:47:34 +0000 Subject: [PATCH 1/8] feature(gritty) add config file --- bin/gritty.js | 68 +++++++++++++++++++++++++++++++++++++++++++++++---- config.json | 5 ++++ 2 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 config.json diff --git a/bin/gritty.js b/bin/gritty.js index e923242..d2d2349 100755 --- a/bin/gritty.js +++ b/bin/gritty.js @@ -2,6 +2,16 @@ 'use strict'; +/** + * @typedef {{ + * "auto-restart": true, + * "port": 8081, + * "command": "bash" + * }} Config + */ + +const fs = require('fs'); + const args = require('yargs-parser')(process.argv.slice(2), { boolean: [ 'version', @@ -14,6 +24,7 @@ const args = require('yargs-parser')(process.argv.slice(2), { ], string: [ 'command', + 'config-path' ], alias: { help: 'h', @@ -39,11 +50,33 @@ function main(args) { if (args.path) return path(); - start({ - port: args.port, - command: args.command, - autoRestart: args.autoRestart, - }); + + if (args.configPath) { + /** + * @type {Config} + */ + let config = null; + + try { + config = loadConfigFile(args.configPath); + } catch (e) { + console.log(e); + console.log('exit'); + return; + } + + start({ + port: config.port, + command: config.command, + autoRestart: config.autoRestart, + }); + } else { + start({ + port: args.port, + command: args.command, + autoRestart: args.autoRestart, + }); + } } function path() { @@ -119,3 +152,28 @@ function exit(msg) { process.exit(-1); } +/** + * @param path {string} config path + * @returns {} + */ +function loadConfigFile(path) { + /** + * @type {Config} + */ + let config = null; + + try { + let fileContent = fs.readFileSync(path, { + encoding: 'utf-8', + flag: 'r' + }); + + config = JSON.parse(fileContent); + } catch (e) { + console.log(e); + throw new Error('Cannot parse config file.'); + } + + return config; +} + diff --git a/config.json b/config.json new file mode 100644 index 0000000..6b7d480 --- /dev/null +++ b/config.json @@ -0,0 +1,5 @@ +{ + "auto-restart": true, + "port": 1337, + "command": "bash" +} From 56f91cdafd0f76343a32e5db1768b5af04b8b28e Mon Sep 17 00:00:00 2001 From: lewohy Date: Tue, 15 Jun 2021 13:55:32 +0000 Subject: [PATCH 2/8] chore(gritty) fix typo --- bin/gritty.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/gritty.js b/bin/gritty.js index d2d2349..1b5d46e 100755 --- a/bin/gritty.js +++ b/bin/gritty.js @@ -4,9 +4,9 @@ /** * @typedef {{ - * "auto-restart": true, - * "port": 8081, - * "command": "bash" + * "auto-restart": string, + * "port": number, + * "command": string * }} Config */ From 16afce9e3590f88e6ab4a5ed294981fb6aaf4f8a Mon Sep 17 00:00:00 2001 From: lewohy Date: Thu, 17 Jun 2021 14:37:13 +0000 Subject: [PATCH 3/8] feature(gritty) add external fonts) --- bin/gritty.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++-- client/gritty.js | 7 ++++- config.json | 10 +++++++- index.html | 1 + server/gritty.js | 4 +++ 5 files changed, 84 insertions(+), 4 deletions(-) diff --git a/bin/gritty.js b/bin/gritty.js index 1b5d46e..65071b4 100755 --- a/bin/gritty.js +++ b/bin/gritty.js @@ -3,10 +3,18 @@ 'use strict'; /** + * @typedef {{ + * "name": string, + * "format": string, +* "path": string + * }} FontData + * * @typedef {{ * "auto-restart": string, * "port": number, - * "command": string + * "command": string, + * "font-family": string, + * "external-fonts": FontData[] * }} Config */ @@ -64,11 +72,18 @@ function main(args) { console.log('exit'); return; } + + if (config['external-fonts'].length > 0) { + generateExternalFontStylesheet(config['external-fonts']); + } + + console.log(config); start({ port: config.port, command: config.command, - autoRestart: config.autoRestart, + autoRestart: config['auto-restart'], + fontFamily: config['font-family'] }); } else { start({ @@ -91,6 +106,7 @@ function start(options) { port, command, autoRestart, + fontFamily } = options; check(port); @@ -117,6 +133,7 @@ function start(options) { gritty.listen(socket, { command, autoRestart, + fontFamily }); server.listen(port, ip) @@ -177,3 +194,48 @@ function loadConfigFile(path) { return config; } +/** + * + * @param {FontData[]} fontDataList + */ +function generateExternalFontStylesheet(fontDataList) { + const path = require('path'); + const cssDir = path.resolve('./css'); + const fontsDir = path.resolve('./fonts'); + const fileName = 'external-font.css'; + + let getFontFaceStatement = (name, path, format) => { + return ( + `@font-face {\n` + + ` font-family: '${name}';\n` + + ` src: url('${path}') format('${format}');\n` + + `}\n\n`); + }; + + if (fs.existsSync(cssDir) && fs.lstatSync(cssDir).isDirectory()) { + if (fs.existsSync(fontsDir)) { + if (!fs.lstatSync(fontsDir).isDirectory()) { + throw new Error('Cannot copy font file.'); + } + } else { + fs.mkdirSync(fontsDir); + } + + let content = ''; + + for (let data of fontDataList) { + let resolvedPath = path.join(data.path); + if (fs.existsSync(resolvedPath)) { + fs.copyFileSync(resolvedPath, path.join(fontsDir, path.basename(resolvedPath)), ) + content += getFontFaceStatement(data.name, `../fonts/${path.basename(resolvedPath)}`, data.format); + fs.writeFileSync(path.resolve('./', cssDir, fileName), content); + } else { + console.warn(`Warning: Cannot found font file: ${resolvedPath}`); + } + } + } else { + throw new Error('Cannot make "external-font.css".'); + } + +} + diff --git a/client/gritty.js b/client/gritty.js index 7530baf..0382bd1 100644 --- a/client/gritty.js +++ b/client/gritty.js @@ -66,12 +66,13 @@ function createTerminal(terminalContainer, {env, cwd, command, autoRestart, sock scrollback: 1000, tabStopWidth: 4, fontFamily, + rendererType: 'dom' }); terminal.open(terminalContainer); terminal.focus(); - terminal.loadAddon(webglAddon); + //terminal.loadAddon(webglAddon); terminal.loadAddon(fitAddon); fitAddon.fit(); @@ -85,6 +86,9 @@ function createTerminal(terminalContainer, {env, cwd, command, autoRestart, sock socket.on('accept', onConnect(socket, fitAddon, {env, cwd, cols, rows, command, autoRestart})); socket.on('disconnect', onDisconnect(terminal)); socket.on('data', onData(terminal)); + socket.on('set-font', data => { + terminal.setOption('fontFamily', data.fontFamily); + }); return { socket, @@ -95,6 +99,7 @@ function createTerminal(terminalContainer, {env, cwd, command, autoRestart, sock function _onConnect(socket, fitAddon, {env, cwd, cols, rows, command, autoRestart}) { socket.emit('terminal', {env, cwd, cols, rows, command, autoRestart}); socket.emit('resize', {cols, rows}); + socket.emit('test', 'testc'); fitAddon.fit(); } diff --git a/config.json b/config.json index 6b7d480..ff249e5 100644 --- a/config.json +++ b/config.json @@ -1,5 +1,13 @@ { "auto-restart": true, "port": 1337, - "command": "bash" + "command": "zsh", + "font-family": "Hack", + "external-fonts": [ + { + "name": "Hack", + "format": "truetype", + "path": "/some/path/Hack/Hack-Regular.ttf" + } + ] } diff --git a/index.html b/index.html index 2b0c1bb..fadb0db 100644 --- a/index.html +++ b/index.html @@ -4,6 +4,7 @@ Gritty +
diff --git a/server/gritty.js b/server/gritty.js index dda9fba..adb1e67 100644 --- a/server/gritty.js +++ b/server/gritty.js @@ -194,6 +194,10 @@ function connection(options, socket) { socket.on('data', onData); socket.on('resize', onResize); socket.on('disconnect', onDisconnect); + + if (options.fontFamily) { + socket.emit('set-font', { fontFamily: options.fontFamily }); + } } } From 3ad11d660a77024c5c711440feb18e66cb733837 Mon Sep 17 00:00:00 2001 From: lewohy Date: Sat, 19 Jun 2021 04:06:04 +0000 Subject: [PATCH 4/8] chore(gritty) Fix to auto fit when font changed --- client/gritty.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/client/gritty.js b/client/gritty.js index 0382bd1..7a702ed 100644 --- a/client/gritty.js +++ b/client/gritty.js @@ -78,6 +78,9 @@ function createTerminal(terminalContainer, {env, cwd, command, autoRestart, sock terminal.onResize(onTermResize(socket)); terminal.onData(onTermData(socket)); + terminal.onRender((a, b) => { + terminal._addonManager._addons[0].instance.fit(); + }); window.addEventListener('resize', onWindowResize(fitAddon)); @@ -89,6 +92,8 @@ function createTerminal(terminalContainer, {env, cwd, command, autoRestart, sock socket.on('set-font', data => { terminal.setOption('fontFamily', data.fontFamily); }); + + window.t = terminal; return { socket, @@ -99,7 +104,6 @@ function createTerminal(terminalContainer, {env, cwd, command, autoRestart, sock function _onConnect(socket, fitAddon, {env, cwd, cols, rows, command, autoRestart}) { socket.emit('terminal', {env, cwd, cols, rows, command, autoRestart}); socket.emit('resize', {cols, rows}); - socket.emit('test', 'testc'); fitAddon.fit(); } From 2d20000f6950946722f4552e22aa892ecf419a72 Mon Sep 17 00:00:00 2001 From: lewohy Date: Sat, 19 Jun 2021 04:06:25 +0000 Subject: [PATCH 5/8] chore(gritty) Change cwd to $HOME --- server/gritty.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/gritty.js b/server/gritty.js index adb1e67..05d27e8 100644 --- a/server/gritty.js +++ b/server/gritty.js @@ -74,7 +74,7 @@ function createTerminal({command, env, cwd, cols, rows}) { name: 'xterm-color', cols, rows, - cwd, + cwd: process.env['HOME'], env: { ...process.env, ...env, From 2b8fd00f02c958d89d646aeddf7ea73162bb207c Mon Sep 17 00:00:00 2001 From: lewohy Date: Sat, 19 Jun 2021 05:18:33 +0000 Subject: [PATCH 6/8] feature(gritty) Add base-path option to config --- bin/gritty.js | 8 ++++++-- config.json | 3 ++- index.html | 6 +++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/bin/gritty.js b/bin/gritty.js index 65071b4..cf3bba5 100755 --- a/bin/gritty.js +++ b/bin/gritty.js @@ -13,6 +13,7 @@ * "auto-restart": string, * "port": number, * "command": string, + * "base-path": string, * "font-family": string, * "external-fonts": FontData[] * }} Config @@ -82,6 +83,7 @@ function main(args) { start({ port: config.port, command: config.command, + basePath: config['base-path'], autoRestart: config['auto-restart'], fontFamily: config['font-family'] }); @@ -105,6 +107,7 @@ function start(options) { const { port, command, + basePath, autoRestart, fontFamily } = options; @@ -125,13 +128,14 @@ function start(options) { const ip = process.env.IP || /* c9 */ '0.0.0.0'; - app.use(gritty()) - .use(express.static(DIR)); + app.use(`${basePath}`, gritty()) + .use(`${basePath}`, express.static(DIR)); const socket = io(server); gritty.listen(socket, { command, + basePath, autoRestart, fontFamily }); diff --git a/config.json b/config.json index ff249e5..7635b8c 100644 --- a/config.json +++ b/config.json @@ -1,7 +1,8 @@ { "auto-restart": true, - "port": 1337, + "port": 8082, "command": "zsh", + "base-path": "/base-path", "font-family": "Hack", "external-fonts": [ { diff --git a/index.html b/index.html index fadb0db..9ee30da 100644 --- a/index.html +++ b/index.html @@ -3,12 +3,12 @@ Gritty - - + +
- +