Better algorithm to calculate the level

This commit is contained in:
Ionică Bizău 2015-02-01 10:45:03 +02:00
parent 34d48dcb6a
commit 2c7516f3d4

View file

@ -2,28 +2,18 @@
var FsExtra = require("fs-extra")
, Ul = require("ul")
, Moment = require("moment")
, Couleurs = require("couleurs")()
, CliBox = require("cli-box")
;
// Constants
const STORE_PATH = Ul.USER_DIR + "/.git-stats"
, LEVELS = 5
, SQUARES = process.argv.indexOf("--no-ansi") !== -1 ? [
, SQUARES = [
"⬚"
, "▢"
, "▢"
, "▤"
, "▣"
, "⬛"
] : [
Couleurs.fg("⬚", "#eee")
, Couleurs.fg("▢", "#eee")
, Couleurs.fg("▢", "#d6e685")
, Couleurs.fg("▤", "#8cc665")
, Couleurs.fg("▤", "#8cc665")
, Couleurs.fg("▣", "#44a340")
, Couleurs.fg("▣", "#1e6823")
]
, DAYS = [
"Sun"
@ -119,10 +109,12 @@ GitStats.iterateDays = function (data, callback) {
var start = data.start
, end = data.end
, tomrrow = Moment(end.format(DATE_FORMAT), DATE_FORMAT).add(1, "days")
, endStr = tomrrow.format(DATE_FORMAT)
, cDay = null
;
while (start.format(DATE_FORMAT) !== end.format(DATE_FORMAT)) {
while (start.format(DATE_FORMAT) !== endStr) {
cDay = start.format(data.format);
callback(cDay, start);
start.add(1, "days")
@ -163,10 +155,13 @@ GitStats.graph = function (data, callback) {
GitStats.calendar = function (data, callback) {
GitStats.graph(data, function (err, graph) {
if (err) { return callback(err); }
var cal = { total: 0, days: {}, cStreak: 0, lStreak: 0 }
, cDay = null
, days = Object.keys(graph)
, max = 0
, levels = null
, cLevel = 0
;
days.forEach(function (c) {
@ -185,11 +180,15 @@ GitStats.calendar = function (data, callback) {
}
});
levels = max / LEVELS;
days.forEach(function (c) {
cDay = graph[c];
cal.days[c] = {
c: cDay.c
, level: cDay.c === 0 ? 0 : LEVELS - Math.floor(max / (cDay.c + 2))
, level: !levels
? 0 : (cLevel = Math.floor(cDay.c / levels)) >= 5
? 4 : !cLevel && cDay.c > 0 ? 1 : cLevel
};
});
@ -218,13 +217,16 @@ GitStats.ansiCalendar = function (data, callback) {
GitStats.iterateDays(function (cDay, mDay) {
sDay = mDay.format("ddd");
cDayObj = cal.days[cDay];
if (!cDayObj) return;
if (sDay === "Sun" && Object.keys(cWeek).length) {
year.push(cWeek);
cWeek = [" ", " ", " ", " ", " ", " ", " "];
}
if (!cDayObj) return;
if (!SQUARES[cDayObj.level]) {
debugger
}
cWeek[DAYS.indexOf(sDay)] = SQUARES[cDayObj.level];
});