Skip to content

Commit

Permalink
Lib (#17)
Browse files Browse the repository at this point in the history
* mv main.go to cmd/calma/

* split file
  • Loading branch information
ddddddO authored Jul 14, 2022
1 parent 2959050 commit b256971
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 65 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ Markdown output image<br>
go version is 1.16 or higher.

```console
go install github.com/ddddddO/calma@latest
go install github.com/ddddddO/calma/cmd/calma@latest
```

go version is 1.15 or less.
```console
go get github.com/ddddddO/calma
go get github.com/ddddddO/calma/cmd/calma
```

or using Homebrew.
Expand Down
64 changes: 11 additions & 53 deletions main.go → calendar.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package main
package calma

import (
"bytes"
"flag"
"fmt"
"os"
"sync"
"text/template"
"time"
Expand All @@ -15,52 +13,6 @@ import (
"golang.org/x/xerrors"
)

var jst = time.FixedZone("JST", +9*60*60)

func main() {
flag.Usage = func() {
usage := "This CLI outputs Japanese calendar in Markdown. It supports national holidays.\n\nUsage of %s:\n"
fmt.Fprintf(flag.CommandLine.Output(), usage, os.Args[0])
flag.PrintDefaults()
}
var retreat, advance int
flag.IntVar(&retreat, "r", 0, "Number of months to retreat")
flag.IntVar(&advance, "a", 0, "Number of months to advance")
var isHTML bool
flag.BoolVar(&isHTML, "html", false, "Output html")
flag.Parse()

if retreat != 0 && advance != 0 {
fmt.Fprintln(os.Stderr, xerrors.New("Please use either"))
os.Exit(1)
}

targetDate := time.Now().In(jst)
if retreat != 0 {
targetDate = targetDate.AddDate(0, -retreat, 0)
}
if advance != 0 {
targetDate = targetDate.AddDate(0, advance, 0)
}

calendar, err := newCalendar(targetDate)
if err != nil {
fmt.Fprintln(os.Stderr, xerrors.Errorf("%+v", err))
os.Exit(1)
}
if err := calendar.build(); err != nil {
fmt.Fprintln(os.Stderr, xerrors.Errorf("%+v", err))
os.Exit(1)
}

if isHTML {
html := calendar.html()
fmt.Print(html)
return
}
fmt.Print(calendar)
}

const (
weekTemplate = `{{ if eq .Sunday.HolidayType 1 }} <font color="red">{{ if .Sunday.IsTargetMonth }}<b>{{ end }}{{.Sunday.N}}</font> {{ else if eq .Sunday.HolidayType 2 }} <font color="blue">{{ if .Sunday.IsTargetMonth }}<b>{{ end }}{{.Sunday.N}}</font> {{ else }} {{ if .Sunday.IsTargetMonth }}<b>{{ end }}{{.Sunday.N}} {{ end }}` +
`|{{ if eq .Monday.HolidayType 1 }} <font color="red">{{ if .Monday.IsTargetMonth }}<b>{{ end }}{{.Monday.N}}</font> {{ else if eq .Monday.HolidayType 2 }} <font color="blue">{{ if .Monday.IsTargetMonth }}<b>{{ end }}{{.Monday.N}}</font> {{ else }} {{ if .Monday.IsTargetMonth }}<b>{{ end }}{{.Monday.N}} {{ end }}` +
Expand All @@ -78,25 +30,31 @@ type calendar struct {
buf *bytes.Buffer
}

func newCalendar(target time.Time) (*calendar, error) {
func NewCalendar(target time.Time) (*calendar, error) {
tmpl, err := template.New("week").Parse(weekTemplate)
if err != nil {
return nil, xerrors.Errorf("failed to template.New: %w", err)
}

return &calendar{
c := &calendar{
weekTemplate: tmpl,
target: target,
month: &month{},
buf: &bytes.Buffer{},
}, nil
}

if err := c.build(); err != nil {
return nil, xerrors.Errorf("failed to build calendar: %w", err)
}

return c, nil
}

func (c *calendar) String() string {
return c.buf.String()
}

func (c *calendar) html() string {
func (c *calendar) HTML() string {
md := []byte(c.String())
html := markdown.ToHTML(md, nil, nil)
return string(html)
Expand Down
16 changes: 6 additions & 10 deletions main_test.go → calendar_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package calma

import (
"fmt"
Expand All @@ -9,6 +9,8 @@ import (
"github.com/stretchr/testify/assert"
)

var jst = time.FixedZone("JST", +9*60*60)

func TestString(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -89,10 +91,7 @@ func TestString(t *testing.T) {
for _, tt := range tests {
t.Log(tt.name)

calendar, err := newCalendar(tt.date)
assert.NoError(t, err)

err = calendar.build()
calendar, err := NewCalendar(tt.date)
assert.NoError(t, err)

got := fmt.Sprint(calendar)
Expand Down Expand Up @@ -204,13 +203,10 @@ func TestHTML(t *testing.T) {
for _, tt := range tests {
t.Log(tt.name)

calendar, err := newCalendar(tt.date)
assert.NoError(t, err)

err = calendar.build()
calendar, err := NewCalendar(tt.date)
assert.NoError(t, err)

got := calendar.html()
got := calendar.HTML()
assert.Equal(t, tt.want, got)
}
}
54 changes: 54 additions & 0 deletions cmd/calma/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"flag"
"fmt"
"os"
"time"

"golang.org/x/xerrors"

"github.com/ddddddO/calma"
)

var jst = time.FixedZone("JST", +9*60*60)

func main() {
flag.Usage = func() {
usage := "This CLI outputs Japanese calendar in Markdown. It supports national holidays.\n\nUsage of %s:\n"
fmt.Fprintf(flag.CommandLine.Output(), usage, os.Args[0])
flag.PrintDefaults()
}
var retreat, advance int
flag.IntVar(&retreat, "r", 0, "Number of months to retreat")
flag.IntVar(&advance, "a", 0, "Number of months to advance")
var isHTML bool
flag.BoolVar(&isHTML, "html", false, "Output html")
flag.Parse()

if retreat != 0 && advance != 0 {
fmt.Fprintln(os.Stderr, xerrors.New("Please use either"))
os.Exit(1)
}

targetDate := time.Now().In(jst)
if retreat != 0 {
targetDate = targetDate.AddDate(0, -retreat, 0)
}
if advance != 0 {
targetDate = targetDate.AddDate(0, advance, 0)
}

calendar, err := calma.NewCalendar(targetDate)
if err != nil {
fmt.Fprintln(os.Stderr, xerrors.Errorf("%+v", err))
os.Exit(1)
}

if isHTML {
html := calendar.HTML()
fmt.Print(html)
return
}
fmt.Print(calendar)
}

0 comments on commit b256971

Please sign in to comment.