Skip to content

Commit 51f3bea

Browse files
authored
feat: support multiple lines mode (#8)
Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
1 parent 58a4e19 commit 51f3bea

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

cli/root.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func NewRootCommand() (cmd *cobra.Command) {
3131
flags := cmd.Flags()
3232
flags.BoolVarP(&opt.loop, "loop", "", true, "Run the Markdown in loop mode.")
3333
flags.BoolVarP(&opt.keepFilter, "keep-filter", "", true, "Indicate if keep the filter.")
34+
flags.BoolVarP(&opt.keepScripts, "keep-scripts", "", false, "Indicate if keep the temporary scripts.")
3435
return
3536
}
3637

@@ -75,6 +76,8 @@ func (o *option) runMarkdown(mdFilePath string) (err error) {
7576
}
7677

7778
if content != "" && lang == "shell" {
79+
originalContent := content
80+
7881
// handle the break line
7982
breakline := regexp.MustCompile(`\\\n`)
8083
content = breakline.ReplaceAllString(content, "")
@@ -91,7 +94,15 @@ func (o *option) runMarkdown(mdFilePath string) (err error) {
9194
continue
9295
}
9396
title = strings.TrimPrefix(title, "#!title: ")
94-
cmdMap[title] = append(cmdMap[title], lines[1:]...)
97+
// support multiple lines mode
98+
if strings.Contains(title, "+f") {
99+
title = strings.ReplaceAll(title, "+f", "")
100+
title = strings.TrimSpace(title)
101+
102+
cmdMap[title] = append(cmdMap[title], originalContent)
103+
} else {
104+
cmdMap[title] = append(cmdMap[title], lines[1:]...)
105+
}
95106
}
96107
}
97108

@@ -102,8 +113,9 @@ func (o *option) runMarkdown(mdFilePath string) (err error) {
102113
}
103114

104115
type option struct {
105-
loop bool
106-
keepFilter bool
116+
loop bool
117+
keepFilter bool
118+
keepScripts bool
107119
}
108120

109121
func (o *option) execute(cmdMap map[string][]string, contextDir string) (err error) {
@@ -145,7 +157,7 @@ func (o *option) execute(cmdMap map[string][]string, contextDir string) (err err
145157
continue
146158
}
147159

148-
err = runCmdLine(cmdLine, contextDir)
160+
err = runCmdLine(cmdLine, contextDir, o.keepScripts)
149161
if err != nil {
150162
break
151163
}
@@ -187,15 +199,17 @@ func inputRequest(pair []string) (result []string, err error) {
187199
return
188200
}
189201

190-
func runCmdLine(cmdLine, contextDir string) (err error) {
202+
func runCmdLine(cmdLine, contextDir string, keepScripts bool) (err error) {
191203
var shellFile string
192204
if shellFile, err = writeAsShell(cmdLine, contextDir); err != nil {
193205
fmt.Println(err)
194206
return
195207
}
196-
defer func() {
197-
_ = os.RemoveAll(shellFile)
198-
}()
208+
if !keepScripts {
209+
defer func() {
210+
_ = os.RemoveAll(shellFile)
211+
}()
212+
}
199213

200214
cmd := exec.Command("bash", path.Base(shellFile))
201215
cmd.Dir = contextDir

0 commit comments

Comments
 (0)