oh-my-posh/src/segment_command.go

56 lines
1.2 KiB
Go
Raw Normal View History

2019-03-13 04:14:30 -07:00
package main
import "strings"
type command struct {
2022-01-01 11:08:08 -08:00
props Properties
2022-01-01 11:09:52 -08:00
env Environment
2022-01-22 10:46:56 -08:00
Output string
2019-03-13 04:14:30 -07:00
}
const (
// ExecutableShell to execute command in
ExecutableShell Property = "shell"
// Command to execute
2019-03-13 04:14:30 -07:00
Command Property = "command"
)
func (c *command) template() string {
return "{{ .Output }}"
}
2019-03-13 04:14:30 -07:00
func (c *command) enabled() bool {
shell := c.props.getString(ExecutableShell, "bash")
if !c.env.HasCommand(shell) {
return false
}
2019-03-13 04:14:30 -07:00
command := c.props.getString(Command, "echo no command specified")
if strings.Contains(command, "||") {
commands := strings.Split(command, "||")
for _, cmd := range commands {
output := c.env.RunShellCommand(shell, strings.TrimSpace(cmd))
2019-03-13 04:14:30 -07:00
if output != "" {
2022-01-22 10:46:56 -08:00
c.Output = output
2019-03-13 04:14:30 -07:00
return true
}
}
}
if strings.Contains(command, "&&") {
var output string
commands := strings.Split(command, "&&")
for _, cmd := range commands {
output += c.env.RunShellCommand(shell, strings.TrimSpace(cmd))
2019-03-13 04:14:30 -07:00
}
2022-01-22 10:46:56 -08:00
c.Output = output
return c.Output != ""
2019-03-13 04:14:30 -07:00
}
c.Output = c.env.RunShellCommand(shell, strings.TrimSpace(command))
2022-01-22 10:46:56 -08:00
return c.Output != ""
2019-03-13 04:14:30 -07:00
}
2022-01-01 11:09:52 -08:00
func (c *command) init(props Properties, env Environment) {
2019-03-13 04:14:30 -07:00
c.props = props
c.env = env
}