Skip to content

Commit

Permalink
Added clock functions and parameters
Browse files Browse the repository at this point in the history
New configurations:
- 'org.addLeftZero' - allow left zero padding on months, dates, hours and minutes (boolean, default false)
- 'org.clockInOutSeparator' - set the separator between clock in and clock out blocks (string, default "--")
- 'org.clockTotalSeparator' - set the separator between clocks and the clock total (string, default " =>  ")

New commands:
- 'org.clockin' - Adds the "CLOCK:" tag (if it doesn't already exists) and a [{year}-{month}-{day} {weekday} {hours}:{minutes}] timestamp
- 'org.clockout' - Clears previous clock out block (if exists), adds clockInOutSeparator and a [{year}-{month}-{day} {weekday} {hours}:{minutes}] timestamp
- 'org.updateclock' - Clears previous clock total block (if exists), add clockTotalSeparator and the difference between clockin and clockout in the {hours}:{minutes} format.
  Raises error if there's less than two timestamp blocks

Other additions/changes:
<simple-datetime.ts>
- interface 'ISimpleDateTime' - For the clock functions, extends ISimpleDate with hours and minutes
- function 'buildDateString' - Add verification for org.addLeftZero
- function 'padVal' - Renamed to padDate
- function 'buildDateTimeString' - Like buildDateString, but adds time
- function 'padTime' - Like padDate, but for times
- function 'dateToSimpleDateTime' - Like dateToSimpleDate, but returns a ISimpleDateTime
- function 'currentDatetime' - Renamed to 'currentDate'
- function 'currentDateTime' - Like currentDate, but returns an ISimpleDateTime
- function 'getClockTotal' - Returns the full string (separator + {hours}:{minutes}) for the clock total
  • Loading branch information
Maianne Ribeiro committed Oct 3, 2017
1 parent 1421007 commit 49229bd
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 6 deletions.
42 changes: 42 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@
"command": "org.timestamp",
"title": "Org: Timestamp"
},
{
"command": "org.clockin",
"title": "Org: Clock In"
},
{
"command": "org.clockout",
"title": "Org: Clock Out"
},
{
"command": "org.updateclock",
"title": "Org: Update Clock Total"
},
{
"command": "org.incrementContext",
"title": "Org: Increment Context"
Expand Down Expand Up @@ -119,6 +131,21 @@
"DONE"
],
"description": "Specifies the types of TODO statuses available (for switching)."
},
"org.addLeftZero": {
"type": "boolean",
"default": false,
"description": "Add left zero to single-digit dates and months."
},
"org.clockInOutSeparator": {
"type": "string",
"default": "--",
"description": "Clock in and clock out separator"
},
"org.clockTotalSeparator": {
"type": "string",
"default": " => ",
"description": "Clock total separator"
}
}
},
Expand Down Expand Up @@ -164,6 +191,21 @@
"key": "ctrl+alt+o t",
"when": "editorLangId == 'org'"
},
{
"command": "org.clockin",
"key": "ctrl+alt+o ctrl+i",
"when": "editorLangId == 'org'"
},
{
"command": "org.clockout",
"key": "ctrl+alt+o ctrl+o",
"when": "editorLangId == 'org'"
},
{
"command": "org.updateclock",
"key": "ctrl+alt+o ctrl+u",
"when": "editorLangId == 'org'"
},
{
"command": "org.incrementContext",
"key": "alt+right",
Expand Down
3 changes: 3 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export function activate(context: vscode.ExtensionContext) {
let demoteSubtreeCmd = vscode.commands.registerTextEditorCommand('org.demoteSubtree', SubtreeFunctions.demoteSubtree);

let insertTimestampCmd = vscode.commands.registerTextEditorCommand('org.timestamp', TimestampFunctions.insertTimestamp);
let clockInCmd = vscode.commands.registerTextEditorCommand('org.clockin', TimestampFunctions.clockIn);
let clockOutCmd = vscode.commands.registerTextEditorCommand('org.clockout', TimestampFunctions.clockOut);
let updateClockCmd = vscode.commands.registerTextEditorCommand('org.updateclock', TimestampFunctions.updateClock);

let incrementContextCmd = vscode.commands.registerTextEditorCommand('org.incrementContext', incrementContext);

Expand Down
81 changes: 77 additions & 4 deletions src/simple-datetime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import * as datefns from 'date-fns';
import * as Utils from './utils';

const weekdayArray = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];

Expand All @@ -10,6 +11,11 @@ export interface ISimpleDate {
weekday: string;
}

export interface ISimpleDateTime extends ISimpleDate {
hours: number;
minutes: number;
}

export function parseDate(dateString: string): ISimpleDate {
const dateRegExp = /(\d{4})-(\d{1,2})-(\d{1,2})/;
const dateResult = dateRegExp.exec(dateString);
Expand Down Expand Up @@ -38,16 +44,47 @@ export function buildDateString(datetime: ISimpleDate): string {
const { year, month, day, weekday } = datetime;

let dateString = `${year}-${month}-${day}`;

if (Utils.getLeftZero()) {
dateString = padDate(dateString);
}
if (weekday) {
dateString = `${dateString} ${weekday}`;
}

return `[${dateString}]`
};

function padVal(str: string): string {
return str.length === 1 ? `0${str}` : str;
export function buildDateTimeString(datetime: ISimpleDateTime): string {
const { year, month, day, hours, minutes, weekday } = datetime;

let dateString = `${year}-${month}-${day}`;
let timeString = `${hours}:${minutes}`;
if (Utils.getLeftZero()) {
dateString = padDate(dateString);
timeString = padTime(timeString);
}
if (weekday) {
dateString = `${dateString} ${weekday}`;
}


return `[${dateString} ${timeString}]`
};

function padDate(str: string): string {
let regex = /-(\d)(-|$)/;
while (regex.exec(str) !== null) {
str = str.replace(regex, '-0$1$2');
}
return str;
}

function padTime(str: string): string {
let regex = /(^|:)(\d)(:|$)/;
while (regex.exec(str) !== null) {
str = str.replace(regex, '$10$2$3');
}
return str;
}

export function dateToSimpleDate(dateObject: Date): ISimpleDate {
Expand All @@ -65,12 +102,26 @@ export function dateToSimpleDate(dateObject: Date): ISimpleDate {
}
}

export function currentDatetime(): ISimpleDate {
export function dateToSimpleDateTime(dateObject: Date): ISimpleDateTime {
let simpleDateTime = dateToSimpleDate(dateObject) as ISimpleDateTime;
simpleDateTime.hours = dateObject.getHours();
simpleDateTime.minutes = dateObject.getMinutes();

return simpleDateTime;
}

export function currentDate(): ISimpleDate {
const currentDate = new Date();

return dateToSimpleDate(new Date());
}

export function currentDateTime(): ISimpleDateTime {
const currentDate = new Date();

return dateToSimpleDateTime(new Date());
}

export function modifyDate(dateString: string, action: string): string {
const oldDate = parseDate(dateString);
let dateObject = datefns.parse(`${oldDate.year}-${oldDate.month}-${oldDate.day}`);
Expand All @@ -88,4 +139,26 @@ export function modifyDate(dateString: string, action: string): string {
}

return buildDateString(newDate);
}

export function getClockTotal(line) {
const separator = Utils.getClockTotalSeparator();

const regex = /\d{1,2}:\d{1,2}/g;
const match = line.match(regex);

if (match.length < 2) return '';

const clockIn = new Date(`2017-01-01 ${match[0]}`);
const clockOut = new Date(`2017-01-01 ${match[1]}`);
const clock = clockOut.getTime() - clockIn.getTime();
const hours = Math.floor(clock/(60*60*1000));
const minutes = clock/(60*1000) - (60*hours);

let clockString = `${hours}:${minutes}`;
if (Utils.getLeftZero()) {
clockString = padTime(clockString);
}

return clockString;
}
63 changes: 61 additions & 2 deletions src/timestamp-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,67 @@ export function insertTimestamp(textEditor: vscode.TextEditor, edit: vscode.Text
const document = Utils.getActiveTextEditorEdit();
const cursorPos = Utils.getCursorPosition();

const dateObject = Datetime.currentDatetime();
const dateObject = Datetime.currentDate();
const dateString = Datetime.buildDateString(dateObject);

edit.insert(cursorPos, dateString);
}
}

export function clockIn(textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) {
const document = Utils.getActiveTextEditorEdit();
const cursorPos = Utils.getCursorPosition();
const line = Utils.getLine(document, cursorPos);

if (line.indexOf('CLOCK:') === -1) {
edit.insert(cursorPos, 'CLOCK: ');
}

insertDateTime(edit, cursorPos);
}

export function clockOut(textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) {
const document = Utils.getActiveTextEditorEdit();
const cursorPos = Utils.getCursorPosition();
const line = Utils.getLine(document, cursorPos);

const separator = Utils.getClockInOutSeparator();
const separatorIndex = line.indexOf(separator);
if (separatorIndex !== -1) {
const initPos = new vscode.Position(cursorPos.line, separatorIndex);
const endPos = new vscode.Position(cursorPos.line, line.length);
const range = new vscode.Range(initPos, endPos);
edit.replace(range, '');
}
edit.insert(cursorPos, separator)
insertDateTime(edit, cursorPos);
}

export function updateClock(textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) {
const document = Utils.getActiveTextEditorEdit();
const cursorPos = Utils.getCursorPosition();
const line = Utils.getLine(document, cursorPos);

const clockTotal = Datetime.getClockTotal(line);
if (!clockTotal) {
vscode.window.showErrorMessage('You need two timestamps to update the clock total.');
return;
}

const separator = Utils.getClockTotalSeparator();
const separatorIndex = line.indexOf(separator);
if (separatorIndex !== -1) {
const initPos = new vscode.Position(cursorPos.line, separatorIndex);
const endPos = new vscode.Position(cursorPos.line, line.length);
const range = new vscode.Range(initPos, endPos);
edit.replace(range, '');
}
edit.insert(cursorPos, separator)
edit.insert(cursorPos, clockTotal);
}

function insertDateTime(edit: vscode.TextEditorEdit, cursorPos: vscode.Position) {
const dateTimeObject = Datetime.currentDateTime();
const dateTimeString = Datetime.buildDateTimeString(dateTimeObject);

edit.insert(cursorPos, dateTimeString);
}
18 changes: 18 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,24 @@ export function getKeywords() {
return todoKeywords;
}

export function getLeftZero() {
const settings = vscode.workspace.getConfiguration("org");
let addLeftZero = settings.get<boolean>("addLeftZero");
return addLeftZero;
}

export function getClockInOutSeparator() {
const settings = vscode.workspace.getConfiguration("org");
let clockInOutSeparator = settings.get<string>("clockInOutSeparator");
return clockInOutSeparator;
}

export function getClockTotalSeparator() {
const settings = vscode.workspace.getConfiguration("org");
let clockTotalSeparator = settings.get<string>("clockTotalSeparator");
return clockTotalSeparator;
}

export function getUniq(arr: string[]): string[] {
// Must also preserve order
let map = {};
Expand Down

0 comments on commit 49229bd

Please sign in to comment.