Skip to content

Commit

Permalink
feat: add list command
Browse files Browse the repository at this point in the history
  • Loading branch information
Diegiwg committed Jun 26, 2024
1 parent ac12196 commit c28fda4
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Make sure the `$GOPATH/bin` directory is added to your `$PATH` so you can run th
- **Description**: When you want to see how much time has passed without finishing the record, use this command.
- **Usage**: `tt show`

6. **list**: Lists all time records.
- **Description**: When you want to see all time records, use this command.
- **Usage**: `tt list [--limit: int]`

### Example Usage

```bash
Expand All @@ -57,6 +61,8 @@ tt show
# See that in 10 minutes it's time to stop working
tt stop
# Ends today's work, and see the result on the screen of how many hours have passed
tt list
# See the previous record in the list of registered records
```

## Contributing
Expand Down
56 changes: 56 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cmd

import (
"strconv"
"time"

"github.com/Diegiwg/cli"
"github.com/Diegiwg/tt/data"
"github.com/Diegiwg/tt/model"
)

func totalTime(r model.Record) string {
var acc time.Duration

count := len(r.Items)
for index, item := range r.Items {
start := item.Start
end := item.End

if end == (time.Time{}) {

if index != count-1 {
continue
}

end = time.Now()
}

acc += end.Sub(start)
}

return "Total time: " + acc.String()
}

func List(ctx *cli.Context) error {
table := data.ReadOrCreateRecord(ctx)
recordsInTable := len(table.Records)

limit, err := strconv.Atoi(ctx.Flags["limit"])
if err != nil {
limit = 10
}

println("List of last " + strconv.Itoa(limit) + " records total time:")

for index := recordsInTable - 1; index >= recordsInTable-limit; index-- {
if index < 0 {
break
}

r := table.Records[index]
println(totalTime(r))
}

return nil
}
6 changes: 6 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Make sure the `$GOPATH/bin` directory is added to your `$PATH` so you can run th
- **Description**: When you want to see how much time has passed without finishing the record, use this command.
- **Usage**: `tt show`

6. **list**: Lists all time records.
- **Description**: When you want to see all time records, use this command.
- **Usage**: `tt list [--limit: int]`

### Example Usage

```bash
Expand All @@ -57,6 +61,8 @@ tt show
# See that in 10 minutes it's time to stop working
tt stop
# Ends today's work, and see the result on the screen of how many hours have passed
tt list
# See the previous record in the list of registered records
```

## Contributing
Expand Down
6 changes: 6 additions & 0 deletions docs/pt/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Certifique-se de que o diretório `$GOPATH/bin` está adicionado ao seu `$PATH`
- **Descrição**: Quando você quiser ver quanto tempo passou sem finalizar o registro, use este comando.
- **Uso**: `tt show`

6. **list**: Lista todos os registros de tempo.
- **Descrição**: Quando você quiser ver todos os registros de tempo, use este comando.
- **Uso**: `tt list [--limit: int]`

### Exemplo de Uso

```bash
Expand All @@ -57,6 +61,8 @@ tt show
# Verifique que em 10 minutos é hora de parar de trabalhar
tt stop
# Termina o trabalho de hoje e vê o resultado na tela de quantas horas se passaram
tt show
# Veja o registro anterior na lista de registros cadastrados
```

## Contribuindo
Expand Down
8 changes: 8 additions & 0 deletions tt.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ func main() {
Exec: cmd.Show,
})

app.AddCommand(&cli.Command{
Name: "list",
Desc: "Lists all time records",
Help: "When you want to see all time records, use this command.",
Usage: "[--limit: int]",
Exec: cmd.List,
})

err := app.Run()
if err != nil {
println(err.Error())
Expand Down

0 comments on commit c28fda4

Please sign in to comment.