Skip to content

Commit 15a3d25

Browse files
committed
Add time format ceiling
1 parent 06de05b commit 15a3d25

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,23 @@ timeFormat: "[%sign][%minutes_overall]"
717717
1095
718718
```
719719

720+
##### `[%hours_overall:2]`, `[%days_overall:3]`
721+
722+
You can ceil any float value by adding the number of decimals to keep separated with a `:`.
723+
724+
**Example config:**
725+
726+
```yaml
727+
timeFormat: "[%sign][%hours_overall:2]"
728+
```
729+
730+
**Example outputs:**
731+
732+
```shell
733+
0,51
734+
18,25
735+
```
736+
720737
## how to use gtt as a library
721738

722739
Add as a dependency using yarn:

src/gtt-log.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ Cli.verbose = program.verbose;
1818

1919
let config = new Config(__dirname).set('hoursPerDay', program.hours_per_day),
2020
tasks = new Tasks(config),
21-
timeFormat = config.set('timeFormat', program.time_format).get('timeFormat');
22-
23-
timeFormat = _.isObject(timeFormat) && timeFormat['log'] ? timeFormat['log'] : timeFormat;
21+
timeFormat = config.set('timeFormat', program.time_format).get('timeFormat', 'log');
2422

2523
function toHumanReadable(input) {
2624
return Time.toHumanReadable(Math.ceil(input), config.get('hoursPerDay'), timeFormat);

src/models/time.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const defaultTimeFormat = '[%sign][%days>d ][%hours>h ][%minutes>m ][%seconds>s]
55
const mappings = ['complete', 'sign', 'weeks', 'days', 'hours', 'minutes', 'seconds'];
66
const regex = /^(?:([-])\s*)?(?:(\d+)w\s*)?(?:(\d+)d\s*)?(?:(\d+)h\s*)?(?:(\d+)m\s*)?(?:(\d+)s\s*)?$/;
77
const conditionalRegex = /(\[\%([^\>\]]*)\>([^\]]*)\])/ig;
8+
const roundedRegex = /(\[\%([^\>\]]*)\:([^\]]*)\])/ig;
89
const defaultRegex = /(\[\%([^\]]*)\])/ig;
910

1011
Number.prototype.padLeft = function (n, str) {
@@ -127,6 +128,13 @@ class time {
127128
output = output.replace(match[0], inserts[match[2]] > 0 ? inserts[match[2]] + match[3] : '');
128129
}
129130

131+
// rounded
132+
while ((match = roundedRegex.exec(format)) !== null) {
133+
if (match.index === roundedRegex.lastIndex) roundedRegex.lastIndex++;
134+
// console.log(match); process.exit();
135+
output = output.replace(match[0], Math.ceil(inserts[match[2]] * Math.pow(10, match[3])) / Math.pow(10, match[3]));
136+
}
137+
130138
// default
131139
format = output;
132140
while ((match = defaultRegex.exec(format)) !== null) {

0 commit comments

Comments
 (0)