Skip to content

Commit 9344700

Browse files
committed
feat(rendering): adds max width to command borders when screen width < max line len
1 parent fbcbcde commit 9344700

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed

examples/run1/Runfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# vim: set ft=yaml:
12
env:
23
file1_env1: "value1"
34

@@ -17,5 +18,6 @@ tasks:
1718
dotenv:
1819
- ../../.secrets/env
1920
cmd:
21+
- run: echo
2022
- printenv file1_env1
2123
- printenv key_id

examples/run2/Runfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
version: 0.0.1
2-
1+
# vim: set ft=yaml:
32
dotenv:
43
- ../../.secrets/env
54

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ require (
99
github.com/charmbracelet/lipgloss v1.0.0
1010
github.com/joho/godotenv v1.5.1
1111
github.com/muesli/termenv v0.15.2
12-
github.com/nxtcoder17/fastlog v0.0.0-20250416154215-c6aac24aebcb
1312
github.com/nxtcoder17/fwatcher v1.2.2-0.20250318121757-bfc2065fa9f5
1413
github.com/nxtcoder17/go.pkgs v0.0.0-20250216034729-39e2d2cd48da
1514
github.com/urfave/cli/v3 v3.0.0-beta1
1615
golang.org/x/sync v0.10.0
16+
golang.org/x/term v0.32.0
1717
sigs.k8s.io/yaml v1.4.0
1818
)
1919

@@ -32,6 +32,6 @@ require (
3232
github.com/samber/lo v1.47.0 // indirect
3333
github.com/samber/slog-common v0.18.1 // indirect
3434
github.com/samber/slog-zerolog/v2 v2.7.3 // indirect
35-
golang.org/x/sys v0.28.0 // indirect
35+
golang.org/x/sys v0.33.0 // indirect
3636
golang.org/x/text v0.16.0 // indirect
3737
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6T
3737
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
3838
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
3939
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
40-
github.com/nxtcoder17/fastlog v0.0.0-20250416154215-c6aac24aebcb h1:BHKArf9In3jINU2B3mTLQb75F5PMoJ5fnOlRD7dOq/w=
41-
github.com/nxtcoder17/fastlog v0.0.0-20250416154215-c6aac24aebcb/go.mod h1:EWaIRNdsxHdqCv53XcNw9H9QMwhEV3LTWfsSQZCiiC4=
4240
github.com/nxtcoder17/fwatcher v1.2.2-0.20250318121757-bfc2065fa9f5 h1:6BulIAQBv2FZqUFZcKtFEhc1vMOeOdeMS+S5d874FMY=
4341
github.com/nxtcoder17/fwatcher v1.2.2-0.20250318121757-bfc2065fa9f5/go.mod h1:SMwIdCpyi5fBygrkCX8hIIUeILzgoxJFaDSlhFBOWWQ=
4442
github.com/nxtcoder17/go.pkgs v0.0.0-20250216034729-39e2d2cd48da h1:Y6GILHFlrihVfDqDPQ98y2kdUeI0SQc8tnoXh2NbEIA=
@@ -67,8 +65,10 @@ golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
6765
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6866
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6967
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
70-
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
71-
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
68+
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
69+
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
70+
golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
71+
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
7272
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
7373
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
7474
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

runner/run-task.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
fn "github.com/nxtcoder17/runfile/functions"
2121
"github.com/nxtcoder17/runfile/parser"
2222
"github.com/nxtcoder17/runfile/types"
23+
"golang.org/x/term"
2324
)
2425

2526
type runTaskArgs struct {
@@ -34,6 +35,18 @@ func isDarkTheme() bool {
3435
return termenv.NewOutput(os.Stdout).HasDarkBackground()
3536
}
3637

38+
func longestLineLen(str string) int {
39+
sp := strings.Split(str, "\n")
40+
l := len(sp[0])
41+
for i := 1; i < len(sp); i++ {
42+
if len(sp[i]) > l {
43+
l = len(sp[i])
44+
}
45+
}
46+
47+
return l
48+
}
49+
3750
func padString(str string, padWith string) string {
3851
sp := strings.Split(str, "\n")
3952
for i := range sp {
@@ -65,8 +78,15 @@ func printCommand(writer io.Writer, prefix, lang, cmd string) {
6578
if !isDarkTheme() {
6679
borderColor = "#3d5485"
6780
}
81+
6882
s := lipgloss.NewStyle().BorderForeground(lipgloss.Color(borderColor)).PaddingLeft(1).PaddingRight(1).Border(lipgloss.RoundedBorder(), true, true, true, true)
6983

84+
width := 0
85+
86+
if term.IsTerminal(0) {
87+
width, _, _ = term.GetSize(0)
88+
}
89+
7090
hlCode := new(bytes.Buffer)
7191
// choose colorschemes from `https://swapoff.org/chroma/playground/`
7292
colorscheme := "catppuccin-macchiato"
@@ -75,10 +95,15 @@ func printCommand(writer io.Writer, prefix, lang, cmd string) {
7595
}
7696
_ = colorscheme
7797

98+
longestLen := longestLineLen(cmd) + len(prefix) + 2 // 2 for spaces around prefix
99+
78100
cmdStr := strings.TrimSpace(cmd)
79101

80102
quick.Highlight(hlCode, cmdStr, lang, "terminal16m", colorscheme)
81103

104+
if width > 0 && longestLen >= width-2 {
105+
s = s.Width(width - 2)
106+
}
82107
fmt.Fprintf(writer, "\r%s%s\n", s.Render(padString(hlCode.String(), prefix)), s.UnsetBorderStyle())
83108
}
84109
}

0 commit comments

Comments
 (0)