Skip to content

Commit

Permalink
Change channels setting to a string (#76)
Browse files Browse the repository at this point in the history
* Change `channels` setting to a string

* Trim the channels for whitespaces
  • Loading branch information
parithon authored and clarkio committed Mar 1, 2019
1 parent 2f23462 commit b91e419
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 27 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@
"title": "Twitch Highlighter",
"properties": {
"twitchHighlighter.channels": {
"type": "array",
"default": [],
"description": "The channel name(s) to connect to on Twitch. Example: ['clarkio'], Another Example: ['clarkio', 'parithon']"
"type": "string",
"default": "",
"description": "A comma seperated list of channel name(s) to connect to on Twitch. Example: 'clarkio', Another Example: 'clarkio, parithon'"
},
"twitchHighlighter.nickname": {
"type": "string",
Expand Down
97 changes: 75 additions & 22 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
HighlighterNode
} from './twitchHighlighterTreeView';
import { TwitchChatClient } from './twitchChatClient';
import { isArray } from 'util';
import { extSuffix, Settings, Commands } from './constants';

let highlightDecorationType: vscode.TextEditorDecorationType;
Expand All @@ -25,6 +26,8 @@ let twitchHighlighterStatusBar: vscode.StatusBarItem;
export function activate(context: vscode.ExtensionContext) {
setupDecoratorType();

updateChannelsSetting();

twitchChatClient = new TwitchChatClient(
context.asAbsolutePath(path.join('out', 'twitchLanguageServer.js')),
context.subscriptions
Expand Down Expand Up @@ -61,12 +64,24 @@ export function activate(context: vscode.ExtensionContext) {
registerCommand(context, Commands.highlight, highlightHandler);
registerCommand(context, Commands.gotoHighlight, gotoHighlightHandler);
registerCommand(context, Commands.removeHighlight, removeHighlightHandler);
registerCommand(context, Commands.unhighlightSpecific, unhighlightSpecificHandler);
registerCommand(
context,
Commands.unhighlightSpecific,
unhighlightSpecificHandler
);
registerCommand(context, Commands.unhighlightAll, unhighlightAllHandler);
registerCommand(context, Commands.refreshTreeView, refreshTreeViewHandler);
registerCommand(context, Commands.removeTwitchClientId, removeTwitchClientIdHandler);
registerCommand(
context,
Commands.removeTwitchClientId,
removeTwitchClientIdHandler
);
registerCommand(context, Commands.setTwitchPassword, setTwitchTokenHandler);
registerCommand(context, Commands.removeTwitchPassword, removeTwitchPasswordHandler);
registerCommand(
context,
Commands.removeTwitchPassword,
removeTwitchPasswordHandler
);
registerCommand(context, Commands.startChat, startChatHandler);
registerCommand(context, Commands.stopChat, stopChatHandler);
registerCommand(context, Commands.toggleChat, toggleChatHandler);
Expand Down Expand Up @@ -168,7 +183,9 @@ export function activate(context: vscode.ExtensionContext) {
function highlightHandler() {
vscode.window
.showInputBox({ prompt: 'Enter a line number' })
.then(lineString => highlight('self', +(lineString || 0), +(lineString || 0)));
.then(lineString =>
highlight('self', +(lineString || 0), +(lineString || 0))
);
}

function unhighlightAllHandler() {
Expand All @@ -188,10 +205,7 @@ export function activate(context: vscode.ExtensionContext) {

let pickerOptions: Array<string> = new Array<string>();
highlighters.forEach(highlighter => {
pickerOptions = [
...pickerOptions,
...highlighter.getPickerDetails()
];
pickerOptions = [...pickerOptions, ...highlighter.getPickerDetails()];
});

vscode.window.showQuickPick(pickerOptions).then(pickedOption => {
Expand All @@ -211,15 +225,25 @@ export function activate(context: vscode.ExtensionContext) {

async function stopChatHandler() {
const config = vscode.workspace.getConfiguration(extSuffix);
let unhighlightOnDisconnect = config.get<boolean>(Settings.unhighlightOnDisconnect);
let unhighlightOnDisconnect = config.get<boolean>(
Settings.unhighlightOnDisconnect
);

if (highlighters.length > 0 && highlighters.some(h => h.highlights.length > 0) && !unhighlightOnDisconnect) {
const result = await vscode.window.showInformationMessage('Do you want to keep or remove the existing highlights when disconnecting from chat?', 'Always Remove', 'Remove', 'Keep');
if (
highlighters.length > 0 &&
highlighters.some(h => h.highlights.length > 0) &&
!unhighlightOnDisconnect
) {
const result = await vscode.window.showInformationMessage(
'Do you want to keep or remove the existing highlights when disconnecting from chat?',
'Always Remove',
'Remove',
'Keep'
);
if (result && result === 'Remove') {
unhighlightOnDisconnect = true;
}
if (result && result === 'Always Remove')
{
if (result && result === 'Always Remove') {
unhighlightOnDisconnect = true;
config.update(Settings.unhighlightOnDisconnect, true, true);
}
Expand Down Expand Up @@ -295,7 +319,13 @@ export function deactivate(): Thenable<void> {
return twitchChatClient.dispose();
}

function highlight(twitchUser: string, startLine: number, endLine: number, fileName?: string, comment?: string) {
function highlight(
twitchUser: string,
startLine: number,
endLine: number,
fileName?: string,
comment?: string
) {
console.log(`highlight called.`);
if (!startLine) {
console.warn('A line number was not provided to highlight');
Expand Down Expand Up @@ -338,10 +368,19 @@ function highlight(twitchUser: string, startLine: number, endLine: number, fileN

const decoration = {
range,
hoverMessage: `From @${twitchUser === 'self' ? 'You' : twitchUser}${comment !== undefined ? `: ${comment}` : ''}`
hoverMessage: `From @${twitchUser === 'self' ? 'You' : twitchUser}${
comment !== undefined ? `: ${comment}` : ''
}`
};

addHighlight(existingHighlighter, decoration, editor, startLine, endLine, twitchUser);
addHighlight(
existingHighlighter,
decoration,
editor,
startLine,
endLine,
twitchUser
);
}

function unhighlight(lineNumber: number, fileName?: string) {
Expand Down Expand Up @@ -464,7 +503,11 @@ function findHighlighter(fileName: string): Highlighter | undefined {
});
}

function getHighlightRange(startLine: number, endLine: number, doc: vscode.TextDocument) {
function getHighlightRange(
startLine: number,
endLine: number,
doc: vscode.TextDocument
) {
// prefix string with plus (+) to make string a number
// well at least that's what codephobia says :P
// const zeroIndexedLineNumber = +lineNumber - 1;
Expand Down Expand Up @@ -497,14 +540,24 @@ function registerCommand(
context.subscriptions.push(disposable);
}

/**
* Used to upgrade the channels setting from an array of strings ['clarkio','parithon']
* to a string 'clarkio, parithon'.
*/
function updateChannelsSetting() {
const configuration = vscode.workspace.getConfiguration('twitchHighlighter');
const channels = configuration.get<string>('channels');
if (isArray(channels)) {
// Update the global settings
configuration.update('channels', channels.join(', '), true);
}
}

function setupDecoratorType() {
const configuration = vscode.workspace.getConfiguration(
'twitchHighlighter'
);
const configuration = vscode.workspace.getConfiguration('twitchHighlighter');
highlightDecorationType = vscode.window.createTextEditorDecorationType({
backgroundColor: configuration.get<string>('highlightColor') || 'green',
border:
configuration.get<string>('highlightBorder') || '2px solid white',
border: configuration.get<string>('highlightBorder') || '2px solid white',
color: configuration.get<string>('highlightFontColor') || 'white'
});
}
4 changes: 2 additions & 2 deletions src/twitchLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ connection.onShutdown(() => {
});

function getTwitchChatOptions(params: {
channels: string[];
channels: string;
username: string;
clientId: string;
password: string;
}): tmi.ClientOptions {
return {
channels: params.channels,
channels: params.channels.split(',').map(s => s.trim()),
connection: {
secure: true,
reconnect: true,
Expand Down

0 comments on commit b91e419

Please sign in to comment.