Skip to content

Commit

Permalink
refactor: BoxSize to common type XyDim
Browse files Browse the repository at this point in the history
  • Loading branch information
patrislav1 committed Mar 2, 2024
1 parent b0c4ed3 commit a03e37d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 56 deletions.
20 changes: 9 additions & 11 deletions ansitosvg/ansisvg.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/wader/ansisvg/ansidecoder"
"github.com/wader/ansisvg/colorscheme/schemes"
"github.com/wader/ansisvg/svgscreen"
"github.com/wader/ansisvg/svgscreen/xydim"
)

type Options struct {
Expand All @@ -15,7 +16,7 @@ type Options struct {
FontRef string
FontSize int
TerminalWidth int
CharBoxSize svgscreen.BoxSize
CharBoxSize xydim.XyDimInt
ColorScheme string
Transparent bool
GridMode bool
Expand All @@ -24,7 +25,7 @@ type Options struct {
var DefaultOptions = Options{
FontName: "Courier",
FontSize: 14,
CharBoxSize: svgscreen.BoxSize{Width: 0, Height: 0},
CharBoxSize: xydim.XyDimInt{X: 0, Y: 0},
ColorScheme: "Builtin Dark",
Transparent: false,
}
Expand Down Expand Up @@ -129,15 +130,12 @@ func Convert(r io.Reader, w io.Writer, opts Options) error {
FontRef: opts.FontRef,
FontSize: opts.FontSize,
},
CharacterBoxSize: svgscreen.BoxSize{
Width: opts.CharBoxSize.Width,
Height: opts.CharBoxSize.Height,
},
TerminalWidth: terminalWidth,
Columns: ad.MaxX + 1,
NrLines: ad.MaxY + 1,
Lines: lines,
GridMode: opts.GridMode,
CharacterBoxSize: opts.CharBoxSize,
TerminalWidth: terminalWidth,
Columns: ad.MaxX + 1,
NrLines: ad.MaxY + 1,
Lines: lines,
GridMode: opts.GridMode,
}
return s.Render(w)
}
37 changes: 5 additions & 32 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,12 @@ import (
"flag"
"fmt"
"io"
"strconv"
"strings"

"github.com/wader/ansisvg/ansitosvg"
"github.com/wader/ansisvg/colorscheme/schemes"
"github.com/wader/ansisvg/svgscreen"
)

type boxSize struct {
Width int
Height int
}

func (d *boxSize) String() string {
return fmt.Sprintf("%dx%d", d.Width, d.Height)
}

func (d *boxSize) Set(s string) error {
parts := strings.Split(s, "x")
if len(parts) != 2 {
return fmt.Errorf("must be WxH")
}
d.Width, _ = strconv.Atoi(parts[0])
d.Height, _ = strconv.Atoi(parts[1])
return nil
}

type Env struct {
Version string
ReadFile func(string) ([]byte, error)
Expand Down Expand Up @@ -59,10 +38,7 @@ func Main(env Env) error {
var helpFlag bool
fs.BoolVar(&helpFlag, "h", false, "")
fs.BoolVar(&helpFlag, "help", false, "Show help")
var charBoxSize = boxSize{
Width: ansitosvg.DefaultOptions.CharBoxSize.Width,
Height: ansitosvg.DefaultOptions.CharBoxSize.Height,
}
charBoxSize := ansitosvg.DefaultOptions.CharBoxSize
fs.Var(&charBoxSize, "charboxsize", "WxH|Character box size (use pixel units instead of font units)")
// handle error and usage output ourself
fs.Usage = func() {}
Expand Down Expand Up @@ -159,13 +135,10 @@ Example usage:
FontRef: *fontRefFlag,
FontSize: *fontSizeFlag,
TerminalWidth: terminalWidthFlag,
CharBoxSize: svgscreen.BoxSize{
Width: charBoxSize.Width,
Height: charBoxSize.Height,
},
ColorScheme: *colorSchemeFlag,
Transparent: *transparentFlag,
GridMode: *gridModeFlag,
CharBoxSize: charBoxSize,
ColorScheme: *colorSchemeFlag,
Transparent: *transparentFlag,
GridMode: *gridModeFlag,
},
)
}
22 changes: 9 additions & 13 deletions svgscreen/svgscreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
_ "embed"
"encoding/base64"
"fmt"
"github.com/wader/ansisvg/svgscreen/xydim"
"html/template"
"io"
"strconv"
Expand All @@ -31,11 +32,6 @@ type Line struct {
Chars []Char
}

