@@ -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
2526type 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+
3750func 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