Skip to content

Commit

Permalink
feat(prism): add header
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMarble committed May 4, 2022
1 parent ef7c7f4 commit 7a9ffd7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
coverage.txt
/*.ttf
.task
prism
8 changes: 6 additions & 2 deletions cmd/prism/prism.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ var CLI struct {
File string `arg:"" type:"existingfile" help:"File with code"`
Language string `name:"lang" short:"l" help:"Language to parse." required:""`
Output string `name:"output" short:"o" help:"output image" type:"path"`
Numbers bool `short:"n" help:"display line numbers"`

Numbers bool `short:"n" help:"display line numbers"`
Header bool `help:"display header"`

Debug bool `help:"Debug logging"`
}
Expand Down Expand Up @@ -62,7 +64,9 @@ func run(ctx *kong.Context) error {
if CLI.Numbers {
options.LineNumbers = true
}

if CLI.Header {
options.Header = true
}
if CLI.Output != "" {
err := pr.SavePNG(string(code), CLI.Output, options)
if err != nil {
Expand Down
26 changes: 21 additions & 5 deletions prism.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Context struct {

type Options struct {
LineNumbers bool
Header bool
}

//go:embed fonts/FiraCode-Regular.ttf
Expand Down Expand Up @@ -78,14 +79,20 @@ func (ctx *Context) calculate(code string, options Options) (width, height int,
horizontalMargin += ctx.measureString(fmt.Sprint(ctx.lines)) + (horizontalMargin / 2)
}

return int(codeWidth) + ctx.margin*2, int(codeHeight + verticalMargin*2), horizontalMargin, verticalMargin
if options.Header {
verticalMargin += 70
}

width = int(codeWidth) + ctx.margin + int(horizontalMargin)
height = int(codeHeight) + ctx.margin + int(verticalMargin)
return width, height, horizontalMargin, verticalMargin
}

func (ctx *Context) parse(code string, options Options) *gg.Context {
code = removeAccents(code)
ctx.lines = strings.Count(code, "\n")

width, height, hMargin, _ := ctx.calculate(code, options)
width, height, hMargin, vMargin := ctx.calculate(code, options)

// gg
dc := gg.NewContext(width, height)
Expand All @@ -99,11 +106,20 @@ func (ctx *Context) parse(code string, options Options) *gg.Context {
// font
dc.SetFontFace(ctx.font)

if options.Header {
colors := [3]string{"#ff5f58", "#ffbd2e", "#18c132"}
for i, color := range colors {
dc.DrawCircle(float64(ctx.margin*(i+1)), float64(ctx.margin), 15)
dc.SetHexColor(color)
dc.Fill()
}
}

if options.LineNumbers {
pad := int(math.Log10(float64(ctx.lines)) + 1)
for i := 0; i < ctx.lines; i++ {
dc.SetHexColor(ctx.theme[tokenizer.COMMENT])
dc.DrawString(fmt.Sprintf("%*d", pad, i+1), float64(ctx.margin), float64(ctx.margin)+ctx.points+((ctx.points*ctx.lineSpacing)*float64(i)))
dc.DrawString(fmt.Sprintf("%*d", pad, i+1), float64(ctx.margin), vMargin+ctx.points+((ctx.points*ctx.lineSpacing)*float64(i)))
}
}

Expand All @@ -114,8 +130,8 @@ func (ctx *Context) parse(code string, options Options) *gg.Context {
for _, token := range tokens {
color := ctx.theme.GetColor(token)
dc.SetHexColor(color)
x := float64(hMargin) + float64(token.Col)*runeWidth
y := float64(ctx.margin) + ctx.points + ((ctx.points * ctx.lineSpacing) * float64(token.Line))
x := hMargin + float64(token.Col)*runeWidth
y := vMargin + ctx.points + ((ctx.points * ctx.lineSpacing) * float64(token.Line))
dc.DrawString(token.Content, x, y)
}
return dc
Expand Down

0 comments on commit 7a9ffd7

Please sign in to comment.