Skip to content

Commit

Permalink
feat: freeze wrap text at character width (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani authored May 23, 2024
1 parent 96bc878 commit fc03c0d
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Config struct {
Interactive bool `hidden:"" json:",omitempty" help:"Use an interactive form for configuration options." short:"i" group:"Settings"`
Language string `json:"language,omitempty" help:"Language of code file." short:"l" group:"Settings" placeholder:"go"`
Theme string `json:"theme" help:"Theme to use for syntax highlighting." short:"t" group:"Settings" placeholder:"charm"`
Wrap int `json:"wrap" help:"Wrap lines at a specific width." short:"w" group:"Settings" default:"0" placeholder:"80"`

Output string `json:"output,omitempty" help:"Output location for {{.svg}}, {{.png}}, or {{.webp}}." short:"o" group:"Settings" default:"" placeholder:"freeze.svg"`
Execute string `json:"-" help:"Capture output of command execution." short:"x" group:"Settings" default:""`
Expand Down
5 changes: 5 additions & 0 deletions freeze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ func TestFreezeConfigurations(t *testing.T) {
flags: []string{},
output: "tab",
},
{
input: "test/input/wrap.go",
flags: []string{"--wrap", "80", "--width", "600"},
output: "wrap",
},
}

err := os.RemoveAll("test/output/svg")
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/charmbracelet/x/exp/term/ansi"
"github.com/charmbracelet/x/exp/term/ansi/parser"
"github.com/mattn/go-isatty"
"github.com/muesli/reflow/wordwrap"
)

const (
Expand Down Expand Up @@ -172,6 +173,12 @@ func main() {
isAnsi := strings.ToLower(config.Language) == "ansi" || strippedInput != input
strippedInput = cut(strippedInput, config.Lines)

// wrap to character limit.
if config.Wrap > 0 {
strippedInput = wordwrap.String(strippedInput, config.Wrap)
input = wordwrap.String(input, config.Wrap)
}

if !isAnsi && lexer == nil {
printErrorFatal("Language Unknown", errors.New("specify a language with the --language flag"))
}
Expand Down
2 changes: 1 addition & 1 deletion test/golden/svg/tab.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions test/golden/svg/wrap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/input/tab.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package main

// freeze/issues/50

Expand Down
9 changes: 9 additions & 0 deletions test/input/wrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import "fmt"

// freeze/issues/14

func main() {
fmt.Println("This is a really long line that is going to go over the 80 character limit. This is a really long line that is going to go over the 80 character limit. This is a really long line that is going to go over the 80 character limit. This is a really long line that is going to go over the 80 character limit.")
}

0 comments on commit fc03c0d

Please sign in to comment.