Skip to content

Commit

Permalink
Merge pull request #36 from parithon/use-settings
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkio authored Jan 30, 2019
2 parents 88a1ecc + 3efce25 commit a289bff
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 109 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
},
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
"typescript.tsc.autoDetect": "off",
"typescript.tsdk": "node_modules\\typescript\\lib"
"typescript.tsdk": "node_modules\\typescript\\lib",
"prettier.singleQuote": true
}
54 changes: 29 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
{
"command": "twitchhighlighter.setTwitchPassword",
"title": "Twitch Highlighter: Set Password"
"title": "Twitch Highlighter: Set Password"
},
{
"command": "twitchhighlighter.removeTwitchPassword",
Expand All @@ -70,7 +70,7 @@
],
"menus": {
"commandPalette": [
{
{
"when": "false",
"command": "twitchhighlighter.toggleChat"
},
Expand Down Expand Up @@ -115,44 +115,48 @@
"twitchhighlighter-explorer": [
{
"id": "twitchHighlighterTreeView",
"name": "Highlights",
"when": "config.twitchhighlighter.shouldConnect"
}
"name": "Highlights"
}
]
},
"configuration": {
"type": "object",
"title": "Twitch Highlighter Configuration",
"properties": {
"twitchhighlighter.shouldConnect": {
"type": "boolean",
"default": true,
"description": "Whether to connect to Twitch Chat."
"twitchhighlighter.channels": {
"type": "array",
"default": [],
"description": "The channel name(s) to connect to on Twitch. Example: ['clarkio'], Another Example: ['clarkio', 'parithon']"
},
"twitchhighlighter.server": {
"twitchhighlighter.nickname": {
"type": "string",
"default": "irc.chat.twitch.tv",
"description": "The Twitch server to connect to."
"default": "",
"description": "The username the bot should use when joining a Twitch channel."
},
"twitchhighlighter.port": {
"type": "number",
"default": 6667,
"description": "The IRC server port."
"twitchhighlighter.highlightColor": {
"type": "string",
"default": "green",
"markdownDescription": "Background color of the decoration. Use rgba() and define transparent background colors to play well with other decorations. Example: green"
},
"twitchhighlighter.nickname": {
"twitchhighlighter.highlightBorder": {
"type": "string",
"default": "clarkio",
"description": "IRC nickname. For Twitch, use your username."
"default": "2px solid white",
"description": "CSS styling property that will be applied to text enclosed by a decoration."
},
"twitchhighlighter.announceBot": {
"type": "boolean",
"default": true,
"description": "Whether or not the bot should announce its joining or leaving the chat room"
},
"twitchhighlighter.channel": {
"twitchhighlighter.joinMessage": {
"type": "string",
"default": "#clarkio",
"description": "The channel name. For Twitch, use #${your channel/user name}."
"default": "Twitch Highlighter in the house!",
"description": "The message the bot will say when joining a chat room"
},
"twitchhighlighter.command": {
"twitchhighlighter.leaveMessage": {
"type": "string",
"default": ":highlight",
"description": "The command for your viewers to use."
"default": "Twitch Highlighter has left the building!",
"description": "The message the bot will say when leaving a chat room"
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions src/credentialManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ function getNodeModule<T>(moduleName: string): T | undefined {
}

export interface TwitchCredentials {
clientId: string;
password: string;
clientId: string | null;
password: string | null;
}

export class CredentialManager {
Expand Down Expand Up @@ -54,14 +54,10 @@ export class CredentialManager {
}
return false;
}
public static getTwitchCredentials(): Promise<TwitchCredentials | null> {
return new Promise<TwitchCredentials | null>(async resolve => {
public static getTwitchCredentials(): Promise<TwitchCredentials> {
return new Promise<TwitchCredentials>(async resolve => {
const clientId = await CredentialManager.getPassword(CredentialManager.clientIdIdentifier);
const password = await CredentialManager.getPassword(CredentialManager.passwordIdentifier);
if (clientId === null || password === null) {
resolve(null);
return;
}
const twitchCredential: TwitchCredentials = {
clientId,
password
Expand Down
120 changes: 74 additions & 46 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ import {
HighlighterNode
} from './twitchhighlighterTreeView';

const highlightDecorationType = vscode.window.createTextEditorDecorationType({
backgroundColor: 'green',
border: '2px solid white'
});
let highlightDecorationType: vscode.TextEditorDecorationType;
const twitchhighlighterStatusBarIcon: string = '$(plug)'; // The octicon to use for the status bar icon (https://octicons.github.com/)
let highlighters: Array<Highlighter> = new Array<Highlighter>();
let client: LanguageClient;
Expand All @@ -30,6 +27,14 @@ let isConnected: boolean = false;
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {

setupDecoratorType();
vscode.workspace.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration('twitchhighlighter')) {
setupDecoratorType();
}
});

let serverModule = context.asAbsolutePath(path.join('out', 'server.js'));
let debugOptions = { execArgv: ['--nolazy', '--inspect=6009'] };
let serverOptions: ServerOptions = {
Expand All @@ -50,14 +55,14 @@ export function activate(context: vscode.ExtensionContext) {
};

client = new LanguageClient(
'twitchChatLanguageServer',
'Twitch Chat Highlighter', // sets the name of the 'output' window in VSCode
serverOptions,
clientOptions
);

client.onReady().then(() => {
client.onNotification('error', (params: any) => {
console.debug('Error handling in extension from client has been reached');
console.log('Error handling in extension from client has been reached');
vscode.window.showErrorMessage(params.message);
});
client.onNotification('exited', () => {
Expand All @@ -68,7 +73,7 @@ export function activate(context: vscode.ExtensionContext) {
});

client.onNotification('highlight', (params: any) => {
console.debug(params);
console.log(params);
if (!params.line) {
vscode.window.showWarningMessage(
'A line number was not provided to unhighlight'
Expand All @@ -79,7 +84,7 @@ export function activate(context: vscode.ExtensionContext) {
});

client.onNotification('unhighlight', (params: any) => {
console.debug(params);
console.log(params);
if (!params.line) {
vscode.window.showWarningMessage(
'A line number was not provided to unhighlight'
Expand Down Expand Up @@ -202,35 +207,31 @@ export function activate(context: vscode.ExtensionContext) {
// #endregion command registrations

// #region command handlers
function setTwitchClientIdHandler() {
vscode.window
async function setTwitchClientIdHandler(): Promise<boolean> {
const value = await vscode.window
.showInputBox({
prompt:
'Enter Twitch Client Id. Register your app here: https://glass.twitch.tv/console/apps/create',
prompt: 'Enter Twitch Client Id. Register your app here: https://glass.twitch.tv/console/apps/create',
ignoreFocusOut: true,
password: true
})
.then(setTwitchClientIdWithCredentialManager);
});
if (value === undefined || value === null) { return false; }
await setTwitchClientIdWithCredentialManager(value);
return true;
}

function setTwitchClientIdWithCredentialManager(value: string | undefined) {
async function setTwitchClientIdWithCredentialManager(value: string | undefined): Promise<void> {
if (value !== undefined) {
CredentialManager.setClientId(value)
.then(() => {
vscode.window.showInformationMessage(
`Twitch Client Id saved in your keychain`
);
})
.catch(reason => {
vscode.window.showInformationMessage(
`Failed to set Twitch Chat Client Id`
);
console.error(
'An error occured while saving your password to the keychain'
);
try {
await CredentialManager.setClientId(value);
vscode.window.showInformationMessage(`Twitch Client Id saved in your keychain`);
}
catch (reason) {
vscode.window.showInformationMessage(`Failed to set Twitch Chat Client Id`);
console.error('An error occured while saving your password to the keychain');
console.error(reason);
});
}
}
}

function removeTwitchClientIdHandler() {
CredentialManager.deleteTwitchClientId()
Expand All @@ -250,19 +251,21 @@ export function activate(context: vscode.ExtensionContext) {
});
}

function setTwitchPasswordHandler() {
vscode.window
async function setTwitchPasswordHandler(): Promise<boolean> {
const value = await vscode.window
.showInputBox({
prompt:
'Enter Twitch token. Generate a token here: http://www.twitchapps.com/tmi',
prompt: 'Enter Twitch token. Generate a token here: http://www.twitchapps.com/tmi',
ignoreFocusOut: true,
password: true
})
.then(setPasswordWithCredentialManager);
});
if (value === undefined || value === null) { return false; }
await setPasswordWithCredentialManager(value);
return true;
}

function setPasswordWithCredentialManager(value: string | undefined) {
async function setPasswordWithCredentialManager(value: string | undefined) {
if (value !== undefined) {
CredentialManager.setPassword(value)
await CredentialManager.setPassword(value)
.then(() => {
vscode.window.showInformationMessage(
`Twitch Chat password saved in your keychain`
Expand Down Expand Up @@ -336,36 +339,53 @@ export function activate(context: vscode.ExtensionContext) {

function startChatHandler() {
setConnectionStatus(false, true);
console.debug('Retrieving twitch credentials');
console.log('Retrieving twitch credentials');
CredentialManager.getTwitchCredentials()
.then((creds: TwitchCredentials | null) => {
if (creds === null) {
.then((creds: TwitchCredentials) => {
if (creds.clientId === null || creds.password === null) {
setConnectionStatus(false, false);
vscode.window.showInformationMessage(
'Missing Twitch credentials. Cannot start Chat client'
);
'Missing Twitch credentials. Cannot start Chat client',
"Set Credentials"
)
.then(async (action) => {
if (action) { // The user did not click the 'cancel' button.
// Set the clientId when null, if the result is false (i.e. user cancelled) then cancel the connection
if (creds.clientId === null && !await setTwitchClientIdHandler()) { return; }
// Set the password when null, if the result is false (i.e. user cancelled) then cancel the connection
if (creds.password === null && !await setTwitchPasswordHandler()) { return; }
startChatHandler();
}
});
return;
}

vscode.window.showInformationMessage(
'Twitch Highlighter: Starting Chat Listener...'
);

const configuration = vscode.workspace.getConfiguration('twitchhighlighter');

// TODO: get channels and username from extension specific settings
const chatParams = {
channels: ['clarkio'],
channels: configuration.get<string[]>('channels'),
nickname: configuration.get<string>('nickname'),
clientId: creds.clientId,
username: 'clarkio',
password: creds.password
password: creds.password,
announce: configuration.get<boolean>('announceBot') || false,
joinMessage: configuration.get<string>('joinMessage') || "",
leaveMessage: configuration.get<string>('leaveMessage') || ""
};
client.sendRequest('startchat', chatParams).then(
result => {
console.debug('We have begun connection with the Language Server');
console.log('We have begun connection with the Language Server');
vscode.window.showInformationMessage(
'Twitch Highlighter: Chat Listener Connected.'
);
setConnectionStatus(true);
},
() => {
setConnectionStatus(false, false);
vscode.window.showErrorMessage('Unable to connect to Twitch Chat');
}
);
Expand Down Expand Up @@ -611,3 +631,11 @@ function registerCommand(
let disposable = vscode.commands.registerCommand(name, handler);
context.subscriptions.push(disposable);
}

function setupDecoratorType() {
const configuration = vscode.workspace.getConfiguration("twitchhighlighter");
highlightDecorationType = vscode.window.createTextEditorDecorationType({
backgroundColor: configuration.get<string>("highlightColor") || "green",
border: configuration.get<string>("highlightBorder") || "2px solid white"
});
}
Loading

0 comments on commit a289bff

Please sign in to comment.