Skip to content

Commit

Permalink
Merge pull request #32 from matsuyoshi30/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
matsuyoshi30 authored Apr 6, 2022
2 parents e9a1734 + 34b0784 commit 744d509
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 81 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.16.x]
go-version: [1.17.x, 1.18.x]
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
Expand All @@ -24,8 +24,8 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2
- name: Build
run: go build
- uses: dominikh/staticcheck-action@v1.0.0
run: make build
- uses: dominikh/staticcheck-action@v1.2.0
name: staticcheck
with:
install-go: false
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ BINARY_NAME=germanium
all: build test
build:
$(GOBUILD) -o $(BINARY_NAME) -v ./cmd/$(BINARY_NAME)
test: build
test:
$(GOTEST) -v ./...
gentest: build
$(GOTEST) -v ./... -gen_golden_files
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ cd cmd/germanium && go install

#### Requirements

- go1.16
- Go (1.17 or 1.18)

## Related Projects

Expand Down
6 changes: 4 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var (
)

func Run() (err error) {
var opts Options

parser := flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash)
parser.Usage = fmt.Sprintf(Usage, name)

Expand Down Expand Up @@ -98,10 +100,10 @@ func Run() (err error) {
r = file
}

return run(r, filename)
return run(opts, r, filename)
}

func run(r io.Reader, filename string) error {
func run(opts Options, r io.Reader, filename string) error {
var (
out io.ReadWriter
err error
Expand Down
2 changes: 0 additions & 2 deletions cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,3 @@ type Options struct {
NoWindowAccessBar bool `long:"no-window-access-bar" description:"Hide the window access bar"`
ShowVersion bool `short:"v" long:"version" description:"Show version"`
}

var opts Options
4 changes: 3 additions & 1 deletion cmd/germanium/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"github.com/matsuyoshi30/germanium/cli"
)

var exit = os.Exit

func main() {
if err := cli.Run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
exit(1)
}
}
122 changes: 77 additions & 45 deletions cmd/germanium/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,95 @@ import (
"image"
"image/png"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"
)

var genGoldenFiles = flag.Bool("gen_golden_files", false, "whether to generate the golden files fot test")

func TestRun(t *testing.T) {
filepath.Walk("./testdata", func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".sh") {
cmd := exec.Command("bash", filepath.Base(path))
cmd.Dir = filepath.Dir(path)
if err := cmd.Run(); err != nil {
t.Errorf("FAIL: %v\n", err)
} else {
filename := strings.TrimSuffix(path, filepath.Ext(path))
if *genGoldenFiles {
if err := os.Rename(filename+"-gen.png", filename+".png"); err != nil {
t.Errorf("FAIL: %v\n", err)
}
t.Logf("Generate file: %s\n", filename+".png")
return nil
}
func TestMain(t *testing.T) {
defaultArg := []string{"germanium", filepath.Join("testdata", "main.go"), "-l", "go"}

wantFile := filename + ".png"
want, err := os.Open(wantFile)
if err != nil {
t.Errorf("FAIL: Reading want file: %s", wantFile)
}
wantImg, err := png.Decode(want)
if err != nil {
t.Errorf("FAIL: Decoding want file: %s", wantFile)
}
tests := []struct {
desc string
args []string
}{
{
desc: "only-editor",
args: []string{"--no-line-number", "--no-window-access-bar"},
},
{
desc: "default",
},
{
desc: "broken-style",
args: []string{"-s", "pygments"},
},
{
desc: "light-style",
args: []string{"-s", "autumn"},
},
{
desc: "no-line-num",
args: []string{"--no-line-number"},
},
{
desc: "no-window-access-bar",
args: []string{"--no-window-access-bar"},
},
{
desc: "style",
args: []string{"-s", "solarized-dark"},
},
}

gotFile := filename + "-gen.png"
got, err := os.Open(gotFile)
if err != nil {
t.Errorf("FAIL: Reading got file: %s", gotFile)
}
gotImg, err := png.Decode(got)
if err != nil {
t.Errorf("FAIL: Decoding got file: %s", gotFile)
}
for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
genfile := tt.desc + "-gen.png"
os.Args = append(defaultArg, append(tt.args, "-o", genfile)...)
exit = func(code int) { t.Fatalf("exit %d during main", code) }

main()

if reflect.DeepEqual(wantImg.(*image.RGBA), gotImg.(*image.RGBA)) {
t.Logf("PASS: %s\n", path)
} else {
t.Errorf("FAIL: output differs: %s\n", path)
if *genGoldenFiles {
if err := os.Rename(genfile, filepath.Join("testdata", tt.desc+".png")); err != nil {
t.Errorf("FAIL: %v\n", err)
}
t.Logf("Generate file: %s\n", tt.desc+".png")
return
}

want, err := os.Open(filepath.Join("testdata", tt.desc+".png"))
if err != nil {
t.Errorf("FAIL: reading want file: %s\n", tt.desc)
}
defer want.Close()
wantImg, err := png.Decode(want)
if err != nil {
t.Errorf("FAIL: decoding want file: %s\n", tt.desc)
}

got, err := os.Open(genfile)
if err != nil {
t.Errorf("FAIL: reading got file: %s\n", tt.desc)
}
defer got.Close()
gotImg, err := png.Decode(got)
if err != nil {
t.Errorf("FAIL: decoding got file: %s\n", tt.desc)
}

if !reflect.DeepEqual(wantImg.(*image.RGBA), gotImg.(*image.RGBA)) {
t.Errorf("FAIL: output differs: %s\n", tt.desc)
}

if err := os.Remove(gotFile); err != nil {
t.Errorf("FAIL: cleanup got file: %s\n", path)
if !*genGoldenFiles {
if err := os.Remove(genfile); err != nil {
t.Errorf("FAIL: cleanup got file: %s\n", tt.desc)
}
}
}
return nil
})
})
}
}
3 changes: 0 additions & 3 deletions cmd/germanium/testdata/broken-style.sh

