Skip to content

Commit

Permalink
Fixed due date printing
Browse files Browse the repository at this point in the history
  • Loading branch information
harrybrwn committed Mar 20, 2021
1 parent c0ec90c commit b7ddd89
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 65 deletions.
25 changes: 4 additions & 21 deletions cmd/commands/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,6 @@ func (ff *fileFinder) addToFlagSet(flagset *pflag.FlagSet) {
flagset.StringVar(&ff.search, "search", "", "search for files by name")
}

type dueDate struct {
id, name string
date time.Time
}

type dueDates []dueDate

func (dd dueDates) Len() int {
return len(dd)
}

func (dd dueDates) Swap(i, j int) {
dd[i], dd[j] = dd[j], dd[i]
}

func (dd dueDates) Less(i, j int) bool {
return dd[i].date.Before(dd[j].date)
}

func newDueCmd(flags *opts.Global) *cobra.Command {
var nolinks, all bool
dueCmd := &cobra.Command{
Expand Down Expand Up @@ -110,7 +91,7 @@ func newDueCmd(flags *opts.Global) *cobra.Command {
wg.Add(len(courses))
tab := internal.NewTable(cmd.OutOrStdout())
tab.SetAutoWrapText(false)
internal.SetTableHeader(tab, []string{"id", "name", "due"}, !flags.NoColor)
internal.SetTableHeader(tab, []string{"id", "name", "due", "time remaining"}, !flags.NoColor)
tab.SetAutoWrapText(true)
tab.SetColWidth(50)

Expand All @@ -119,9 +100,11 @@ func newDueCmd(flags *opts.Global) *cobra.Command {
WaitGroup: &wg,
Table: tab,
Now: time.Now(),
Location: time.Local, // parameterize this with time.LoadLocation
All: all,
}
for _, course := range courses {
go printer.PrintCourseAssignments(course, all)
go printer.PrintCourseAssignments(course)
}
wg.Wait()
return nil
Expand Down
13 changes: 7 additions & 6 deletions cmd/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/harrybrwn/edu/cmd/internal"
"github.com/harrybrwn/edu/cmd/internal/files"
"github.com/harrybrwn/edu/cmd/internal/opts"
"github.com/harrybrwn/edu/cmd/print"
"github.com/harrybrwn/edu/pkg/term"
"github.com/harrybrwn/go-canvas"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -168,18 +169,18 @@ func newCourseCmd(globals *opts.Global) *cobra.Command {
tab.Render()
} else if ass {
internal.SetTableHeader(tab, []string{"id", "name", "due date"}, !globals.NoColor)
var dates dueDates
var dates print.DueDates
for as := range course.Assignments() {
dueAt := as.DueAt.Local()
dates = append(dates, dueDate{
id: strconv.Itoa(as.ID),
name: as.Name,
date: dueAt,
dates = append(dates, print.DueDate{
Id: strconv.Itoa(as.ID),
Name: as.Name,
Date: dueAt,
})
}
sort.Sort(dates)
for _, d := range dates {
tab.Append([]string{d.id, d.name, d.date.Format(time.RFC822)})
tab.Append([]string{d.Id, d.Name, d.Date.Format(time.RFC822)})
}
tab.Render()
} else {
Expand Down
78 changes: 57 additions & 21 deletions cmd/print/assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package print
import (
"fmt"
"io"
"math"
"sort"
"strconv"
"strings"
"sync"
"time"

Expand All @@ -18,32 +20,35 @@ import (
type AssignmentPrinter struct {
io.Writer
*sync.WaitGroup
Table *tablewriter.Table
Now time.Time
tableMu sync.Mutex

Table *tablewriter.Table
Now time.Time
Location *time.Location
All bool
}

// PrintCourseAssignments prints all the assignments for one course.
func (p *AssignmentPrinter) PrintCourseAssignments(course *canvas.Course, all bool) {
var dates dueDates
func (p *AssignmentPrinter) PrintCourseAssignments(course *canvas.Course) {
var dates DueDates
for as := range course.Assignments(canvas.Opt("order_by", "due_at")) {
dueAt := as.DueAt.Local()
if all && dueAt.Before(p.Now) {
dueAt := as.DueAt.In(p.Location)
if !p.All && dueAt.Before(p.Now) {
continue
}
dates = append(dates, dueDate{
id: strconv.Itoa(as.ID),
name: as.Name,
date: dueAt,
dates = append(dates, DueDate{
Id: strconv.Itoa(as.ID),
Name: as.Name,
Date: dueAt,
})
}
sort.Sort(dates)

// rendering
p.tableMu.Lock()
p.tableMu.Lock() // might be using share shared table concurrently
fmt.Fprintln(p, term.Colorf(" %m", course.Name))
for _, d := range dates {
p.Table.Append([]string{d.id, d.name, d.date.Format(time.RFC822)})
p.Table.Append([]string{d.Id, d.Name, d.Date.Format(time.RFC822), humanizeDuration(d.Date.Sub(p.Now))})
}
if p.Table.NumLines() > 0 {
p.Table.Render()
Expand All @@ -56,22 +61,53 @@ func (p *AssignmentPrinter) PrintCourseAssignments(course *canvas.Course, all bo
p.Done()
}

// TODO: this may become useful in it's own package
type dueDate struct {
id, name string
date time.Time
func humanizeDuration(duration time.Duration) string {
days := int64(duration.Hours() / 24)
hours := int64(math.Mod(duration.Hours(), 24))
minutes := int64(math.Mod(duration.Minutes(), 60))
seconds := int64(math.Mod(duration.Seconds(), 60))

chunks := []struct {
singularName string
amount int64
}{
{"day", days},
{"hour", hours},
{"minute", minutes},
{"second", seconds},
}

parts := []string{}

for _, chunk := range chunks {
switch chunk.amount {
case 0:
continue
case 1:
parts = append(parts, fmt.Sprintf("%d %s", chunk.amount, chunk.singularName))
default:
parts = append(parts, fmt.Sprintf("%d %ss", chunk.amount, chunk.singularName))
}
}

return strings.Join(parts, " ")
}

type DueDate struct {
Id, Name string
Date time.Time
}

type dueDates []dueDate
type DueDates []DueDate

func (dd dueDates) Len() int {
func (dd DueDates) Len() int {
return len(dd)
}

func (dd dueDates) Swap(i, j int) {
func (dd DueDates) Swap(i, j int) {
dd[i], dd[j] = dd[j], dd[i]
}

func (dd dueDates) Less(i, j int) bool {
return dd[i].date.Before(dd[j].date)
func (dd DueDates) Less(i, j int) bool {
return dd[i].Date.Before(dd[j].Date)
}
21 changes: 17 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
stdlog "log"
"os"
"path/filepath"
"strings"

"github.com/gen2brain/beeep"
"github.com/harrybrwn/config"
Expand Down Expand Up @@ -88,7 +89,7 @@ func Execute() (err error) {
globalFlags := opts.Global{}
globalFlags.AddToFlagSet(root.PersistentFlags())

root.SetUsageTemplate(commandTemplate)
root.SetUsageTemplate(config.IndentedCobraHelpTemplate)
root.AddCommand(append(
commands.All(&globalFlags),
completionCmd,
Expand Down Expand Up @@ -191,8 +192,20 @@ func errmsg(msg interface{}) {
fmt.Fprintf(os.Stderr, "Error: %v\n", msg)
}

func init() {
cobra.AddTemplateFunc("indent", indent)
}

func indent(s string) string {
parts := strings.Split(s, "\n")
for i := range parts {
parts[i] = " " + parts[i]
}
return strings.Join(parts, "\n")
}

var commandTemplate = `Usage:
{{if .Runnable}}
{{ if (or .Runnable .HasAvailableSubCommands) }}
{{.UseLine}}{{end}}{{if gt (len .Aliases) 0}}
Aliases:
Expand All @@ -207,11 +220,11 @@ Available Commands:
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces | indent}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces | indent}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:
{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)

replace github.com/harrybrwn/go-canvas => ../../pkg/go-canvas
replace (
github.com/harrybrwn/config => ../../pkg/config
github.com/harrybrwn/go-canvas => ../../pkg/go-canvas
)
15 changes: 3 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/gen2brain/beeep v0.0.0-20200526185328-e9c15c258e28 h1:M2Zt3G2w6Q57GZndOYk42p7RvMeO8izO8yKTfIxGqxA=
github.com/gen2brain/beeep v0.0.0-20200526185328-e9c15c258e28/go.mod h1:ElSskYZe3oM8kThaHGJ+kiN2yyUMVXMZ7WxF9QqLDS8=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down Expand Up @@ -53,15 +54,10 @@ github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoA
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/harrybrwn/config v0.0.2 h1:FtWYgbgZsiGlCwe/ZcsSPb/tTb3pGMkPShMQznJ1YV4=
github.com/harrybrwn/config v0.0.2/go.mod h1:Qx/o16iSaWA6/rj1mwKY0MUyrb2YCUVY5iqKcEWSraA=
github.com/harrybrwn/errs v0.0.2-0.20200523142445-e4279967174e h1:RtaB1O5oXuA5jbz3TprWGk3KcfsaS5pkBnanTARjXSI=
github.com/harrybrwn/errs v0.0.2-0.20200523142445-e4279967174e/go.mod h1:xekiTrup1h5u3YF9bpJT7qD1cmgYBA8qN9iHd/h/sho=
github.com/harrybrwn/go-canvas v0.0.2-0.20200821044925-04b08bcecf29 h1:/aL4+pznAb3w4Vxc2BixmShPeDW5X/FL+iQKqRqZzUM=
github.com/harrybrwn/go-canvas v0.0.2-0.20200821044925-04b08bcecf29/go.mod h1:bC9IrjKdu7XVstPy+lAvPYH07W3TgD6m796BjqF5QWo=
github.com/harrybrwn/go-querystring v1.0.1-0.20200812230556-de172bc021ad h1:1aAZGfm8SrKkfVODudFMNcDNq2eflHTxFXqjzucxNOA=
github.com/harrybrwn/go-querystring v1.0.1-0.20200812230556-de172bc021ad/go.mod h1:vgIvUzro/BNvc4qAmR133SLQZDvURWGxCesSpBnjeQc=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand All @@ -86,7 +82,6 @@ github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+tw
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.3.3 h1:SzB1nHZ2Xi+17FP0zVQBHIZqvwRN9408fJO8h+eeNA8=
github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
Expand All @@ -96,7 +91,6 @@ github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8=
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand All @@ -119,15 +113,11 @@ github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
Expand Down Expand Up @@ -169,6 +159,7 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down

0 comments on commit b7ddd89

Please sign in to comment.