Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use getopt style short/long argument flags (without using getopt package) #34

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 18 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,24 @@ Produces [colortest.ansi.svg](cli/testdata/colortest.ansi.svg):

```
$ ansisvg -h
Usage of ansisvg:
-charboxsize value
Character box size (forces pixel units instead of font-relative units)
-colorscheme string
Color scheme (default "Builtin Dark")
-fontfile string
Font file to use and embed
-fontname string
Font name (default "Courier")
-fontref string
External font file to reference
-fontsize int
Font size (default 14)
-grid
Enable grid mode (sets position for each character)
-listcolorschemes
List color schemes
-transparent
Transparent background
-width int
Terminal width (auto)
ansisvg - Convert ANSI to SVG
Usage: ansisvg [FLAGS]

Example usage:
program | ansisvg > file.svg

--charboxsize Character box size (use pixel units instead of font units)
--colorscheme Color scheme
--fontfile Font file to use and embed
--fontname Font name
--fontref External font URL to use
--fontsize Font size
--grid Grid mode (sets position for each character)
--help, -h Show help
--listcolorschemes List color schemes
--transparent Transparent background
--version, -v Show version
--width, -w Terminal width (auto if not set)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I shorted some texts to stay within 80 characters. Otherwise help text look good? should include something more?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

```

Color themes are the ones from https://github.com/mbadolato/iTerm2-Color-Schemes
Expand Down
32 changes: 16 additions & 16 deletions ansitosvg/ansisvg.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import (
)

type Options struct {
FontName string
FontEmbedded []byte
FontRef string
FontSize int
TerminalWidth int
CharacterBoxSize svgscreen.BoxSize
ColorScheme string
Transparent bool
GridMode bool
FontName string
FontEmbedded []byte
FontRef string
FontSize int
TerminalWidth int
CharBoxSize svgscreen.BoxSize
ColorScheme string
Transparent bool
GridMode bool
}

var DefaultOptions = Options{
FontName: "Courier",
FontSize: 14,
CharacterBoxSize: svgscreen.BoxSize{Width: 0, Height: 0},
ColorScheme: "Builtin Dark",
Transparent: false,
FontName: "Courier",
FontSize: 14,
CharBoxSize: svgscreen.BoxSize{Width: 0, Height: 0},
ColorScheme: "Builtin Dark",
Transparent: false,
}

// Convert reads ANSI input from r and writes SVG to w
Expand Down Expand Up @@ -130,8 +130,8 @@ func Convert(r io.Reader, w io.Writer, opts Options) error {
FontSize: opts.FontSize,
},
CharacterBoxSize: svgscreen.BoxSize{
Width: opts.CharacterBoxSize.Width,
Height: opts.CharacterBoxSize.Height,
Width: opts.CharBoxSize.Width,
Height: opts.CharBoxSize.Height,
},
TerminalWidth: terminalWidth,
Columns: ad.MaxX + 1,
Expand Down
82 changes: 67 additions & 15 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,77 @@ type Env struct {
}

