Skip to content

Commit

Permalink
feat(charmbracelet#89): added input file as window title
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroSuero committed Jun 8, 2024
1 parent fc03c0d commit 808ac7a
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Screenshots can be customized with `--flags` or [Configuration](#configuration)
- [`-t`](#theme), [`--theme`](#theme): Theme to use for syntax highlighting.
- [`-w`](#window), [`--window`](#window): Display window controls.
- [`-H`](#height), [`--height`](#height): Height of terminal window.
- [`--window.title`](#window-title): Display input file as title when window controls are displayed.
- [`--border.width`](#border-width): Border width thickness.
- [`--border.color`](#border-width): Border color.
- [`--shadow.blur`](#shadow): Shadow Gaussian Blur.
Expand Down Expand Up @@ -216,6 +217,16 @@ freeze artichoke.hs --window
<img alt="output of freeze command, Haskell code block with window controls applied" src="./test/golden/svg/window.svg" width="600" />
</a>

#### Window Title

Display the input file as the title of the window.

```bash
freeze artichoke.hs --window --window.title
```

<img alt="output of freeze command, Haskell code block with window title applied" src="./test/golden/svg/title.svg" width="600" />

### Background

Set the background color of the terminal window.
Expand Down
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Config struct {
Margin []float64 `json:"margin" help:"Apply margin to the window." short:"m" placeholder:"0" group:"Window"`
Padding []float64 `json:"padding" help:"Apply padding to the code." short:"p" placeholder:"0" group:"Window"`
Window bool `json:"window" help:"Display window controls." group:"Window"`
Title bool `json:"title" help:"Display input file as title." prefix:"window." group:"Window" hidden:""`
Width float64 `json:"width" help:"Width of terminal window." short:"W" group:"Window"`
Height float64 `json:"height" help:"Height of terminal window." short:"H" group:"Window"`

Expand Down
5 changes: 5 additions & 0 deletions freeze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ func TestFreezeConfigurations(t *testing.T) {
flags: []string{"--wrap", "80", "--width", "600"},
output: "wrap",
},
{
input: "test/input/artichoke.hs",
flags: []string{"--border.radius", "8", "--window", "--window.title"},
output: "title",
},
}

err := os.RemoveAll("test/output/svg")
Expand Down
4 changes: 4 additions & 0 deletions interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ func runForm(config *Config) (*Config, error) {
Inline(true).
Value(&config.Window),

huh.NewConfirm().Title("Title").
Inline(true).
Value(&config.Title),

huh.NewNote().Title("Font"),

huh.NewInput().Title("Font Family ").
Expand Down
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,14 @@ func main() {
svg.Move(windowControls, float64(config.Margin[left]), float64(config.Margin[top]))
image.AddChild(windowControls)
config.Padding[top] += (15 * scale)
if config.Title {
title, err := svg.NewWindowTitle(float64(config.Margin[left]+imageWidth/2), float64(config.Margin[top]+config.Font.Size*float64(scale)), scale, config.Font.Size, config.Font.Family, config.Input, s)
if err != nil {
printErrorFatal("Unable to add title", err)
} else {
image.AddChild(title)
}
}
}

if config.Border.Radius > 0 {
Expand Down
16 changes: 16 additions & 0 deletions svg/svg.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package svg

import (
"errors"
"fmt"
"strconv"
"strings"

"github.com/alecthomas/chroma/v2"
"github.com/beevik/etree"
)

Expand Down Expand Up @@ -95,6 +97,20 @@ func NewWindowControls(r float64, x, y float64) *etree.Element {
return bar
}

func NewWindowTitle(x, y, scale, fs float64, ff, text string, s *chroma.Style) (*etree.Element, error) {
if text != "" && text != "-" {
input := etree.NewElement("text")
input.CreateAttr("font-size", fmt.Sprintf("%.2fpx", fs*float64(scale)))
input.CreateAttr("fill", s.Get(chroma.Text).Colour.String())
input.CreateAttr("font-family", ff)
input.SetText(text)
Move(input, float64(x), float64(y))
return input, nil
}
err := errors.New("no text")
return nil, err
}

// SetDimensions sets the width and height of the given element.
func SetDimensions(element *etree.Element, width, height float64) {
widthAttr := element.SelectAttr("width")
Expand Down
29 changes: 29 additions & 0 deletions test/golden/svg/title.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 808ac7a

Please sign in to comment.