add interfaces and option handling

This commit is contained in:
Jörg Breitbart
2019-12-05 21:30:59 +01:00
parent 31ccb796ac
commit 43303ba4c1
7 changed files with 67 additions and 10 deletions
+5
View File
@@ -391,6 +391,11 @@ export class Terminal extends Disposable implements ITerminal, IDisposable, IInp
this._windowsMode = undefined;
}
break;
case 'unicodeVersion':
if (this._unicodeService.activeVersion !== this.optionsService.options.unicodeVersion) {
this._unicodeService.activeVersion = this.optionsService.options.unicodeVersion;
}
break;
}
});
}
+2 -2
View File
@@ -205,10 +205,10 @@ export class UnicodeV11 implements IUnicodeVersionProvider {
}
}
public wcwidth(num: number): number {
public wcwidth(num: number): 0 | 1 | 2 {
if (num < 32) return 0;
if (num < 127) return 1;
if (num < 65536) return UnicodeV11.bmpTable[num];
if (num < 65536) return UnicodeV11.bmpTable[num] as 0 | 1 | 2;
if (UnicodeV11.bisearch(num, UnicodeV11.highCombining)) return 0;
if (UnicodeV11.bisearch(num, UnicodeV11.highWide)) return 2;
return 1;
+2 -2
View File
@@ -118,10 +118,10 @@ export class UnicodeV6 implements IUnicodeVersionProvider {
}
}
public wcwidth(num: number): number {
public wcwidth(num: number): 0 | 1 | 2 {
if (num < 32) return 0;
if (num < 127) return 1;
if (num < 65536) return UnicodeV6.bmpTable[num];
if (num < 65536) return UnicodeV6.bmpTable[num] as 0 | 1 | 2;
if (UnicodeV6.bisearch(num, UnicodeV6.highCombining)) return 0;
if ((num >= 0x20000 && num <= 0x2fffd) || (num >= 0x30000 && num <= 0x3fffd)) return 2;
return 1;
+2 -2
View File
@@ -289,7 +289,7 @@ export interface IUnicodeService {
/** Registered Unicode versions. */
readonly versions: string[];
/** Currently active version. */
readonly activeVersion: string;
activeVersion: string;
/** Event triggered, when activate version changed. */
readonly onChange: IEvent<string>;
@@ -302,5 +302,5 @@ export interface IUnicodeService {
export interface IUnicodeVersionProvider {
version: string;
wcwidth(ucs: number): number;
wcwidth(ucs: number): 0 | 1 | 2;
}
+3 -3
View File
@@ -9,8 +9,8 @@ import { IUnicodeVersionProvider } from 'common/services/Services';
class DummyProvider implements IUnicodeVersionProvider {
version = '123';
wcwidth(n: number): number {
return 123;
wcwidth(n: number): 0 | 1 | 2 {
return 2;
}
}
@@ -41,7 +41,7 @@ describe('unicode provider', () => {
const dummyProvider = new DummyProvider();
us.register(dummyProvider);
us.activeVersion = dummyProvider.version;
assert.equal(us.getStringCellWidth('hello'), 123 * 5);
assert.equal(us.getStringCellWidth('hello'), 2 * 5);
});
it('wcwidth V6 emoji test', () => {
const widthV6 = us.getStringCellWidth('🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣');
+22 -1
View File
@@ -3,7 +3,7 @@
* @license MIT
*/
import { Terminal as ITerminalApi, ITerminalOptions, IMarker, IDisposable, ILinkMatcherOptions, ITheme, ILocalizableStrings, ITerminalAddon, ISelectionPosition, IBuffer as IBufferApi, IBufferLine as IBufferLineApi, IBufferCell as IBufferCellApi, IParser, IFunctionIdentifier } from 'xterm';
import { Terminal as ITerminalApi, ITerminalOptions, IMarker, IDisposable, ILinkMatcherOptions, ITheme, ILocalizableStrings, ITerminalAddon, ISelectionPosition, IBuffer as IBufferApi, IBufferLine as IBufferLineApi, IBufferCell as IBufferCellApi, IParser, IFunctionIdentifier, IUnicodeHandling, IUnicodeVersionProvider } from 'xterm';
import { ITerminal } from '../Types';
import { IBufferLine } from 'common/Types';
import { IBuffer } from 'common/buffer/Types';
@@ -41,6 +41,9 @@ export class Terminal implements ITerminalApi {
}
return this._parser;
}
public get unicode(): IUnicodeHandling {
return new UnicodeApi(this._core);
}
public get textarea(): HTMLTextAreaElement | undefined { return this._core.textarea; }
public get rows(): number { return this._core.rows; }
public get cols(): number { return this._core.cols; }
@@ -240,3 +243,21 @@ class ParserApi implements IParser {
return this._core.addOscHandler(ident, callback);
}
}
class UnicodeApi implements IUnicodeHandling {
constructor(private _core: ITerminal) {}
public register(provider: IUnicodeVersionProvider): void {}
public get versions(): string[] {
return (this._core as any)._unicodeService.versions;
}
public get activeVersion(): string {
return (this._core as any)._unicodeService.activeVersion;
}
public set activeVersion(version: string) {
(this._core as any)._unicodeService.activeVersion = version;
}
}
+31
View File
@@ -426,6 +426,12 @@ declare module 'xterm' {
*/
readonly parser: IParser;
/**
* (EXPERIMENTAL) Get the Unicode handling interface
* to register and switch Unicode version.
*/
readonly unicode: IUnicodeHandling;
/**
* Natural language strings that can be localized.
*/
@@ -1130,4 +1136,29 @@ declare module 'xterm' {
*/
addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable;
}
/**
* (EXPERIMENTAL) Unicode version provider.
* Used to register custom Unicode versions with `Terminal.unicode.register`.
*/
export interface IUnicodeVersionProvider {
/** String indicating the Unicode version provided. */
version: string;
/** Unicode version dependent wcwidth implementation. */
wcwidth(ucs: number): 0 | 1 | 2;
}
/**
* (EXPERIMENTAL) Unicode handling interface.
*/
export interface IUnicodeHandling {
/**
* Register a custom Unicode version provider.
*/
register(provider: IUnicodeVersionProvider): void;
/**
* Registered Unicode versions.
*/
versions: string[];
}
}