mirror of
https://gitlab.com/aviortheking/code-stats-vscode.git
synced 2025-04-22 10:52:13 +00:00
Profile API
This commit is contained in:
parent
454fa33ce1
commit
35ce1e244f
2854
package-lock.json
generated
Normal file
2854
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -34,8 +34,13 @@
|
|||||||
},
|
},
|
||||||
"codestats.apiurl": {
|
"codestats.apiurl": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"default": "https://codestats.net/api/my/pulses",
|
"default": "https://codestats.net/api/",
|
||||||
"description": "Code::Stats API URL. Only change if you know what you are doing!"
|
"description": "Code::Stats API URL. Only change if you know what you are doing!"
|
||||||
|
},
|
||||||
|
"codestats.username": {
|
||||||
|
"type": "string",
|
||||||
|
"default": "",
|
||||||
|
"description": "Code::Stats User Name"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -4,13 +4,16 @@ import * as axios from "axios";
|
|||||||
|
|
||||||
export class CodeStatsAPI {
|
export class CodeStatsAPI {
|
||||||
private API_KEY = null;
|
private API_KEY = null;
|
||||||
private UPDATE_URL = "https://codestats.net/api/my/pulses";
|
private USER_NAME = null;
|
||||||
private PROFILE_URL = "https://codestats.net/api/users";
|
private UPDATE_URL = "https://codestats.net/api/";
|
||||||
|
//private PROFILE_URL = "https://codestats.net/api/users";
|
||||||
private axios = null;
|
private axios = null;
|
||||||
|
|
||||||
constructor(apiKey: string, apiURL: string) {
|
constructor(apiKey: string, apiURL: string, userName: string) {
|
||||||
this.API_KEY = apiKey;
|
this.API_KEY = apiKey;
|
||||||
this.UPDATE_URL = apiURL;
|
this.UPDATE_URL = apiURL;
|
||||||
|
this.USER_NAME = userName;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
this.API_KEY === null ||
|
this.API_KEY === null ||
|
||||||
this.API_KEY === undefined ||
|
this.API_KEY === undefined ||
|
||||||
@ -48,7 +51,7 @@ export class CodeStatsAPI {
|
|||||||
console.log(`JSON: ${json}`);
|
console.log(`JSON: ${json}`);
|
||||||
|
|
||||||
return this.axios
|
return this.axios
|
||||||
.post(this.UPDATE_URL, json)
|
.post("my/pulses", json)
|
||||||
.then(response => {
|
.then(response => {
|
||||||
console.log(response);
|
console.log(response);
|
||||||
})
|
})
|
||||||
@ -59,6 +62,18 @@ export class CodeStatsAPI {
|
|||||||
console.log(error);
|
console.log(error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getProfile(): axios.AxiosPromise {
|
||||||
|
return this.axios
|
||||||
|
.get(`users/${this.USER_NAME}`)
|
||||||
|
.then(response => {
|
||||||
|
console.log("Got Response\n");
|
||||||
|
return response.data;
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ApiJSON {
|
class ApiJSON {
|
||||||
|
116
src/profileHtmlProvider.ts
Normal file
116
src/profileHtmlProvider.ts
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
|
||||||
|
import {
|
||||||
|
Uri,
|
||||||
|
Event,
|
||||||
|
CancellationToken,
|
||||||
|
TextDocumentContentProvider
|
||||||
|
} from "vscode";
|
||||||
|
|
||||||
|
import { CodeStatsAPI } from "./code-stats-api";
|
||||||
|
|
||||||
|
export class ProfileHtmlProvider implements TextDocumentContentProvider {
|
||||||
|
onDidChange?: Event<Uri>;
|
||||||
|
|
||||||
|
api: CodeStatsAPI;
|
||||||
|
|
||||||
|
constructor(api: CodeStatsAPI) {
|
||||||
|
this.api = api;
|
||||||
|
}
|
||||||
|
|
||||||
|
provideTextDocumentContent(uri: Uri, token: CancellationToken): string | Thenable<string> {
|
||||||
|
|
||||||
|
const LEVEL_FACTOR = 0.025;
|
||||||
|
|
||||||
|
function getLevel(xp: number): number {
|
||||||
|
return Math.floor(Math.sqrt(xp) * LEVEL_FACTOR);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getNextLevelXp(level: number): number {
|
||||||
|
return Math.pow(Math.ceil((level+1)/LEVEL_FACTOR) ,2);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getLevelProgress(xp: number): number {
|
||||||
|
let level = getLevel(xp);
|
||||||
|
let curLevelXp = getNextLevelXp(level-1);
|
||||||
|
let nextLevelXp = getNextLevelXp(level);
|
||||||
|
|
||||||
|
let haveXp = xp - curLevelXp;
|
||||||
|
let needXp = nextLevelXp - curLevelXp;
|
||||||
|
|
||||||
|
return Math.round(haveXp * 100.0 / needXp);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getLanguages(languages: any): string {
|
||||||
|
|
||||||
|
let ret = '';
|
||||||
|
for( let lang in languages )
|
||||||
|
{
|
||||||
|
ret+=`<div class="language">${lang}<span style="display:block;">${languages[lang]["xps"]}</span></div>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getHeader(profile: any): string {
|
||||||
|
|
||||||
|
let userName = profile["user"];
|
||||||
|
let totalXp = profile["total_xp"];
|
||||||
|
let newXp = profile["new_xp"];
|
||||||
|
let currentLevel = getLevel(totalXp);
|
||||||
|
|
||||||
|
return `<h3> ${userName}'s Profile Level ${currentLevel} (${totalXp} XP) ${newXp > 0 ? '<sup>(+' + newXp +')</sup>' :'' }</h3>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return this.api.getProfile().then( profile => {
|
||||||
|
|
||||||
|
console.log(profile);
|
||||||
|
|
||||||
|
return `
|
||||||
|
<style>
|
||||||
|
.language {
|
||||||
|
float: left;
|
||||||
|
width: 6rem;
|
||||||
|
height: 6rem;
|
||||||
|
padding-top: 2rem;
|
||||||
|
text-align: center;
|
||||||
|
background: rgb(37,37,38);
|
||||||
|
margin: 0.1rem;
|
||||||
|
border-radius: 50%;
|
||||||
|
border-style: solid 2px;
|
||||||
|
border-color: black;
|
||||||
|
|
||||||
|
font-size: 0.9em;
|
||||||
|
-webkit-box-sizing: border-box;
|
||||||
|
-moz-box-sizing: border-box;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
.language span {
|
||||||
|
font-weight: bold;
|
||||||
|
font-size: 1.1em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.vs-dark .language {
|
||||||
|
color: #ddca7e;
|
||||||
|
}
|
||||||
|
|
||||||
|
.vs .language {
|
||||||
|
color: blue;
|
||||||
|
}
|
||||||
|
|
||||||
|
sup {
|
||||||
|
top: -.5em;
|
||||||
|
font-size: 75%;
|
||||||
|
}
|
||||||
|
|
||||||
|
h3 {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
${getHeader(profile)}
|
||||||
|
${getLanguages(profile["languages"])}
|
||||||
|
`;
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -6,7 +6,6 @@ import {
|
|||||||
Uri,
|
Uri,
|
||||||
ViewColumn,
|
ViewColumn,
|
||||||
commands,
|
commands,
|
||||||
TextDocumentContentProvider,
|
|
||||||
Event,
|
Event,
|
||||||
CancellationToken,
|
CancellationToken,
|
||||||
StatusBarItem,
|
StatusBarItem,
|
||||||
@ -18,61 +17,7 @@ import {
|
|||||||
} from "vscode";
|
} from "vscode";
|
||||||
import { Pulse } from "./pulse";
|
import { Pulse } from "./pulse";
|
||||||
import { CodeStatsAPI } from "./code-stats-api";
|
import { CodeStatsAPI } from "./code-stats-api";
|
||||||
|
import { ProfileHtmlProvider } from "./profileHtmlProvider";
|
||||||
class HtmlProvider implements TextDocumentContentProvider {
|
|
||||||
onDidChange?: Event<Uri>;
|
|
||||||
provideTextDocumentContent(uri: Uri, token: CancellationToken): string | Thenable<string> {
|
|
||||||
return `
|
|
||||||
<style>
|
|
||||||
.tile {
|
|
||||||
float: left;
|
|
||||||
width: 7rem;
|
|
||||||
height: 7rem;
|
|
||||||
padding-top: 2.5rem;
|
|
||||||
text-align: center;
|
|
||||||
background: gray;
|
|
||||||
margin: 0.1rem;
|
|
||||||
border-radius: 50%;
|
|
||||||
border-style: solid 2px;
|
|
||||||
border-color: black;
|
|
||||||
|
|
||||||
color: #ddca7e;
|
|
||||||
font-size: 0.8em;
|
|
||||||
-webkit-box-sizing: border-box;
|
|
||||||
-moz-box-sizing: border-box;
|
|
||||||
box-sizing: border-box;
|
|
||||||
}
|
|
||||||
|
|
||||||
.tile span {
|
|
||||||
font-weight: bold;
|
|
||||||
font-size: 1.2em;
|
|
||||||
}
|
|
||||||
|
|
||||||
.tile:nth-child(even) {
|
|
||||||
background: gray;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
<div class="tile">C#<span style="display:block;">10</span></div>
|
|
||||||
<div class="tile">Elixir</div>
|
|
||||||
<div class="tile">TypeScript</div>
|
|
||||||
<div class="tile">Rust</div>
|
|
||||||
<div class="tile">Html</div>
|
|
||||||
<div class="tile">PlainText</div>
|
|
||||||
<div class="tile">C++</div>
|
|
||||||
<div class="tile">VB</div>
|
|
||||||
<div class="tile">F#</div>
|
|
||||||
<div class="tile">C#</div>
|
|
||||||
<div class="tile">Elixir</div>
|
|
||||||
<div class="tile">TypeScript</div>
|
|
||||||
<div class="tile">Rust</div>
|
|
||||||
<div class="tile">Html</div>
|
|
||||||
<div class="tile">PlainText</div>
|
|
||||||
<div class="tile">C++</div>
|
|
||||||
<div class="tile">VB</div>
|
|
||||||
<div class="tile">F#</div>
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class XpCounter {
|
export class XpCounter {
|
||||||
private combinedDisposable: Disposable;
|
private combinedDisposable: Disposable;
|
||||||
@ -107,7 +52,8 @@ export class XpCounter {
|
|||||||
this.statusBarItem.command = "code-stats.profile";
|
this.statusBarItem.command = "code-stats.profile";
|
||||||
}
|
}
|
||||||
|
|
||||||
let provider = new HtmlProvider();
|
let provider = new ProfileHtmlProvider(this.api);
|
||||||
|
|
||||||
let registration = workspace.registerTextDocumentContentProvider('code-stats', provider);
|
let registration = workspace.registerTextDocumentContentProvider('code-stats', provider);
|
||||||
|
|
||||||
subscriptions.push(registration);
|
subscriptions.push(registration);
|
||||||
@ -190,12 +136,16 @@ export class XpCounter {
|
|||||||
|
|
||||||
const apiKey: string = config.get("apikey");
|
const apiKey: string = config.get("apikey");
|
||||||
const apiURL: string = config.get("apiurl");
|
const apiURL: string = config.get("apiurl");
|
||||||
|
const userName: string = config.get("username");
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
"code-stats-vscode setting up with API URL",
|
`code-stats-vscode setting up:
|
||||||
apiURL,
|
API URL: ${apiURL}
|
||||||
"and key",
|
NAME: ${userName}
|
||||||
apiKey
|
KEY: ${apiKey}
|
||||||
|
`
|
||||||
);
|
);
|
||||||
this.api = new CodeStatsAPI(apiKey, apiURL);
|
|
||||||
|
this.api = new CodeStatsAPI(apiKey, apiURL, userName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user