Skip to content

Commit

Permalink
add: --marginsize argument
Browse files Browse the repository at this point in the history
  • Loading branch information
patrislav1 committed Mar 2, 2024
1 parent a03e37d commit 2571373
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
3 changes: 3 additions & 0 deletions ansitosvg/ansisvg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Options struct {
FontSize int
TerminalWidth int
CharBoxSize xydim.XyDimInt
MarginSize xydim.XyDimFloat
ColorScheme string
Transparent bool
GridMode bool
Expand All @@ -26,6 +27,7 @@ var DefaultOptions = Options{
FontName: "Courier",
FontSize: 14,
CharBoxSize: xydim.XyDimInt{X: 0, Y: 0},
MarginSize: xydim.XyDimFloat{X: 0, Y: 0},
ColorScheme: "Builtin Dark",
Transparent: false,
}
Expand Down Expand Up @@ -131,6 +133,7 @@ func Convert(r io.Reader, w io.Writer, opts Options) error {
FontSize: opts.FontSize,
},
CharacterBoxSize: opts.CharBoxSize,
MarginSize: opts.MarginSize,
TerminalWidth: terminalWidth,
Columns: ad.MaxX + 1,
NrLines: ad.MaxY + 1,
Expand Down
3 changes: 3 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func Main(env Env) error {
fs.BoolVar(&helpFlag, "help", false, "Show help")
charBoxSize := ansitosvg.DefaultOptions.CharBoxSize
fs.Var(&charBoxSize, "charboxsize", "WxH|Character box size (use pixel units instead of font units)")
marginSize := ansitosvg.DefaultOptions.MarginSize
fs.Var(&marginSize, "marginsize", "WxH|Margin size (in either pixel or font units)")
// handle error and usage output ourself
fs.Usage = func() {}
fs.SetOutput(io.Discard)
Expand Down Expand Up @@ -136,6 +138,7 @@ Example usage:
FontSize: *fontSizeFlag,
TerminalWidth: terminalWidthFlag,
CharBoxSize: charBoxSize,
MarginSize: marginSize,
ColorScheme: *colorSchemeFlag,
Transparent: *transparentFlag,
GridMode: *gridModeFlag,
Expand Down
1 change: 1 addition & 0 deletions cli/testdata/help-short-flag.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Example usage:
--grid Grid mode (sets position for each character)
--help, -h Show help
--listcolorschemes List color schemes
--marginsize WxH Margin size (in either pixel or font units)
--transparent Transparent background
--version, -v Show version
--width, -w NUMBER Terminal width (auto if not set)
1 change: 1 addition & 0 deletions cli/testdata/help.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Example usage:
--grid Grid mode (sets position for each character)
--help, -h Show help
--listcolorschemes List color schemes
--marginsize WxH Margin size (in either pixel or font units)
--transparent Transparent background
--version, -v Show version
--width, -w NUMBER Terminal width (auto if not set)
24 changes: 11 additions & 13 deletions svgscreen/svgscreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type Screen struct {
ANSIColors [16]string

CharacterBoxSize xydim.XyDimInt
MarginSize xydim.XyDimFloat
TerminalWidth int
Columns int
NrLines int
Expand Down Expand Up @@ -296,25 +297,22 @@ func (s *Screen) Render(w io.Writer) error {
s.Foreground.Custom = map[string]int{}
s.Background.Custom = map[string]int{}

marginX := float32(5.0)
marginY := float32(5.0)

// Set SVG size
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)
if marginX > 0 || marginY > 0 {
s.Dom.MarginX = fmt.Sprintf("%gch", marginX)
s.Dom.MarginY = fmt.Sprintf("%gem", marginY)
s.Dom.Width = s.columnCoordinate(float32(s.TerminalWidth) + 2*s.MarginSize.X)
s.Dom.Height = s.rowCoordinate(float32(s.NrLines) + 2*s.MarginSize.Y)
if s.MarginSize.X > 0 || s.MarginSize.Y > 0 {
s.Dom.MarginX = fmt.Sprintf("%gch", s.MarginSize.X)
s.Dom.MarginY = fmt.Sprintf("%gem", s.MarginSize.Y)
}
} else {
// Pixel coordinates
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)
s.Dom.Width = fmt.Sprintf("%gpx", float32(s.CharacterBoxSize.X*s.TerminalWidth)+2*s.MarginSize.X)
s.Dom.Height = fmt.Sprintf("%gpx", float32(s.CharacterBoxSize.Y*s.NrLines)+2*s.MarginSize.Y)
if s.MarginSize.X > 0 || s.MarginSize.Y > 0 {
s.Dom.MarginX = fmt.Sprintf("%gpx", s.MarginSize.X)
s.Dom.MarginY = fmt.Sprintf("%gpx", s.MarginSize.Y)
}
}

Expand Down

0 comments on commit 2571373

Please sign in to comment.