Skip to content

Commit

Permalink
Merge pull request #11 from matsuyoshi30/change-package-as-a-library
Browse files Browse the repository at this point in the history
Change package as a library
  • Loading branch information
matsuyoshi30 authored May 1, 2021
2 parents b466d9b + 82a8aa1 commit 4352809
Show file tree
Hide file tree
Showing 30 changed files with 260 additions and 205 deletions.
11 changes: 9 additions & 2 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ before:
- go mod tidy

builds:
- main: .
- main: ./cmd/germanium
binary: germanium
ldflags: -s -w -X main.version={{.Version}} -X main.commit={{.ShortCommit}} -X main.date={{.Date}}
ldflags:
- -s -w
- -X github.com/matsuyoshi30/germanium/cli.version={{.Version}}
- -X github.com/matsuyoshi30/germanium/cli.commit={{.ShortCommit}}
- -X github.com/matsuyoshi30/germanium/cli.date={{.Date}}

archives:
- replacements:
Expand All @@ -24,3 +28,6 @@ archives:

release:
prerelease: auto

changelog:
skip: true
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ BINARY_NAME=germanium

all: build test
build:
$(GOBUILD) -o $(BINARY_NAME) -v
$(GOBUILD) -o $(BINARY_NAME) -v ./cmd/$(BINARY_NAME)
test: build
$(GOTEST) -v ./...
gentest:
$(GOTEST) -gen_golden_files ./...
clean:
$(GOCLEAN)
rm -f $(BINARY_NAME)
run:
$(GOBUILD) -o $(BINARY_NAME) -v ./...
./$(BINARY_NAME)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ yay germanium

```
git clone https://github.com/matsuyoshi30/germanium
cd germanium && go install
cd cmd/germanium && go install
```

#### Requirements
Expand Down
106 changes: 106 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package cli

import (
"fmt"
"io"
"os"
"path/filepath"

flags "github.com/jessevdk/go-flags"
"github.com/matsuyoshi30/germanium"
)

var name = "germanium"

var (
// these are set in build step
version = "unversioned"
commit = "?"
date = "?"
)

func Run() (err error) {
parser := flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash)
parser.Usage = fmt.Sprintf(Usage, name)

args, err := parser.Parse()
if err != nil {
if err, ok := err.(*flags.Error); ok {
fmt.Println(parser.Usage)

if err.Type != flags.ErrHelp {
fmt.Fprintln(os.Stderr, err.Error())
return nil
}
}
return nil
}

var filename string
if len(args) > 0 {
filename = args[0]
}

if opts.ListFonts {
germanium.ListFonts()
return nil
}

var r io.Reader
switch filename {
case "", "-":
if opts.Language == "" {
err = fmt.Errorf("If you want to use stdin, specify language")
return
}
r = os.Stdin
default:
if _, err := os.Stat(filename); os.IsNotExist(err) {
return err
}

file, err := os.Open(filename)
if err != nil {
return err
}
defer func() {
if err := file.Close(); err != nil {
return
}
}()
r = file
}

return run(r, filename)
}

func run(r io.Reader, filename string) error {
currentDir, err := os.Getwd()
if err != nil {
return err
}
out, err := os.Create(filepath.Join(currentDir, opts.Output))
if err != nil {
return err
}

face, err := germanium.LoadFont(opts.Font)
if err != nil {
return err
}

src, err := germanium.ReadString(r, face)
if err != nil {
return err
}

image := germanium.NewImage(src, face, opts.NoWindowAccessBar)
if err := image.Draw(opts.BackgroundColor, opts.NoWindowAccessBar); err != nil {
return err
}
if err := image.Label(out, filename, src, opts.Language, face, !opts.NoLineNum); err != nil {
return err
}

return nil
}
13 changes: 13 additions & 0 deletions cli/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cli

