Skip to content

Commit

Permalink
Implement --output flag for timetrace status (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikbraun authored Jun 15, 2021
1 parent ecfae17 commit a691827
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 109 deletions.
194 changes: 105 additions & 89 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
![CLI screenshot 64x16](timetrace.png)

:fire: **New:** [Display the tracking status as JSON or in your own format](#print-the-tracking-status)
:fire: **New:** [Reverting `edit` and `delete` commands is now possible](#edit-a-record)

## Installation
Expand Down Expand Up @@ -112,10 +113,113 @@ when = "timetrace status"
shell = "sh"
```

You can find a list of available formatting variables in the [`status` reference](#print-current-status).
You can find a list of available formatting variables in the [`status` reference](#print-the-tracking-status).

## Command reference

### Start tracking

**Syntax:**

```
timetrace start <PROJECT KEY>
```

**Arguments:**

|Argument|Description|
|-|-|
|`PROJECT KEY`|The key of the project.|

**Flags:**

|Flag|Short|Description|
|-|-|-|
|`--billable`|`-b`|Mark the record as billable.|

**Example:**

Start working on a project called `make-coffee` and mark it as billable:

```
timetrace start --billable make-coffee
```

### Print the tracking status

**Syntax:**

```
timetrace status
```

**Flags:**

|Flag|Short|Description|
|-|-|-|
|`--format`|`-f`|Display the status in a custom format (see below).|
|`--output`|`-o`|Display the status in a specific output. Valid values: `json`|

**Formatting variables:**

The names of the formatting variables are the same as the JSON keys printed by `--output json`.

|Variable|Description|
|-|-|
|`{project}`|The key of the current project.|
|`{trackedTimeCurrent}`|The time tracked for the current record.|
|`{trackedTimeToday}`|The time tracked today.|
|`{breakTimeToday}`|The break time since the first record.|

**Example:**

Print the current tracking status:

```
timetrace status
+-------------------+----------------------+----------------+
| CURRENT PROJECT | WORKED SINCE START | WORKED TODAY |
+-------------------+----------------------+----------------+
| make-coffee | 1h 15min | 4h 30min |
+-------------------+----------------------+----------------+
```

Print the current project and the total working time as a custom string. Given the example above, the output will be
`Current project: make-coffee - Worked today: 3h 30min`.

```
timetrace status --format "Current project: {project} - Worked today: {trackedTimeToday}"
```

Print the status as JSON:

```
timetrace status -o json
{
"project": "web-store",
"trackedTimeCurrent": "1h 45min",
"trackedTimeToday": "7h 30min",
"breakTimeToday": "0h 30min"
}
```

### Stop tracking

**Syntax:**

```
timetrace stop
```

**Example:**

Stop working on your current project:

```
timetrace stop
```

### Create a project

**Syntax:**
Expand Down Expand Up @@ -401,94 +505,6 @@ timetrace delete record 2021-05-01-15-00
timetrace delete record 2021-05-01-15-00 --revert
```

### Start tracking

**Syntax:**

```
timetrace start <PROJECT KEY>
```

**Arguments:**

|Argument|Description|
|-|-|
|`PROJECT KEY`|The key of the project.|

**Flags:**

|Flag|Short|Description|
|-|-|-|
|`--billable`|`-b`|Mark the record as billable.|

**Example:**

Start working on a project called `make-coffee` and mark it as billable:

```
timetrace start --billable make-coffee
```

### Print current status

**Syntax:**

```
timetrace status
```

**Flags:**

|Flag|Short|Description|
|-|-|-|
|`--format`|`-f`|Display the status in a custom format (see below).|

**Formatting variables:**

|Variable|Description|
|-|-|
|`{project}`|The key of the current project.|
|`{trackedTimeCurrent}`|The time tracked for the current record.|
|`{trackedTimeToday}`|The time tracked today.|
|`{breakTimeToday}`|The break time since the first record.|

**Example:**

Print the current tracking status:

```
timetrace status
+-------------------+----------------------+----------------+
| CURRENT PROJECT | WORKED SINCE START | WORKED TODAY |
+-------------------+----------------------+----------------+
| make-coffee | 1h 15min | 4h 30min |
+-------------------+----------------------+----------------+
```

Print the current project and the total working time as a custom string:

```
timetrace status --format "Current project: {project} - Worked today: {trackedTimeToday}"
```

Given the example above, the output will be `Current project: make-coffee - Worked today: 3h 30min`.

### Stop tracking

**Syntax:**

```
timetrace stop
```

**Example:**

Stop working on your current project:

```
timetrace stop
```

### Print version information

**Syntax:**
Expand Down
75 changes: 55 additions & 20 deletions cli/status.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"encoding/json"
"errors"
"fmt"
"strings"
Expand All @@ -11,8 +12,20 @@ import (
"github.com/spf13/cobra"
)

type statusReport struct {
Project string `json:"project"`
TrackedTimeCurrent string `json:"trackedTimeCurrent"`
TrackedTimeToday string `json:"trackedTimeToday"`
BreakTimeToday string `json:"breakTimeToday"`
}

type statusOptions struct {
format string
output string
}

func statusCommand(t *core.Timetrace) *cobra.Command {
var format string
var options statusOptions

status := &cobra.Command{
Use: "status",
Expand All @@ -32,41 +45,63 @@ func statusCommand(t *core.Timetrace) *cobra.Command {
return
}

project := defaultString
statusReport := statusReport{
Project: defaultString,
TrackedTimeCurrent: defaultString,
TrackedTimeToday: t.Formatter().FormatTodayTime(report),
BreakTimeToday: t.Formatter().FormatBreakTime(report),
}

if report.Current != nil {
project = report.Current.Project.Key
statusReport.Project = report.Current.Project.Key
}

trackedTimeCurrent := defaultString

if report.TrackedTimeCurrent != nil {
trackedTimeCurrent = t.Formatter().FormatCurrentTime(report)
statusReport.TrackedTimeCurrent = t.Formatter().FormatCurrentTime(report)
}

rows := [][]string{
{
project,
trackedTimeCurrent,
t.Formatter().FormatTodayTime(report),
t.Formatter().FormatBreakTime(report),
},
}
if format != "" {
format = strings.ReplaceAll(format, "{project}", project)
format = strings.ReplaceAll(format, "{trackedTimeCurrent}", trackedTimeCurrent)
format = strings.ReplaceAll(format, "{trackedTimeToday}", t.Formatter().FormatTodayTime(report))
format = strings.ReplaceAll(format, "{breakTimeToday}", t.Formatter().FormatBreakTime(report))
if options.format != "" {
format := options.format
format = strings.ReplaceAll(format, "{project}", statusReport.Project)
format = strings.ReplaceAll(format, "{trackedTimeCurrent}", statusReport.TrackedTimeCurrent)
format = strings.ReplaceAll(format, "{trackedTimeToday}", statusReport.BreakTimeToday)
format = strings.ReplaceAll(format, "{breakTimeToday}", statusReport.BreakTimeToday)
format = strings.ReplaceAll(format, `\n`, "\n")
fmt.Printf(format)
return
}

if options.output != "" {
switch options.output {
case "json":
bytes, err := json.MarshalIndent(statusReport, "", "\t")
if err != nil {
out.Err("error printing JSON: %s", err.Error())
return
}
fmt.Println(string(bytes))
return
default:
out.Err("unknown output format: %s", options.format)
return
}
}

rows := [][]string{
{
statusReport.Project,
statusReport.TrackedTimeCurrent,
statusReport.TrackedTimeToday,
statusReport.BreakTimeToday,
},
}

out.Table([]string{"Current project", "Worked since start", "Worked today", "Breaks"}, rows, nil)
},
}

status.Flags().StringVarP(&format, "format", "f", "", "Format string, availiable:\n{project}, {trackedTimeCurrent}, {trackedTimeToday}, {breakTimeToday}")
status.Flags().StringVarP(&options.format, "format", "f", "", "Format string, availiable:\n{project}, {trackedTimeCurrent}, {trackedTimeToday}, {breakTimeToday}")
status.Flags().StringVarP(&options.output, "output", "o", "", "The output format. Available: json")

return status
}

0 comments on commit a691827

Please sign in to comment.