-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from matsuyoshi30/change-package-as-a-library
Change package as a library
- Loading branch information
Showing
30 changed files
with
260 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package germanium | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package germanium |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.