type BoxSize struct {
Width int
Height int
}

type textSpan struct {
X string
Class string
Expand Down Expand Up @@ -89,7 +85,7 @@ type Screen struct {
Background ColorMap
ANSIColors [16]string

CharacterBoxSize BoxSize
CharacterBoxSize xydim.XyDimInt
TerminalWidth int
Columns int
NrLines int
Expand All @@ -100,18 +96,18 @@ type Screen struct {

func (s *Screen) columnCoordinate(col float32) string {
unit := "ch"
if s.CharacterBoxSize.Width > 0 {
if s.CharacterBoxSize.X > 0 {
unit = "px"
col *= float32(s.CharacterBoxSize.Width)
col *= float32(s.CharacterBoxSize.X)
}
return fmt.Sprintf("%g%s", col, unit)
}

func (s *Screen) rowCoordinate(row float32) string {
unit := "em"
if s.CharacterBoxSize.Height > 0 {
if s.CharacterBoxSize.Y > 0 {
unit = "px"
row *= float32(s.CharacterBoxSize.Height)
row *= float32(s.CharacterBoxSize.Y)
}
return fmt.Sprintf("%g%s", row, unit)
}
Expand Down Expand Up @@ -304,7 +300,7 @@ func (s *Screen) Render(w io.Writer) error {
marginY := float32(5.0)

// Set SVG size
if s.CharacterBoxSize.Width == 0 {
if s.CharacterBoxSize.X == 0 {
// Font-relative coordinates
s.Dom.Width = s.columnCoordinate(float32(s.TerminalWidth) + 2*marginX)
s.Dom.Height = s.rowCoordinate(float32(s.NrLines) + 2*marginY)
Expand All @@ -314,8 +310,8 @@ func (s *Screen) Render(w io.Writer) error {
}
} else {
// Pixel coordinates
s.Dom.Width = fmt.Sprintf("%gpx", float32(s.CharacterBoxSize.Width*s.TerminalWidth)+2*marginX)
s.Dom.Height = fmt.Sprintf("%gpx", float32(s.CharacterBoxSize.Height*s.NrLines)+2*marginY)
s.Dom.Width = fmt.Sprintf("%gpx", float32(s.CharacterBoxSize.X*s.TerminalWidth)+2*marginX)
s.Dom.Height = fmt.Sprintf("%gpx", float32(s.CharacterBoxSize.Y*s.NrLines)+2*marginY)
if marginX > 0 || marginY > 0 {
s.Dom.MarginX = fmt.Sprintf("%gpx", marginX)
s.Dom.MarginY = fmt.Sprintf("%gpx", marginY)
Expand Down
59 changes: 59 additions & 0 deletions svgscreen/xydim/xydim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package xydim

import (
"fmt"
"strconv"
"strings"
)

type XyDimInt struct {
X int
Y int
}

func (d *XyDimInt) String() string {
return fmt.Sprintf("%dx%d", d.X, d.Y)
}

func (d *XyDimInt) Set(s string) error {
parts := strings.Split(s, "x")
if len(parts) != 2 {
return fmt.Errorf("must be WxH")
}

var err1, err2 error
d.X, err1 = strconv.Atoi(parts[0])
d.Y, err2 = strconv.Atoi(parts[1])

if err1 != nil || err2 != nil {
return fmt.Errorf("int conversion error")
}
return nil
}

type XyDimFloat struct {
X float32
Y float32
}

func (d *XyDimFloat) String() string {
return fmt.Sprintf("%gx%g", d.X, d.Y)
}

func (d *XyDimFloat) Set(s string) error {
parts := strings.Split(s, "x")
if len(parts) != 2 {
return fmt.Errorf("must be WxH")
}

x, err1 := strconv.ParseFloat(parts[0], 32)
y, err2 := strconv.ParseFloat(parts[1], 32)

if err1 != nil || err2 != nil {
return fmt.Errorf("float conversion error")
}

d.X = float32(x)
d.Y = float32(y)
return nil
}

0 comments on commit a03e37d

Please sign in to comment.