Skip to content

Commit

Permalink
Add Pi CLI
Browse files Browse the repository at this point in the history
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
  • Loading branch information
elgopher committed Sep 28, 2023
1 parent 7141243 commit 5c7b749
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 0 deletions.
57 changes: 57 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// (c) 2022-2023 Jacek Olszak
// This code is licensed under MIT license (see LICENSE for details)

package main

import (
"fmt"
"os"

"github.com/urfave/cli/v2"

"github.com/elgopher/pi/cli/internal/convert"
)

func main() {
app := cli.App{
Usage: "Pi Command Line Interface",
Commands: []*cli.Command{
convertCmd(),
},
}
err := app.Run(os.Args)
if err != nil {
_, _ = os.Stderr.WriteString(err.Error())
}
}

func convertCmd() *cli.Command {
return &cli.Command{
Name: "convert",
Usage: "Converts one file format into another one",
Description: "Format of input and output file is deducted based on files extension.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Aliases: []string{"f"},
Usage: "Format of input file. Overrides what CLI deducted based on input file extension. For now, the only supported input format is p8.",
},
},
ArgsUsage: "input.file output.file",
Action: func(context *cli.Context) error {
inputFile := context.Args().Get(0)
outputFile := context.Args().Get(1)

if context.Args().Len() > 2 {
return fmt.Errorf("too many arguments")
}

command := convert.Command{
InputFormat: convert.InputFormat(context.String("format")),
InputFile: inputFile,
OutputFile: outputFile,
}
return command.Run()
},
}
}
11 changes: 11 additions & 0 deletions cli/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/elgopher/pi/cli

go 1.20

require github.com/urfave/cli/v2 v2.25.7

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
)
8 changes: 8 additions & 0 deletions cli/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
63 changes: 63 additions & 0 deletions cli/internal/convert/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// (c) 2022-2023 Jacek Olszak
// This code is licensed under MIT license (see LICENSE for details)

package convert

import (
"fmt"
"strings"

"github.com/elgopher/pi/cli/internal/convert/internal/p8"
)

type InputFormat string

const (
InputFormatP8 = "p8"
)

type Command struct {
InputFormat
InputFile string
OutputFile string
}

func (o Command) Run() error {
if o.InputFormat != "" && o.InputFormat != InputFormatP8 {
return fmt.Errorf("input format %s not supported", o.InputFormat)
}

if o.InputFormat == "" {
if strings.HasSuffix(o.InputFile, ".p8") {
o.InputFormat = InputFormatP8
} else {
return fmt.Errorf("cannot deduct the format of %s input file", o.InputFile)
}
}

if o.InputFile == "" {
return fmt.Errorf("input file not provided")
}

if o.OutputFile == "" {
return fmt.Errorf("output file not provided")
}

fmt.Printf("Converting %s to %s... ", o.InputFile, o.OutputFile)
fmt.Printf("Using %s input format... ", o.InputFormat)
if err := o.convert(); err != nil {
return err
}
fmt.Println("Done")
return nil
}

func (o Command) convert() error {
if o.InputFormat == InputFormatP8 {
if err := p8.ConvertToAudioSfx(o.InputFile, o.OutputFile); err != nil {
return err
}
}

return nil
}
55 changes: 55 additions & 0 deletions cli/internal/convert/internal/p8/p8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package p8

import (
"fmt"
"os"

"github.com/elgopher/pi/audio"
)

func ConvertToAudioSfx(inputFile, outputFile string) error {
p8file, err := load(inputFile)
if err != nil {
return fmt.Errorf("loading p8 file failed: %w", err)
}

effects, err := p8file.SoundEffects()
if err != nil {
return fmt.Errorf("loading sound effects failed: %w", err)
}
audio.Sfx = effects

patterns, err := p8file.MusicPatterns()
if err != nil {
return fmt.Errorf("loading music patterns failed: %w", err)
}
audio.Pat = patterns

bytes, err := audio.Save()
if err != nil {
return fmt.Errorf("saving audio failed: %w", err)
}

err = os.WriteFile(outputFile, bytes, 0644)
if err != nil {
return fmt.Errorf("writing %s failed: %w", outputFile, err)
}

return nil
}