type Options struct {
Output string `short:"o" long:"output" default:"output.png" description:"Write output image to specific filepath"`
BackgroundColor string `short:"b" long:"background" default:"#aaaaff" description:"Background color of the image"`
Font string `short:"f" long:"font" default:"Hack-Regular" description:"Specify font eg. 'Hack-Bold'"`
Language string `short:"l" long:"language" description:"The language for syntax highlighting"`
ListFonts bool `long:"list-fonts" description:"List all available fonts in your system"`
NoLineNum bool `long:"no-line-number" description:"Hide the line number"`
NoWindowAccessBar bool `long:"no-window-access-bar" description:"Hide the window access bar"`
}

var opts Options
17 changes: 17 additions & 0 deletions cli/usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cli

const Usage = `USAGE:
%s [FLAGS] [FILE]
FLAGS:
-o, --output <PATH> Write output image to specific filepath [default: ./output.png]
-b, --background <COLOR> Background color of the image [default: #aaaaff]
-f, --font <FONT> Specify font eg. 'Hack-Bold'
-l, --language <LANG> The language for syntax highlighting eg. 'go'
--list-fonts List all available fonts in your system
--no-line-number Hide the line number
--no-window-access-bar Hide the window access bar
AUTHOR:
matsuyoshi30 <sfbgwm30@gmail.com>
`
16 changes: 16 additions & 0 deletions cmd/germanium/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"fmt"
"os"

"github.com/matsuyoshi30/germanium/cli"
)

func main() {
if err := cli.Run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return
}
File renamed without changes.
File renamed without changes
2 changes: 2 additions & 0 deletions cmd/germanium/testdata/default.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
../../../germanium main.go -o default-gen.png
File renamed without changes.
File renamed without changes.
File renamed without changes
2 changes: 2 additions & 0 deletions cmd/germanium/testdata/no-line-num.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
../../../germanium --no-line-number main.go -o no-line-num-gen.png
File renamed without changes
2 changes: 2 additions & 0 deletions cmd/germanium/testdata/no-window-access-bar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
../../../germanium --no-window-access-bar main.go -o no-window-access-bar-gen.png
File renamed without changes
2 changes: 2 additions & 0 deletions cmd/germanium/testdata/only-editor.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
../../../germanium --no-line-number --no-window-access-bar main.go -o only-editor-gen.png
4 changes: 2 additions & 2 deletions font.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package germanium

import (
_ "embed"
Expand All @@ -20,7 +20,7 @@ var (

func LoadFont(font string) (font.Face, error) {
fontData := font_hack
if opts.Font != "Hack-Regular" {
if font != "Hack-Regular" {
fontPath, err := findfont.Find(font + ".ttf")
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion formatter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package germanium

import (
"fmt"
Expand Down
1 change: 1 addition & 0 deletions germanium.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package germanium
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ require (
github.com/alecthomas/chroma v0.8.2
github.com/flopp/go-findfont v0.0.0-20201114153133-e7393a00c15b
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/jessevdk/go-flags v1.4.0
github.com/jessevdk/go-flags v1.5.0
github.com/sergi/go-diff v1.2.0 // indirect
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb
golang.org/x/sys v0.0.0-20210412220455-f1c623a9e750 // indirect
)
19 changes: 15 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,34 @@ github.com/flopp/go-findfont v0.0.0-20201114153133-e7393a00c15b h1:/wqXgpZNTP8qV
github.com/flopp/go-findfont v0.0.0-20201114153133-e7393a00c15b/go.mod h1:wKKxRDjD024Rh7VMwoU90i6ikQRCr+JTHB5n4Ejkqvw=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb h1:fqpd0EBDzlHRCjiphRR5Zo/RSWWQlWv34418dnEixWk=
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4 h1:opSr2sbRXk5X5/givKrrKj9HXxFpW2sdCiP8MJSKLQY=
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210412220455-f1c623a9e750 h1:ZBu6861dZq7xBnG1bn5SRU0vA8nx42at4+kP07FMTog=
golang.org/x/sys v0.0.0-20210412220455-f1c623a9e750/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Loading

0 comments on commit 4352809

Please sign in to comment.