Skip to content

Commit

Permalink
fix: improper ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatGodApollo committed Nov 3, 2020
1 parent e52e008 commit 7e02e9d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
3 changes: 3 additions & 0 deletions cmd/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package cmd

const VERSION string = "0.2.0"
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ var verbose *bool
func Exec() {
app := cli.App("genday", "Generate a curday.dat file")

app.Spec = "[-v]"
app.Spec = "[--version] [--verbose]"

verbose = app.BoolOpt("v verbose", false, "Verbose debug mode")
app.Version("v version", "genday " + VERSION)

verbose = app.BoolOpt("V verbose", false, "Verbose debug mode")

app.Command("json", "generate a file from JSON", cmdJSON)

Expand Down
2 changes: 1 addition & 1 deletion lib/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c *Channel) ToBytes() []byte {
out.WriteString(c.Id)
out.WriteByte(0x00)
out.WriteString(fmt.Sprintf("%4s", c.Callsign))
out.Write([]byte{0x00, 0x00, 0x00, 0x00, 0x81, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8A, 0xFF, 0xFF, 0x30, 0x30, 0x00, 0x00, 0x03})
out.Write([]byte{0x00, 0x00, 0x00, 0x00, 0x81, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8A, 0xFF, 0xFF, '0', '0', 0x00, 0x00, 0x03})
out.WriteString(c.Id)
out.WriteByte(0x00)

Expand Down
8 changes: 8 additions & 0 deletions lib/curday.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package genday
import (
"bytes"
"fmt"
"sort"
"time"
)

Expand Down Expand Up @@ -70,8 +71,15 @@ func (c *Curday) ToBytes() []byte {

out.Write(c.Header())

sort.SliceStable(c.Channels, func(p, q int) bool {
return c.Channels[p].Channel < c.Channels[q].Channel
})

for _, channel := range c.Channels {
out.Write(channel.ToBytes())
sort.SliceStable(channel.Listings, func(p, q int) bool {
return channel.Listings[p].ZoneCorrectedSlot(c.Timezone) < channel.Listings[q].ZoneCorrectedSlot(c.Timezone)
})
for _, listing := range channel.Listings {
out.Write(listing.ToBytes(c))
}
Expand Down
17 changes: 11 additions & 6 deletions lib/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,25 @@ func NewListing(timeslot Timeslot, name string) *Listing {
}
}

func (l *Listing) ToBytes(c *Curday) []byte {
var out bytes.Buffer

// {str(timeslot)}\x001\x0034\x000\x000\x00{name}\x00
func (l *Listing) ZoneCorrectedSlot(timezone int) int {
ts := int(l.Timeslot)
ts -= (c.Timezone - 1) * 2
ts -= (timezone - 1) * 2

if ts > 48 {
ts -= 48
} else if ts <= 0 {
ts += 48
}

out.WriteString(fmt.Sprintf("%d", ts))
return ts
}

func (l *Listing) ToBytes(c *Curday) []byte {
var out bytes.Buffer

// {str(timeslot)}\x001\x0034\x000\x000\x00{name}\x00

out.WriteString(fmt.Sprintf("%d", l.ZoneCorrectedSlot(c.Timezone)))
out.WriteByte(0x00)
out.WriteString("1")
out.WriteByte(0x00)
Expand Down

0 comments on commit 7e02e9d

Please sign in to comment.