func load(filename string) (file, error) {
return file{}, nil
}

type file struct {
sfx []byte
}

func (p file) SoundEffects() ([64]audio.SoundEffect, error) {
return [64]audio.SoundEffect{}, nil
}

func (p file) MusicPatterns() ([64]audio.Pattern, error) {
return [64]audio.Pattern{}, nil
}
13 changes: 13 additions & 0 deletions cli/internal/convert/internal/test/code.p8
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pico-8 cartridge // http://www.pico-8.com
version 41
__lua__
function _draw()
end
function _lua
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
9 changes: 9 additions & 0 deletions cli/internal/convert/internal/test/minimal.p8
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pico-8 cartridge // http://www.pico-8.com
version 41
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
15 changes: 15 additions & 0 deletions cli/internal/convert/internal/test/music.p8
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pico-8 cartridge // http://www.pico-8.com
version 41
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__sfx__
000100000000027050270502705027050260502605026050260502605026050260502605026050260502605026050260502605027050270502705027050280500000000000000000000000000000000000000000
00100000000000000000000000003505032050310502e0502d0502b0502a050290502805027050260502505024050240502305000000000000000000000000000000000000000000000000000000000000000000
__music__
00 01424344

11 changes: 11 additions & 0 deletions cli/internal/convert/internal/test/sfx.p8
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pico-8 cartridge // http://www.pico-8.com
version 41
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__sfx__
000100000000027050270502705027050260502605026050260502605026050260502605026050260502605026050260502605027050270502705027050280500000000000000000000000000000000000000000
6 changes: 6 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
go 1.20

use (
.
./cli
)
27 changes: 27 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802 h1:1BDTz0u9nC3//pOCMdNH+CiXJVYJh5UQNCOBG7jbELc=
github.com/hajimehoshi/bitmapfont/v2 v2.2.3 h1:jmq/TMNj352V062Tr5e3hAoipkoxCbY1JWTzor0zNps=
github.com/hajimehoshi/bitmapfont/v2 v2.2.3/go.mod h1:sWM8ejdkGSXaQGlZcegMRx4DyEPOWYyXqsBKIs+Yhzk=
github.com/hajimehoshi/go-mp3 v0.3.4 h1:NUP7pBYH8OguP4diaTZ9wJbUbk3tC0KlfzsEpWmYj68=
github.com/hajimehoshi/go-mp3 v0.3.4/go.mod h1:fRtZraRFcWb0pu7ok0LqyFhCUrPeMsGRSVop0eemFmo=
github.com/jakecoffman/cp v1.2.1 h1:zkhc2Gpo9l4NLUZfeG3j33+3bQD7MkqPa+n5PdX+5mI=
github.com/jakecoffman/cp v1.2.1/go.mod h1:JjY/Fp6d8E1CHnu74gWNnU0+b9VzEdUVPoJxg2PsTQg=
github.com/jfreymuth/oggvorbis v1.0.5 h1:u+Ck+R0eLSRhgq8WTmffYnrVtSztJcYrl588DM4e3kQ=
github.com/jfreymuth/oggvorbis v1.0.5/go.mod h1:1U4pqWmghcoVsCJJ4fRBKv9peUJMBHixthRlBeD6uII=
github.com/jfreymuth/vorbis v1.0.2 h1:m1xH6+ZI4thH927pgKD8JOH4eaGRm18rEE9/0WKjvNE=
github.com/jfreymuth/vorbis v1.0.2/go.mod h1:DoftRo4AznKnShRl1GxiTFCseHr4zR9BN3TWXyuzrqQ=
github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/tools v0.12.1-0.20230818130535-1517d1a3ba60 h1:o4bs4seAAlSiZQAZbO6/RP5XBCZCooQS3Pgc0AUjWts=
golang.org/x/tools v0.12.1-0.20230818130535-1517d1a3ba60/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

0 comments on commit 5c7b749

Please sign in to comment.