Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#89): added input file as window title #107

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
808ac7a
feat(#89): added input file as window title
AlejandroSuero Jun 8, 2024
fbc39f8
fix: removed else statment when fatal
AlejandroSuero Jun 9, 2024
8e465d7
feat(svg): center text and improved error message
AlejandroSuero Jun 10, 2024
09a8bf8
feat(configurations): added `title: auto` to `full.json`
AlejandroSuero Jun 10, 2024
0ec729a
feat(config): modified `config.Title` to be a string
AlejandroSuero Jun 10, 2024
822f84c
feat(interactive): modified to work with `config.Title` as string
AlejandroSuero Jun 10, 2024
32641ce
feat(main): modified to work with `config.Title` as string
AlejandroSuero Jun 10, 2024
095d65c
test(window.title): test suite for `--window.title`
AlejandroSuero Jun 10, 2024
3cd9296
docs: updated `README.md` with new usage of `config.Title` as string
AlejandroSuero Jun 10, 2024
03aff1a
feat(svg): early return
AlejandroSuero Jun 10, 2024
ad05ab8
feat(golden): modified images when `make golden`
AlejandroSuero Jun 10, 2024
0c51a1c
docs(README): better demo and better warning
AlejandroSuero Jun 10, 2024
c087cce
test(freeze): using `t.Cleanup` and checking for child elements
AlejandroSuero Jun 10, 2024
8d2ab0a
feat(main): use `} else if {`
AlejandroSuero Jun 10, 2024
014f87d
feat: added title position and title text
AlejandroSuero Jun 11, 2024
de16e84
feat: added title position logic
AlejandroSuero Jun 11, 2024
716540c
feat: added title positions
AlejandroSuero Jun 11, 2024
89fb933
test(golden): `make golden` after changes
AlejandroSuero Jun 11, 2024
9341f16
refactor(title): moved from `svg.go` to `main.go`
AlejandroSuero Jun 11, 2024
af731cf
docs: updated `--title` usage
AlejandroSuero Jun 11, 2024
a2ba0f3
fix: removed `group:"Window"` from `Title`
AlejandroSuero Jun 11, 2024
406de92
refactor: switch and value usage
AlejandroSuero Jun 12, 2024
11b73ac
refactor: use constants for positions
AlejandroSuero Jun 12, 2024
7a46616
feat: increase font size a bit and improved center with controls
AlejandroSuero Jun 12, 2024
bb40c9b
fix: simply return err
AlejandroSuero Jun 12, 2024
244c20c
Merge branch 'main' into feature/show-filename-when-controls
AlejandroSuero Sep 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
AlejandroSuero marked this conversation as resolved.
Show resolved Hide resolved
AlejandroSuero marked this conversation as resolved.
Show resolved Hide resolved
- [`--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)
AlejandroSuero marked this conversation as resolved.
Show resolved Hide resolved
} 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 != "-" {
AlejandroSuero marked this conversation as resolved.
Show resolved Hide resolved
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.