Skip to content

Commit

Permalink
Improve time rendering (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
feedmeapples authored Jun 7, 2021
1 parent b200ced commit 096dc5e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
37 changes: 37 additions & 0 deletions terminal/defs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package terminal

import (
"time"
)

const (
defaultListPageSize = 20

defaultTimeFormat = "15:04:05" // used for converting UnixNano to string like 16:16:36 (only time)
defaultDateTimeFormat = time.RFC3339 // used for converting UnixNano to string like 2018-02-15T16:16:36-08:00

)
12 changes: 11 additions & 1 deletion terminal/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const (
FlagDetach = "detach"
FlagJSON = "json"
FlagPageSize = "pagesize"
FlagRawTime = "raw-time"
FlagDateTime = "date-time"
)

var FlagsForPagination = []cli.Flag{
Expand All @@ -43,7 +45,7 @@ var FlagsForPagination = []cli.Flag{
},
&cli.IntFlag{
Name: FlagPageSize,
Value: 10,
Value: defaultListPageSize,
Usage: "items per page",
},
&cli.BoolFlag{
Expand All @@ -59,6 +61,14 @@ var FlagsForRendering = []cli.Flag{
Aliases: []string{"j"},
Usage: "print in json format",
},
&cli.BoolFlag{
Name: FlagRawTime,
Usage: "print raw time",
},
&cli.BoolFlag{
Name: FlagDateTime,
Usage: "print date time",
},
}

var FlagsForPaginationAndRendering = append(FlagsForPagination, FlagsForRendering...)
32 changes: 29 additions & 3 deletions terminal/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ import (
"os"
"reflect"
"strings"
"time"

"github.com/gogo/protobuf/proto"
"github.com/olekukonko/tablewriter"
"github.com/temporalio/shared-go/codec"
"github.com/temporalio/shared-go/timestamp"
"github.com/urfave/cli/v2"
)

Expand All @@ -51,11 +53,11 @@ func PrintItems(c *cli.Context, items []interface{}, opts *PrintOptions) {
if isJSON {
printJSON(items)
} else {
printTable(items, opts)
printTable(c, items, opts)
}
}

func printTable(items []interface{}, opts *PrintOptions) {
func printTable(c *cli.Context, items []interface{}, opts *PrintOptions) {
fields := opts.Fields
if len(fields) == 0 {
// dynamically examine fields
Expand Down Expand Up @@ -95,7 +97,7 @@ func printTable(items []interface{}, opts *PrintOptions) {
col = val.Interface()
val = reflect.ValueOf(col)
}
columns = append(columns, fmt.Sprintf("%v", col))
columns = append(columns, format(c, col))
val = reflect.ValueOf(item)
}
table.Append(columns)
Expand All @@ -104,6 +106,30 @@ func printTable(items []interface{}, opts *PrintOptions) {
table.ClearRows()
}

func format(c *cli.Context, i interface{}) string {
if reflect.TypeOf(i) == reflect.TypeOf(&time.Time{}) {
t := i.(*time.Time)

return formatTime(c, t)
}

return fmt.Sprintf("%v", i)
}

func formatTime(c *cli.Context, t *time.Time) string {
printRawTime := c.Bool(FlagRawTime)
printDateTime := c.Bool(FlagDateTime)

tt := timestamp.TimeValue(t)
if printRawTime {
return fmt.Sprintf("%v", tt)
} else if printDateTime {
return tt.Format(defaultDateTimeFormat)
} else {
return tt.Format(defaultTimeFormat)
}
}

func printJSON(o interface{}) {
var b []byte
var err error
Expand Down

0 comments on commit 096dc5e

Please sign in to comment.