rewrite: generateDetails, add support for line numbers and file sizes and much more (#58)
This commit is contained in:
parent
6ae9d98730
commit
389757d7e2
2 changed files with 100 additions and 21 deletions
10
package.json
10
package.json
|
@ -63,12 +63,12 @@
|
||||||
"discord.detailsEditing": {
|
"discord.detailsEditing": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"default": "Editing {filename}",
|
"default": "Editing {filename}",
|
||||||
"description": "Custom string for the details section of the rich presence\n\t- '{null}' will be replaced with an empty space.\n\t- '{filename}' will be replaced with the current file name.\n\t- '{workspace}' will be replaced with the current workspace name, if any."
|
"description": "Custom string for the details section of the rich presence\n\t- '{null}' will be replaced with an empty space.\n\t- '{filename}' will be replaced with the current file name.\n\t- '{dirname}' will get replaced with the folder name that has the current file.\n\t- '{fulldirname}' will get replaced with the full directory name without the current file name.\n\t- '{workspace}' will be replaced with the current workspace name, if any.\n\t- '{currentline}' will get replaced with the current line number.\n\t- '{totallines}' will get replaced with the total line number.\n\t- '{filesize}' will get replaced with the current file's size."
|
||||||
},
|
},
|
||||||
"discord.detailsDebugging": {
|
"discord.detailsDebugging": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"default": "Editing {filename}",
|
"default": "Editing {filename}",
|
||||||
"description": "Custom string for the details section of the rich presence when debugging\n\t- '{null}' will be replaced with an empty space.\n\t- '{filename}' will be replaced with the current file name.\n\t- '{workspace}' will be replaced with the current workspace name, if any."
|
"description": "Custom string for the details section of the rich presence when debugging\n\t- '{null}' will be replaced with an empty space.\n\t- '{filename}' will be replaced with the current file name.\n\t- '{dirname}' will get replaced with the folder name that has the current file.\n\t- '{fulldirname}' will get replaced with the full directory name without the current file name.\n\t- '{workspace}' will be replaced with the current workspace name, if any.\n\t- '{currentline}' will get replaced with the current line number.\n\t- '{totallines}' will get replaced with the total line number.\n\t- '{filesize}' will get replaced with the current file's size."
|
||||||
},
|
},
|
||||||
"discord.detailsIdle": {
|
"discord.detailsIdle": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
|
@ -78,12 +78,12 @@
|
||||||
"discord.lowerDetailsEditing": {
|
"discord.lowerDetailsEditing": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"default": "Workspace: {workspace}",
|
"default": "Workspace: {workspace}",
|
||||||
"description": "Custom string for the state section of the rich presence\n\t- '{null}' will be replaced with an empty space.\n\t- '{filename}' will be replaced with the current file name.\n\t- '{workspace}' will be replaced with the current workspace name, if any."
|
"description": "Custom string for the state section of the rich presence\n\t- '{null}' will be replaced with an empty space.\n\t- '{filename}' will be replaced with the current file name.\n\t- '{dirname}' will get replaced with the folder name that has the current file.\n\t- '{fulldirname}' will get replaced with the full directory name without the current file name.\n\t- '{workspace}' will be replaced with the current workspace name, if any.\n\t- '{currentline}' will get replaced with the current line number.\n\t- '{totallines}' will get replaced with the total line number.\n\t- '{filesize}' will get replaced with the current file's size."
|
||||||
},
|
},
|
||||||
"discord.lowerDetailsDebugging": {
|
"discord.lowerDetailsDebugging": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"default": "Debugging: {workspace}",
|
"default": "Debugging: {workspace}",
|
||||||
"description": "Custom string for the state section of the rich presence when debugging\n\t- '{null}' will be replaced with an empty space.\n\t- '{filename}' will be replaced with the current file name.\n\t- '{workspace}' will be replaced with the current workspace name, if any."
|
"description": "Custom string for the state section of the rich presence when debugging\n\t- '{null}' will be replaced with an empty space.\n\t- '{filename}' will be replaced with the current file name.\n\t- '{dirname}' will get replaced with the folder name that has the current file.\n\t- '{fulldirname}' will get replaced with the full directory name without the current file name..\n\t- '{workspace}' will be replaced with the current workspace name, if any.\n\t- '{currentline}' will get replaced with the current line number.\n\t- '{totallines}' will get replaced with the total line number.\n\t- '{filesize}' will get replaced with the current file's size."
|
||||||
},
|
},
|
||||||
"discord.lowerDetailsIdle": {
|
"discord.lowerDetailsIdle": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
|
@ -93,7 +93,7 @@
|
||||||
"discord.lowerDetailsNotFound": {
|
"discord.lowerDetailsNotFound": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"default": "No workspace.",
|
"default": "No workspace.",
|
||||||
"description": "Custom string for the state section of the rich presence when no workspace is found.\nIf set to '{null}', this will be an empty space."
|
"description": "Custom string for the state section of the rich presence when no workspace is found.\nIf set to '{null}', this will be an empty space.\n\t- '{currentline}' will get replaced with the current line number.\n\t- '{totallines}' will get replaced with the total line number.\n\t- '{filesize}' will get replaced with the current file's size."
|
||||||
},
|
},
|
||||||
"discord.largeImage": {
|
"discord.largeImage": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
|
|
111
src/extension.ts
111
src/extension.ts
|
@ -1,6 +1,6 @@
|
||||||
// Import the required functions & object types from various packages.
|
// Import the required functions & object types from various packages.
|
||||||
import { Client } from 'discord-rpc';
|
import { Client } from 'discord-rpc';
|
||||||
import { basename, extname } from 'path';
|
import { basename, extname, parse, sep } from 'path';
|
||||||
import { setInterval, clearInterval } from 'timers';
|
import { setInterval, clearInterval } from 'timers';
|
||||||
import {
|
import {
|
||||||
commands,
|
commands,
|
||||||
|
@ -14,6 +14,7 @@ import {
|
||||||
workspace,
|
workspace,
|
||||||
WorkspaceFolder
|
WorkspaceFolder
|
||||||
} from 'vscode';
|
} from 'vscode';
|
||||||
|
import { statSync } from 'fs';
|
||||||
const lang = require('./data/languages.json');
|
const lang = require('./data/languages.json');
|
||||||
|
|
||||||
const knownExtentions: { [x: string]: { image: string } } = lang.knownExtentions;
|
const knownExtentions: { [x: string]: { image: string } } = lang.knownExtentions;
|
||||||
|
@ -259,26 +260,104 @@ function setActivity(workspaceElapsedTime: boolean = false): void {
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateDetails(debugging, editing, idling): string {
|
function generateDetails(debugging, editing, idling): string {
|
||||||
|
let string: string = config.get(idling);
|
||||||
|
const emptySpaces = '\u200b\u200b';
|
||||||
|
|
||||||
const fileName: string = window.activeTextEditor ? basename(window.activeTextEditor.document.fileName) : null;
|
const fileName: string = window.activeTextEditor ? basename(window.activeTextEditor.document.fileName) : null;
|
||||||
|
let dirName: string = null;
|
||||||
|
if (window.activeTextEditor) {
|
||||||
|
const { dir } = parse(window.activeTextEditor.document.fileName);
|
||||||
|
const split = dir.split(sep);
|
||||||
|
dirName = split[split.length - 1];
|
||||||
|
}
|
||||||
const checkState: boolean = window.activeTextEditor
|
const checkState: boolean = window.activeTextEditor
|
||||||
? Boolean(workspace.getWorkspaceFolder(window.activeTextEditor.document.uri))
|
? Boolean(workspace.getWorkspaceFolder(window.activeTextEditor.document.uri))
|
||||||
: false;
|
: false;
|
||||||
|
|
||||||
const workspaceFolder: WorkspaceFolder = checkState ? workspace.getWorkspaceFolder(window.activeTextEditor.document.uri) : null;
|
const workspaceFolder: WorkspaceFolder = checkState ? workspace.getWorkspaceFolder(window.activeTextEditor.document.uri) : null;
|
||||||
|
|
||||||
const emptyDebugState: boolean = config.get(debugging) === '{null}';
|
let fullDirName: string = null;
|
||||||
const emptyEditingState: boolean = config.get(editing) === '{null}';
|
if (workspaceFolder) {
|
||||||
|
const { name } = workspaceFolder;
|
||||||
|
const relativePath = workspace.asRelativePath(window.activeTextEditor.document.fileName).split(sep);
|
||||||
|
relativePath.splice(-1, 1);
|
||||||
|
fullDirName = `${name}${sep}${relativePath.join(sep)}`;
|
||||||
|
}
|
||||||
|
|
||||||
return window.activeTextEditor
|
if (window.activeTextEditor) {
|
||||||
? debug.activeDebugSession
|
if (debug.activeDebugSession) {
|
||||||
? emptyDebugState
|
let rawString = config.get(debugging);
|
||||||
? '\u200b\u200b'
|
const { totalLines, size, currentLine } = getFileDetails(rawString);
|
||||||
: config.get(debugging)
|
rawString = rawString
|
||||||
|
.replace('{null}', emptySpaces)
|
||||||
.replace('{filename}', fileName)
|
.replace('{filename}', fileName)
|
||||||
.replace('{workspace}', checkState ? workspaceFolder.name : config.get('lowerDetailsNotFound'))
|
.replace('{dirname}', dirName)
|
||||||
: emptyEditingState
|
.replace('{fulldirname}', fullDirName)
|
||||||
? '\u200b\u200b'
|
.replace('{workspace}',
|
||||||
: config.get(editing)
|
checkState ?
|
||||||
|
workspaceFolder.name :
|
||||||
|
config.get('lowerDetailsNotFound').replace('{null}', emptySpaces)
|
||||||
|
);
|
||||||
|
if (totalLines) rawString = rawString.replace('{totallines}', totalLines);
|
||||||
|
if (size) rawString = rawString.replace('{filesize}', size);
|
||||||
|
if (currentLine) rawString = rawString.replace('{currentline}', currentLine);
|
||||||
|
string = rawString;
|
||||||
|
} else {
|
||||||
|
let rawString = config.get(editing);
|
||||||
|
const { totalLines, size, currentLine } = getFileDetails(rawString);
|
||||||
|
rawString = rawString
|
||||||
|
.replace('{null}', emptySpaces)
|
||||||
.replace('{filename}', fileName)
|
.replace('{filename}', fileName)
|
||||||
.replace('{workspace}', checkState ? workspaceFolder.name : config.get('lowerDetailsNotFound'))
|
.replace('{dirname}', dirName)
|
||||||
: config.get(idling);
|
.replace('{fulldirname}', fullDirName)
|
||||||
|
.replace('{workspace}',
|
||||||
|
checkState ?
|
||||||
|
workspaceFolder.name :
|
||||||
|
config.get('lowerDetailsNotFound').replace('{null}', emptySpaces)
|
||||||
|
);
|
||||||
|
if (totalLines) rawString = rawString.replace('{totallines}', totalLines);
|
||||||
|
if (size) rawString = rawString.replace('{filesize}', size);
|
||||||
|
if (currentLine) rawString = rawString.replace('{currentline}', currentLine);
|
||||||
|
string = rawString;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getFileDetails(rawString): FileDetail {
|
||||||
|
const obj = {
|
||||||
|
size: null,
|
||||||
|
totalLines: null,
|
||||||
|
currentLine: null,
|
||||||
|
};
|
||||||
|
if (!rawString) return obj;
|
||||||
|
if (rawString.includes('{totallines}')) {
|
||||||
|
obj.totalLines = window.activeTextEditor.document.lineCount.toLocaleString();
|
||||||
|
}
|
||||||
|
if (rawString.includes('{currentline}')) {
|
||||||
|
obj.currentLine = (window.activeTextEditor.selection.active.line + 1).toLocaleString();
|
||||||
|
}
|
||||||
|
if (rawString.includes('{filesize}')) {
|
||||||
|
const sizes = ['bytes', 'kb', 'mb', 'gb', 'tb'];
|
||||||
|
let currentDivision = 0;
|
||||||
|
let { size } = statSync(window.activeTextEditor.document.fileName);
|
||||||
|
const originalSize = size;
|
||||||
|
if (originalSize > 1000) {
|
||||||
|
size = size / 1000;
|
||||||
|
currentDivision++;
|
||||||
|
while (size > 1000) {
|
||||||
|
currentDivision++;
|
||||||
|
size = size / 1000;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
obj.size = `${originalSize > 1000 ? size.toFixed(2) : size}${sizes[currentDivision]}`;
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
type FileDetail = {
|
||||||
|
size: string | null,
|
||||||
|
totalLines: string | null,
|
||||||
|
currentLine: string | null,
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in a new issue