This file was deleted.

2 changes: 0 additions & 2 deletions cmd/germanium/testdata/default.sh

This file was deleted.

4 changes: 0 additions & 4 deletions cmd/germanium/testdata/light-style.sh

This file was deleted.

2 changes: 0 additions & 2 deletions cmd/germanium/testdata/no-line-num.sh

This file was deleted.

2 changes: 0 additions & 2 deletions cmd/germanium/testdata/no-window-access-bar.sh

This file was deleted.

2 changes: 0 additions & 2 deletions cmd/germanium/testdata/only-editor.sh

This file was deleted.

2 changes: 0 additions & 2 deletions cmd/germanium/testdata/style.sh

This file was deleted.

28 changes: 19 additions & 9 deletions panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ import (
"golang.org/x/image/font"
)

var (
paddingWidth = 60
paddingHeight = 60
windowHeight = 20 * 3
lineWidth = 40
const (
paddingWidth = 60
paddingHeight = 60
windowHeight = 20 * 3
windowHeightNoBar = 10
lineWidth = 40

radius = 10
)

var (
// default window background color
windowBackgroundColor = color.RGBA{40, 42, 54, 255}

Expand Down Expand Up @@ -141,9 +144,9 @@ func (p *Panel) Draw() error {

// window control bar
if p.noWindowAccessBar {
windowHeight = 10
p.drawWindowControlPanel(width, windowHeightNoBar)
} else {
p.drawWindowControlPanel(width, height)
p.drawWindowControlPanel(width, windowHeight)
}

// round corner
Expand All @@ -159,7 +162,7 @@ func (p *Panel) drawWindowPanel(w, h int) {
}

func (p *Panel) drawWindowControlPanel(w, h int) {
wc := NewPanel(paddingWidth, paddingHeight, w-paddingWidth, paddingHeight+windowHeight)
wc := NewPanel(paddingWidth, paddingHeight, w-paddingWidth, paddingHeight+h)
wc.fillColor(windowBackgroundColor)

wc.drawControlButtons()
Expand Down Expand Up @@ -287,7 +290,14 @@ func (p *Panel) Label(out io.Writer, src io.Reader, filename, language string) e
Src: image.NewUniform(color.White),
Face: p.fontFace,
}
sp := image.Point{X: paddingWidth, Y: paddingHeight + windowHeight}

spy := paddingHeight
if p.noWindowAccessBar {
spy += windowHeightNoBar
} else {
spy += windowHeight
}
sp := image.Point{X: paddingWidth, Y: spy}
p.Formatter = NewPNGFormatter(p.fontSize, drawer, sp, !p.noLineNum)
formatters.Register("png", p.Formatter)

Expand Down

0 comments on commit 744d509

Please sign in to comment.