Permalink
Browse files

split out util functions in a separate file

  • Loading branch information...
1 parent 2c7fda8 commit 20485a7788bf4e2c9e329323fc9f6982d681b84e @tstriker tstriker committed Aug 13, 2012
Showing with 73 additions and 76 deletions.
  1. +6 −76 extension.js
  2. +67 −0 stuff.js
View
@@ -31,6 +31,7 @@ const _ = Gettext.gettext;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Convenience = Me.imports.convenience;
+const Stuff = Me.imports.stuff;
// TODO - why are we not using dbus introspection here or something?
@@ -65,77 +66,6 @@ let WindowsProxy = DBus.makeProxyClass({
-function formatDuration(minutes) {
- return "%02d:%02d".format((minutes - minutes % 60) / 60, minutes % 60)
-}
-
-function formatDurationHuman(minutes) {
- let hours = (minutes - minutes % 60) / 60;
- let mins = minutes % 60;
- let res = ""
-
- if (hours > 0 || mins > 0) {
- if (hours > 0)
- res += "%dh ".format(hours)
-
- if (mins > 0)
- res += "%dmin".format(mins)
- } else {
- res = "Just started"
- }
-
- return res;
-}
-
-function formatDurationHours(minutes) {
- return new Number(minutes / 60.0).toFixed(1) + "h";
-}
-
-function fromDbusFact(fact) {
- // converts a fact coming from dbus into a usable object
- function UTCToLocal(timestamp) {
- // TODO - is this really the way?!
- let res = new Date(timestamp)
- return new Date(res.setUTCMinutes(res.getUTCMinutes() + res.getTimezoneOffset()));
- }
-
- return {
- name: fact[4],
- startTime: UTCToLocal(fact[1]*1000),
- endTime: fact[2] != 0 ? UTCToLocal(fact[2]*1000) : null,
- description: fact[3],
- activityId: fact[5],
- category: fact[6],
- tags: fact[7],
- date: UTCToLocal(fact[8] * 1000),
- delta: Math.floor(fact[9] / 60), // minutes
- id: fact[0]
- }
-};
-
-function fromDbusFacts(facts) {
- let res = [];
- for each(var fact in facts) {
- res.push(fromDbusFact(fact));
- }
-
- return res;
-};
-
-
-function parseFactString(input) {
- let res = {
- "time": null,
- "activity": input,
- "category": null,
- "description": null,
- "tags": null,
- }
-
-
-}
-
-
/* a little box or something */
function HamsterBox() {
this._init.apply(this, arguments);
@@ -349,7 +279,7 @@ HamsterExtension.prototype = {
refresh: function() {
this._proxy.GetTodaysFactsRemote(DBus.CALL_FLAG_START, Lang.bind(this, function(response, err) {
- let facts = fromDbusFacts(response);
+ let facts = Stuff.fromDbusFacts(response);
this.currentActivity = null;
let fact = null;
@@ -380,7 +310,7 @@ HamsterExtension.prototype = {
activities.add(label, {row: i, col: 1});
label = new St.Label({style_class: 'cell-label'});
- label.set_text(formatDurationHuman(fact.delta))
+ label.set_text(Stuff.formatDurationHuman(fact.delta))
activities.add(label, {row: i, col: 2, x_expand: false});
@@ -439,7 +369,7 @@ HamsterExtension.prototype = {
let label = "";
for each (var category in categories) {
- label += category + ": " + formatDurationHours(byCategory[category]) + ", ";
+ label += category + ": " + Stuff.formatDurationHours(byCategory[category]) + ", ";
}
label = label.slice(0, label.length - 2); // strip trailing comma
this.activityEntry.summaryLabel.set_text(label);
@@ -458,7 +388,7 @@ HamsterExtension.prototype = {
this.icon.hide()
if (fact && !fact.endTime) {
- this.panelLabel.text = "%s %s".format(fact.name, formatDuration(fact.delta));
+ this.panelLabel.text = "%s %s".format(fact.name, Stuff.formatDuration(fact.delta));
} else {
this.panelLabel.text = "No activity";
}
@@ -472,7 +402,7 @@ HamsterExtension.prototype = {
// updates panel label. if fact is none, will set panel status to "no activity"
if (fact && !fact.endTime) {
- this.panelLabel.text = formatDuration(fact.delta);
+ this.panelLabel.text = Stuff.formatDuration(fact.delta);
this.icon.gicon = this._trackingIcon;
} else {
this.panelLabel.text = "";
View
@@ -0,0 +1,67 @@
+function formatDuration(minutes) {
+ return "%02d:%02d".format((minutes - minutes % 60) / 60, minutes % 60)
+}
+
+function formatDurationHuman(minutes) {
+ let hours = (minutes - minutes % 60) / 60;
+ let mins = minutes % 60;
+ let res = ""
+
+ if (hours > 0 || mins > 0) {
+ if (hours > 0)
+ res += "%dh ".format(hours)
+
+ if (mins > 0)
+ res += "%dmin".format(mins)
+ } else {
+ res = "Just started"
+ }
+
+ return res;
+}
+
+function formatDurationHours(minutes) {
+ return new Number(minutes / 60.0).toFixed(1) + "h";
+}
+
+function fromDbusFact(fact) {
+ // converts a fact coming from dbus into a usable object
+ function UTCToLocal(timestamp) {
+ // TODO - is this really the way?!
+ let res = new Date(timestamp)
+ return new Date(res.setUTCMinutes(res.getUTCMinutes() + res.getTimezoneOffset()));
+ }
+
+ return {
+ name: fact[4],
+ startTime: UTCToLocal(fact[1]*1000),
+ endTime: fact[2] != 0 ? UTCToLocal(fact[2]*1000) : null,
+ description: fact[3],
+ activityId: fact[5],
+ category: fact[6],
+ tags: fact[7],
+ date: UTCToLocal(fact[8] * 1000),
+ delta: Math.floor(fact[9] / 60), // minutes
+ id: fact[0]
+ }
+};
+
+function fromDbusFacts(facts) {
+ let res = [];
+ for each(var fact in facts) {
+ res.push(fromDbusFact(fact));
+ }
+
+ return res;
+};
+
+
+function parseFactString(input) {
+ let res = {
+ "time": null,
+ "activity": input,
+ "category": null,
+ "description": null,
+ "tags": null,
+ }
+}

0 comments on commit 20485a7

Please sign in to comment.