Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit 8d220fc

Browse files
committed
refactor(): theming system refactored
1 parent c114086 commit 8d220fc

File tree

5 files changed

+102
-28
lines changed

5 files changed

+102
-28
lines changed

src/app/services/config.service.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ import { Injectable, Provider, Inject } from '@angular/core';
22
import { XtermService, Terminal } from './xterm.service';
33
import * as os from 'os';
44
import * as fs from 'fs';
5+
import { CssBuilder } from '../../utils';
56

67
@Injectable()
78
export class ConfigService {
89
homeDir: string;
910
configPath: string;
1011
config: any;
1112
watcher: any;
13+
css: CssBuilder;
1214

13-
constructor(@Inject(XtermService) private hterm: XtermService) {
15+
constructor(@Inject(XtermService) private xterm: XtermService) {
16+
this.css = new CssBuilder();
1417
this.homeDir = os.homedir();
1518
this.configPath = `${this.homeDir}/.bterm.json`;
1619

@@ -29,28 +32,21 @@ export class ConfigService {
2932
let bottomBar: HTMLElement = doc.querySelector('.window-bottom-container') as HTMLElement;
3033
let sidebar: HTMLElement = doc.querySelector('.sidebar') as HTMLElement;
3134

32-
this.hterm.terminals.forEach((term: Terminal) => {
33-
/*
34-
term.term.prefs_.set('font-family', this.config.settings.font);
35-
term.term.prefs_.set('font-size', this.config.settings.font_size);
36-
term.term.prefs_.set('background-color', this.config.style.background);
37-
term.term.prefs_.set('foreground-color', this.config.style.color);
38-
term.term.prefs_.set('cursor-color', this.config.style.cursor);
39-
term.term.prefs_.set('color-palette-overrides', this.config.style.colors);
40-
*/
41-
let el = term.el as HTMLElement;
42-
el.style.background = this.config.style.background;
43-
if (term.active) {
44-
el.classList.add('active');
45-
} else {
46-
el.classList.remove('active');
47-
}
35+
this.css.clear();
36+
37+
this.config.style.colors.forEach( (color: string, index: number) => {
38+
this.css.add(`.xterm-color-${index + 1}`, `color: ${color} !important;`);
4839
});
4940

41+
this.css.add('.terminal-cursor', `background: ${this.config.style.cursor} !important; color: ${this.config.style.cursor} !important;`);
42+
this.css.add('.terminal-instance .active', `font-size: ${this.config.settings.font_size}px !important;`);
43+
this.css.add('.xterm-rows', `color: ${this.config.style.color} !important; font-family: ${this.config.settings.font} !important;`);
44+
this.css.inject();
45+
5046
terminal.style.padding = this.config.settings.windowPadding;
5147

52-
doc.style.fontFamily = this.config.style.font;
53-
doc.style.fontSize = this.config.style.font_size;
48+
doc.style.fontFamily = this.config.settings.font;
49+
doc.style.fontSize = this.config.settings.font_size + 'px';
5450
terminal.style.background = this.config.style.background;
5551
topBar.style.background = this.config.style['top_bar_background'];
5652
bottomBar.style.background = this.config.style['bottom_bar_background'];
@@ -61,6 +57,21 @@ export class ConfigService {
6157
title.style.color = this.config.style.color;
6258
});
6359

60+
this.xterm.terminals.forEach((term: Terminal) => {
61+
let el = term.el as HTMLElement;
62+
el.style.background = this.config.style.background;
63+
if (term.active) {
64+
el.classList.add('active');
65+
} else {
66+
el.classList.remove('active');
67+
}
68+
69+
setTimeout(() => {
70+
term.term.fit();
71+
if (term.active) { term.term.focus(); }
72+
});
73+
});
74+
6475
setTimeout(() => {
6576
let folderIcon = bottomBar.querySelector('.icon-folder > svg > path') as SVGPathElement;
6677
let folderText = bottomBar.querySelector('.current-folder-text') as HTMLElement;

src/app/services/pty.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class PTYService {
2222
this.user = os.userInfo({ encoding: 'utf8' });
2323
switch (os.platform()) {
2424
case 'win32':
25-
this.shell = 'bash.exe';
25+
this.shell = 'cmd.exe';
2626
this.args = [];
2727
break;
2828
case 'darwin':

src/app/services/xterm.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export class XtermService {
123123
terminal.title = splitted[0].trim();
124124
terminal.dir = splitted[1].trim();
125125
} catch (e) {
126-
terminal.title = title;
126+
terminal.title = title || 'Shell';
127127
}
128128

129129
let index = this.terminals.findIndex(t => t.term === terminal.term);
@@ -138,7 +138,7 @@ export class XtermService {
138138
}
139139

140140
fitTerminal() {
141-
this.terminals.forEach( (terminal: Terminal) => { terminal.term.fit(); });
141+
this.terminals.forEach( (terminal: Terminal) => { terminal.term.fit(); });
142142
this.focusCurrent();
143143
}
144144

@@ -164,7 +164,7 @@ export class XtermService {
164164
}
165165

166166
focusCurrent(): void {
167-
setTimeout(() => this.terminals[this.currentIndex].term.focus());
167+
setTimeout(() => this.terminals[this.currentIndex].term.focus());
168168
}
169169

170170
}

src/utils.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,70 @@ export function checkNewVersion(options: string): Promise<string> {
1414
});
1515
});
1616
}
17+
18+
interface ICSSRule {
19+
selector: string;
20+
rule: string;
21+
};
22+
23+
export class CssBuilder {
24+
css: string;
25+
rules: ICSSRule[];
26+
styleEl: HTMLStyleElement;
27+
28+
constructor() {
29+
this.rules = [];
30+
}
31+
32+
has(selector: string): boolean {
33+
return this.rules.filter((rule: ICSSRule) => rule.selector === selector).length > 0;
34+
}
35+
36+
find(selector: string): number {
37+
let index: number = -1;
38+
[].forEach.call(this.rules, (rule: ICSSRule, i: number) => {
39+
if (rule.selector === selector) { index = i; }
40+
});
41+
42+
return index;
43+
}
44+
45+
clear(): CssBuilder { this.rules = []; return this; }
46+
47+
remove(selector: string): void {
48+
let pos: number = this.find(selector);
49+
if (!pos) { return; }
50+
51+
this.rules.splice(pos, 1);
52+
}
53+
54+
add(selector: string, rule: string): CssBuilder {
55+
let newRule: ICSSRule = { selector: selector, rule: rule };
56+
this.rules.push(newRule);
57+
return this;
58+
}
59+
60+
build(): void {
61+
this.css = '';
62+
this.rules.forEach((rule: ICSSRule) => {
63+
this.css += `${rule.selector} { ${rule.rule} }` + '\n';
64+
});
65+
}
66+
67+
toString() {
68+
this.build();
69+
return this.css;
70+
}
71+
72+
inject(): void {
73+
this.build();
74+
if (this.styleEl) { this.styleEl.remove(); }
75+
76+
this.styleEl = document.createElement('style') as HTMLStyleElement;
77+
this.styleEl.setAttribute('type', 'text/css');
78+
this.styleEl.innerHTML = this.css;
79+
80+
console.log(this.styleEl);
81+
setTimeout(() => document.querySelector('head').appendChild(this.styleEl));
82+
}
83+
}

yarn.lock

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,10 +2076,6 @@ hosted-git-info@^2.1.4, hosted-git-info@^2.4.2:
20762076
version "2.4.2"
20772077
resolved "http://morose.bleenco.io/hosted-git-info/-/hosted-git-info-2.4.2.tgz#0076b9f46a270506ddbaaea56496897460612a67"
20782078

2079-
2080-
version "0.0.1"
2081-
resolved "http://morose.bleenco.io/hterm/-/hterm-0.0.1.tgz#50519959a3696e5fd7784d2842324a45cefdc670"
2082-
20832079
html-comment-regex@^1.1.0:
20842080
version "1.1.1"
20852081
resolved "http://morose.bleenco.io/html-comment-regex/-/html-comment-regex-1.1.1.tgz#668b93776eaae55ebde8f3ad464b307a4963625e"
@@ -3982,7 +3978,7 @@ rx@^4.1.0:
39823978
version "4.1.0"
39833979
resolved "http://morose.bleenco.io/rx/-/rx-4.1.0.tgz#a5f13ff79ef3b740fe30aa803fb09f98805d4782"
39843980

3985-
rxjs@5.3.1, rxjs@^5.1.1:
3981+
rxjs@^5.1.1, rxjs@^5.3.1:
39863982
version "5.3.1"
39873983
resolved "http://morose.bleenco.io/rxjs/-/rxjs-5.3.1.tgz#9ecc9e722247e4f4490d30a878577a3740fd0cb7"
39883984
dependencies:

0 commit comments

Comments
 (0)