commit fe83519567bcdac02d9b1414dcbefe4e5cab7068
parent 5bc3a14acb7c99c27283f108f48f3408a9d46c30
Author: Demonstrandum <moi@knutsen.co>
Date: Tue, 26 May 2020 23:06:36 +0100
Add author information to !code command.
Diffstat:
3 files changed, 51 insertions(+), 10 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -2,6 +2,7 @@
secrets.json
.env
export_secrets.sh
+console.rb
# Node / NPM / Yarn
node_modules/
diff --git a/lib/commands/code.ts b/lib/commands/code.ts
@@ -1,9 +1,41 @@
import { readFileSync as read_file } from 'fs';
+import path from 'path';
+
+import git from 'nodegit';
import { FORMATS } from '../extensions';
import { glue_strings } from '../utils';
-export default (homescope : HomeScope) => {
+const GIT_DIR = path.resolve(process.cwd(), "./.git");
+
+type Authors = Record<string, number>;
+
+const file_authors = async (filename: string): Promise<Authors> => {
+ const authors = <Authors>{};
+
+ const repo = await git.Repository.open(GIT_DIR);
+ const blame = await git.Blame.file(repo, filename, ['p']);
+
+ for (let i = 0; i < blame.getHunkCount(); i++) {
+ // Hunk has bad type signatures, someone should tell `nodegit'.
+ const hunk: any = blame.getHunkByIndex(i);
+ for (let j = 0; j < hunk.linesInHunk(); j++) {
+ const oid = hunk.finalCommitId();
+ const commit = await repo.getCommit(oid);
+ const name = commit.author().name();
+
+ if (authors.hasOwnProperty(name))
+ authors[name] += 1;
+ else
+ authors[name] = 1;
+ }
+ }
+
+ return authors;
+};
+
+
+export default async (homescope : HomeScope) => {
const { message, args, CONFIG } = homescope;
const p = CONFIG.commands.prefix;
@@ -12,6 +44,9 @@ export default (homescope : HomeScope) => {
const command = args[0].startsWith(p) ? args[0].tail() : args[0];
+ if (command.match(/\//g))
+ return message.answer("No paths allowed...");
+
const expansion = CONFIG.commands.aliases[command];
if (expansion) return message.channel.send(`\`${p}${command}\``
+ `is an alias that expands to \`${p}${expansion}\`.`);
@@ -20,19 +55,28 @@ export default (homescope : HomeScope) => {
try {
const source = read_file(filename).toString();
- const msg = `Source code for \`${p}${command}\`:`;
+ const authors = await file_authors(filename);
+ const author_str = "\n**Authors**: " + Object.keys(authors)
+ .map(author => `(\`${authors[author]}\`) ${author}`)
+ .join(',');
+
+ const msg = `Source code for \`${p}${command}\`:\n`;
- if (source.length > 1900) {
+ const msg_len = [msg, source, author_str]
+ .reduce((acc, s) => acc + s.length, 0);
+
+ if (msg_len > 1990) {
const chunks = glue_strings(source.split(/^/m), 1950);
message.channel.send(msg);
-
for (const chunk of chunks)
message.channel.send(
chunk.format(FORMATS.code_block, 'typescript'));
+ message.channel.send(author_str);
} else {
- message.channel.send(`${msg}\n`
- + source.format(FORMATS.code_block, 'typescript'));
+ message.channel.send(msg
+ + source.format(FORMATS.code_block, 'typescript')
+ + author_str);
}
} catch {
message.answer(`Source for \`${p}${command}\``
diff --git a/test.sh b/test.sh
@@ -1,4 +0,0 @@
- #/bin/bash
- for i; do
- echo "Arg: '$i'"
- done