Skip to content

Commit 62b7ee8

Browse files
committed
Add Pi CLI
First supported command is "convert". It converts files from one format into another. With this feature you can convert pico-8 file into Pi formats: sprite-sheet.png and audio.sfx
1 parent 0be3e14 commit 62b7ee8

File tree

11 files changed

+516
-0
lines changed

11 files changed

+516
-0
lines changed

go.work

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
go 1.20
2+
3+
use (
4+
.
5+
./picli
6+
)

go.work.sum

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
2+
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
3+
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802 h1:1BDTz0u9nC3//pOCMdNH+CiXJVYJh5UQNCOBG7jbELc=
4+
github.com/hajimehoshi/bitmapfont/v2 v2.2.3 h1:jmq/TMNj352V062Tr5e3hAoipkoxCbY1JWTzor0zNps=
5+
github.com/hajimehoshi/bitmapfont/v2 v2.2.3/go.mod h1:sWM8ejdkGSXaQGlZcegMRx4DyEPOWYyXqsBKIs+Yhzk=
6+
github.com/hajimehoshi/go-mp3 v0.3.4 h1:NUP7pBYH8OguP4diaTZ9wJbUbk3tC0KlfzsEpWmYj68=
7+
github.com/hajimehoshi/go-mp3 v0.3.4/go.mod h1:fRtZraRFcWb0pu7ok0LqyFhCUrPeMsGRSVop0eemFmo=
8+
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6 h1:8UsGZ2rr2ksmEru6lToqnXgA8Mz1DP11X4zSJ159C3k=
9+
github.com/jakecoffman/cp v1.2.1 h1:zkhc2Gpo9l4NLUZfeG3j33+3bQD7MkqPa+n5PdX+5mI=
10+
github.com/jakecoffman/cp v1.2.1/go.mod h1:JjY/Fp6d8E1CHnu74gWNnU0+b9VzEdUVPoJxg2PsTQg=
11+
github.com/jfreymuth/oggvorbis v1.0.5 h1:u+Ck+R0eLSRhgq8WTmffYnrVtSztJcYrl588DM4e3kQ=
12+
github.com/jfreymuth/oggvorbis v1.0.5/go.mod h1:1U4pqWmghcoVsCJJ4fRBKv9peUJMBHixthRlBeD6uII=
13+
github.com/jfreymuth/vorbis v1.0.2 h1:m1xH6+ZI4thH927pgKD8JOH4eaGRm18rEE9/0WKjvNE=
14+
github.com/jfreymuth/vorbis v1.0.2/go.mod h1:DoftRo4AznKnShRl1GxiTFCseHr4zR9BN3TWXyuzrqQ=
15+
github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw=
16+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
17+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
18+
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
19+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
20+
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
21+
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
22+
golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q=
23+
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
24+
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
25+
golang.org/x/tools v0.12.1-0.20230818130535-1517d1a3ba60 h1:o4bs4seAAlSiZQAZbO6/RP5XBCZCooQS3Pgc0AUjWts=
26+
golang.org/x/tools v0.12.1-0.20230818130535-1517d1a3ba60/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
27+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
28+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

picli/cli.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// (c) 2022-2023 Jacek Olszak
2+
// This code is licensed under MIT license (see LICENSE for details)
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"os"
9+
10+
"github.com/urfave/cli/v2"
11+
12+
"github.com/elgopher/pi/picli/internal/convert"
13+
)
14+
15+
func main() {
16+
app := cli.App{
17+
Usage: "Pi Command Line Interface",
18+
Commands: []*cli.Command{
19+
convertCmd(),
20+
},
21+
}
22+
err := app.Run(os.Args)
23+
if err != nil {
24+
_, _ = os.Stderr.WriteString(err.Error())
25+
}
26+
}
27+
28+
func convertCmd() *cli.Command {
29+
return &cli.Command{
30+
Name: "convert",
31+
Usage: "Converts one file format into another one",
32+
Description: "Format of input and output file is deducted based on files extension.",
33+
Flags: []cli.Flag{
34+
&cli.StringFlag{
35+
Name: "format",
36+
Aliases: []string{"f"},
37+
Usage: "Format of input file. Overrides what CLI deducted based on input file extension. For now, the only supported input format is p8.",
38+
},
39+
},
40+
ArgsUsage: "input.file output.file",
41+
Action: func(context *cli.Context) error {
42+
inputFile := context.Args().Get(0)
43+
outputFile := context.Args().Get(1)
44+
45+
if context.Args().Len() > 2 {
46+
return fmt.Errorf("too many arguments")
47+
}
48+
49+
command := convert.Command{
50+
InputFormat: convert.InputFormat(context.String("format")),
51+
InputFile: inputFile,
52+
OutputFile: outputFile,
53+
}
54+
return command.Run()
55+
},
56+
}
57+
}

picli/go.mod

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/elgopher/pi/picli
2+
3+
go 1.20
4+
5+
require github.com/urfave/cli/v2 v2.25.7
6+
7+
require (
8+
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
9+
github.com/icza/bitio v1.1.0 // indirect
10+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
11+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
12+
)

picli/go.sum

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
2+
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
3+
github.com/icza/bitio v1.1.0 h1:ysX4vtldjdi3Ygai5m1cWy4oLkhWTAi+SyO6HC8L9T0=
4+
github.com/icza/bitio v1.1.0/go.mod h1:0jGnlLAx8MKMr9VGnn/4YrvZiprkvBelsVIbA9Jjr9A=
5+
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6/go.mod h1:xQig96I1VNBDIWGCdTt54nHt6EeI639SmHycLYL7FkA=
6+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
7+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
8+
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
9+
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
10+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
11+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=

picli/internal/convert/convert.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// (c) 2022-2023 Jacek Olszak
2+
// This code is licensed under MIT license (see LICENSE for details)
3+
4+
package convert
5+
6+
import (
7+
"fmt"
8+
"strings"
9+
10+
"github.com/elgopher/pi/picli/internal/convert/internal/p8"
11+
)
12+
13+
type InputFormat string
14+
15+
const (
16+
InputFormatP8 = "p8"
17+
)
18+
19+
type Command struct {
20+
InputFormat
21+
InputFile string
22+
OutputFile string
23+
}
24+
25+
func (o Command) Run() error {
26+
if o.InputFile == "" {
27+
return fmt.Errorf("input file not provided")
28+
}
29+
30+
if o.InputFormat != "" && o.InputFormat != InputFormatP8 {
31+
return fmt.Errorf("input format %s not supported", o.InputFormat)
32+
}
33+
34+
if o.InputFormat == "" {
35+
if strings.HasSuffix(o.InputFile, ".p8") {
36+
o.InputFormat = InputFormatP8
37+
} else {
38+
return fmt.Errorf("cannot deduct the format of %s input file", o.InputFile)
39+
}
40+
}
41+
42+
if o.OutputFile == "" {
43+
return fmt.Errorf("output file not provided")
44+
}
45+
46+
fmt.Printf("Converting %s to %s... ", o.InputFile, o.OutputFile)
47+
fmt.Printf("Using %s input format... ", o.InputFormat)
48+
if err := o.convert(); err != nil {
49+
return err
50+
}
51+
fmt.Println("Done")
52+
return nil
53+
}
54+
55+
func (o Command) convert() error {
56+
if o.InputFormat == InputFormatP8 {
57+
if err := p8.ConvertToAudioSfx(o.InputFile, o.OutputFile); err != nil {
58+
return err
59+
}
60+
}
61+
62+
return nil
63+
}

0 commit comments

Comments
 (0)