Skip to content

Commit

Permalink
refactor(logs): use zerolog
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMarble committed Mar 8, 2022
1 parent c4068b5 commit 2b8e436
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 33 deletions.
17 changes: 9 additions & 8 deletions cmd/termsvg/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@ package export
import (
"os"

"github.com/fatih/color"
"github.com/mrmarble/termsvg/internal/svg"
"github.com/mrmarble/termsvg/pkg/asciicast"
"github.com/rs/zerolog/log"
)

type Cmd struct {
Input string `short:"i" type:"existingfile" help:"asciicast file to export"`
Output string `optional:"" short:"o" help:"where to save the file"`
Output string `optional:"" short:"o" type:"path" help:"where to save the file"`
}

func (cmd *Cmd) Run() error {
err := export(cmd.Input, cmd.Output)
output := cmd.Output
if output == "" {
output = cmd.Input + ".svg"
}

err := export(cmd.Input, output)
if err != nil {
return err
}

color.Green("Svg file saved to %s", cmd.Output)
log.Info().Str("output", output).Msg("svg file saved.")

return nil
}
Expand All @@ -30,10 +35,6 @@ func export(input, output string) error {
return err
}

if output == "" {
output = input + ".svg"
}

cast, err := asciicast.Unmarshal(inputFile)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions cmd/termsvg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"os"
"time"

"github.com/alecthomas/kong"
"github.com/mrmarble/termsvg/cmd/termsvg/export"
Expand All @@ -19,7 +18,7 @@ type Context struct {
func init() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)

log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339})
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, PartsExclude: []string{"time"}})
}

func main() {
Expand Down
19 changes: 9 additions & 10 deletions cmd/termsvg/rec/rec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/creack/pty"
"github.com/fatih/color"
"github.com/mrmarble/termsvg/pkg/asciicast"
"github.com/rs/zerolog/log"
"golang.org/x/term"
Expand All @@ -23,16 +22,16 @@ type Cmd struct {
const readSize = 1024

func (cmd *Cmd) Run() error {
color.Green("recording asciicast to %s", cmd.File)
color.Green("exit the opened program when you're done")
log.Info().Str("output", cmd.File).Msg("recording asciicast.")
log.Info().Msg("exit the opened program when you're done.")

err := rec(cmd.File, cmd.Command)
if err != nil {
return err
}

color.Green("recording finished")
color.Green("asciicast saved to %s", cmd.File)
log.Info().Msg("recording finished.")
log.Info().Str("output", cmd.File).Msg("asciicast saved.")

return nil
}
Expand Down Expand Up @@ -81,7 +80,7 @@ func run(command string) ([]asciicast.Event, error) {
// Make sure to close the pty at the end.
defer func() {
if err = ptmx.Close(); err != nil {
log.Printf("error closing pty: %s", err)
log.Fatal().Err(err).Msg("error closing pty")
}
}() // Best effort.

Expand All @@ -91,20 +90,20 @@ func run(command string) ([]asciicast.Event, error) {
// Set stdin in raw mode.
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
log.Fatal().Err(err).Msg("error setting stin in raw mode")
}

defer func() {
if err = term.Restore(int(os.Stdin.Fd()), oldState); err != nil {
log.Printf("error restoring terminal: %s", err)
log.Fatal().Err(err).Msg("error restoring terminal")
}
}() // Best effort.

// Copy stdin to the pty and the pty to stdout.
// NOTE: The goroutine will keep reading until the next keystroke before returning.
go func() {
if _, err = io.Copy(ptmx, os.Stdin); err != nil {
log.Printf("error reading stdin: %s", err)
log.Fatal().Err(err).Msg("error reading stdin")
}
}()

Expand Down Expand Up @@ -146,7 +145,7 @@ func handlePtySize(ptmx *os.File) chan os.Signal {
go func() {
for range ch {
if err := pty.InheritSize(os.Stdin, ptmx); err != nil {
log.Printf("error resizing pty: %s", err)
log.Fatal().Err(err).Msg("error resizing pty")
}
}
}()
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ require (
)

require (
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
)

require (
github.com/alecthomas/kong v0.4.1
github.com/fatih/color v1.13.0
github.com/google/go-cmp v0.5.7
github.com/rs/zerolog v1.26.1
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect
Expand Down
10 changes: 0 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
Expand All @@ -24,11 +22,6 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -65,14 +58,11 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down

0 comments on commit 2b8e436

Please sign in to comment.