func Main(env Env) error {
fs := flag.NewFlagSet("ansisvg", flag.ExitOnError)
var versionFlag = fs.Bool("version", false, "Show version")
fs := flag.NewFlagSet("ansisvg", flag.ContinueOnError)
var versionFlag bool
fs.BoolVar(&versionFlag, "v", false, "")
fs.BoolVar(&versionFlag, "version", false, "Show version")
var fontNameFlag = fs.String("fontname", ansitosvg.DefaultOptions.FontName, "Font name")
var fontFileFlag = fs.String("fontfile", "", "Font file to use and embed")
var fontRefFlag = fs.String("fontref", "", "External font file to reference")
var fontRefFlag = fs.String("fontref", "", "External font URL to use")
var fontSizeFlag = fs.Int("fontsize", ansitosvg.DefaultOptions.FontSize, "Font size")
var terminalWidthFlag = fs.Int("width", 0, "Terminal width (auto)")
var terminalWidthFlag int
fs.IntVar(&terminalWidthFlag, "w", 0, "")
fs.IntVar(&terminalWidthFlag, "width", 0, "Terminal width (auto if not set)")
var colorSchemeFlag = fs.String("colorscheme", ansitosvg.DefaultOptions.ColorScheme, "Color scheme")
var listColorSchemesFlag = fs.Bool("listcolorschemes", false, "List color schemes")
var transparentFlag = fs.Bool("transparent", ansitosvg.DefaultOptions.Transparent, "Transparent background")
var gridModeFlag = fs.Bool("grid", false, "Enable grid mode (sets position for each character)")
var characterBoxSize = boxSize{
Width: ansitosvg.DefaultOptions.CharacterBoxSize.Width,
Height: ansitosvg.DefaultOptions.CharacterBoxSize.Height,
var gridModeFlag = fs.Bool("grid", false, "Grid mode (sets position for each character)")
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,
}
fs.Var(&charBoxSize, "charboxsize", "Character box size (use pixel units instead of font units)")
// handle error and usage output ourself
fs.Usage = func() {}
fs.SetOutput(io.Discard)
longToShort := map[string]string{
"help": "h",
"version": "v",
"width": "w",
}
usage := func() {
maxNameLen := 0
fs.VisitAll(func(f *flag.Flag) {
if len(f.Name) > maxNameLen {
maxNameLen = len(f.Name)
}
})

fmt.Fprintf(env.Stdout, `
%[1]s - Convert ANSI to SVG
Usage: %[1]s [FLAGS]

Example usage:
program | %[1]s > file.svg

`[1:], fs.Name())
fs.VisitAll(func(f *flag.Flag) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should maybe comment that is a ugly hack to skip the short flags

if len(f.Name) == 1 {
return
}

short := ""
if s, ok := longToShort[f.Name]; ok {
short = ", -" + s
}

flagNames := f.Name + short
pad := strings.Repeat(" ", maxNameLen-len(flagNames))
fmt.Fprintf(env.Stdout, "--%s%s%s %s\n", f.Name, short, pad, f.Usage)
})
}
if err := fs.Parse(env.Args[1:]); err != nil {
return err
}
if helpFlag {
usage()
return nil
}
fs.Var(&characterBoxSize, "charboxsize", "Character box size (forces pixel units instead of font-relative units)")
_ = fs.Parse(env.Args[1:])

if *versionFlag {
if versionFlag {
fmt.Fprintln(env.Stdout, env.Version)
return nil
}
Expand Down Expand Up @@ -96,10 +148,10 @@ func Main(env Env) error {
FontEmbedded: fontEmbedded,
FontRef: *fontRefFlag,
FontSize: *fontSizeFlag,
TerminalWidth: *terminalWidthFlag,
CharacterBoxSize: svgscreen.BoxSize{
Width: characterBoxSize.Width,
Height: characterBoxSize.Height,
TerminalWidth: terminalWidthFlag,
CharBoxSize: svgscreen.BoxSize{
Width: charBoxSize.Width,
Height: charBoxSize.Height,
},
ColorScheme: *colorSchemeFlag,
Transparent: *transparentFlag,
Expand Down
2 changes: 1 addition & 1 deletion cli/testdata/charboxfontsize.ansi.args
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-fontsize 40 -fontname Arial -charboxsize 50x50 -grid
--fontsize 40 --fontname Arial --charboxsize 50x50 --grid
2 changes: 1 addition & 1 deletion cli/testdata/colortest_slate.ansi.args
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-colorscheme Slate
--colorscheme Slate
2 changes: 1 addition & 1 deletion cli/testdata/colortest_transparent.ansi.args
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-transparent
--transparent
2 changes: 1 addition & 1 deletion cli/testdata/fontembedded.ansi.args
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-fontfile UbuntuMonoNerdFontMono-Regular.woff2
--fontfile UbuntuMonoNerdFontMono-Regular.woff2
2 changes: 1 addition & 1 deletion cli/testdata/fontname.ansi.args
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-fontname Monaco
--fontname Monaco
2 changes: 1 addition & 1 deletion cli/testdata/fontref.ansi.args
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-fontref font.woff2
--fontref font.woff2
2 changes: 1 addition & 1 deletion cli/testdata/powerline.ansi.args
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-fontfile UbuntuMonoNerdFontMono-Regular.woff2 -grid
--fontfile UbuntuMonoNerdFontMono-Regular.woff2 --grid
2 changes: 1 addition & 1 deletion cli/testdata/terminalwidth4.ansi.args
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-width 4
--width 4
2 changes: 1 addition & 1 deletion cli/testdata/underlinetabs_backgroundcolor.ansi.args
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-colorscheme "Builtin Solarized Light"
--colorscheme "Builtin Solarized Light"