Skip to content

Commit

Permalink
add option nocolor to avoid special characters
Browse files Browse the repository at this point in the history
  • Loading branch information
karampok authored and saschagrunert committed Mar 14, 2024
1 parent cd91313 commit 915ca84
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
13 changes: 10 additions & 3 deletions demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ const (
// any end.
FlagContinuously = "continuously"

// DryRun
FlagDryRun = "dry-run"

// FlagHideDescriptions is the flag for hiding the descriptions.
FlagHideDescriptions = "hide-descriptions"

// FlagImmediate is the flag for disabling the text animations.
FlagImmediate = "immediate"

// DryRun
FlagDryRun = "dry-run"
// NoColor true to print without colors, special characters
FlagNoColor = "no-color"

// FlagSkipSteps is the flag for skipping n amount of steps.
FlagSkipSteps = "skip-steps"
Expand Down Expand Up @@ -81,7 +84,11 @@ func New() *Demo {
&cli.BoolFlag{
Name: FlagDryRun,
Value: true,
Usage: "run the demo in prints only commands",
Usage: "run the demo and only prints the commands",
},
&cli.BoolFlag{
Name: FlagNoColor,
Usage: "run the demo and output to be without colors",
},
&cli.DurationFlag{
Name: FlagAutoTimeout,
Expand Down
37 changes: 29 additions & 8 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Options struct {
ContinueOnError bool
HideDescriptions bool
DryRun bool
NoColor bool
Immediate bool
SkipSteps int
Shell string
Expand Down Expand Up @@ -73,6 +74,7 @@ func optionsFrom(ctx *cli.Context) Options {
ContinueOnError: ctx.Bool(FlagContinueOnError),
HideDescriptions: ctx.Bool(FlagHideDescriptions),
DryRun: ctx.Bool(FlagDryRun),
NoColor: ctx.Bool(FlagNoColor),
Immediate: ctx.Bool(FlagImmediate),
SkipSteps: ctx.Int(FlagSkipSteps),
Shell: ctx.String(FlagShell),
Expand Down Expand Up @@ -156,21 +158,30 @@ func (r *Run) RunWithOptions(opts Options) error {
}

func (r *Run) printTitleAndDescription() error {
if err := write(r.out, color.Cyan.Sprintf("%s\n", r.title)); err != nil {
p := color.Cyan.Sprintf
if r.options.NoColor {
p = fmt.Sprintf
}
if err := write(r.out, p("%s\n", r.title)); err != nil {
return err
}
for range r.title {
if err := write(r.out, color.Cyan.Sprint("=")); err != nil {
if err := write(r.out, p("=")); err != nil {
return err
}
}
if err := write(r.out, "\n"); err != nil {
return err
}
if !r.options.HideDescriptions {
p = color.White.Darken().Sprintf
if r.options.NoColor {
p = fmt.Sprintf
}

for _, d := range r.description {
if err := write(
r.out, color.White.Darken().Sprintf("%s\n", d),
r.out, p("%s\n", d),
); err != nil {
return err
}
Expand Down Expand Up @@ -207,6 +218,11 @@ func (s *step) run(current, max int) error {
}

func (s *step) echo(current, max int) {
p := color.White.Darken().Sprintf
if s.r.options.NoColor {
p = fmt.Sprintf
}

prepared := []string{}
for i, x := range s.text {
if i == len(s.text)-1 {
Expand All @@ -218,13 +234,13 @@ func (s *step) echo(current, max int) {
}
prepared = append(
prepared,
color.White.Darken().Sprintf(
"# %s [%d/%d]%s\n",
x, current, max, colon,
p(
"# %s [%d/%d]:\n",
x, current, max,
),
)
} else {
m := color.White.Darken().Sprintf("# %s", x)
m := p("# %s", x)
prepared = append(prepared, m)
}
}
Expand All @@ -238,7 +254,12 @@ func (s *step) execute() error {
cmd.Stderr = s.r.out
cmd.Stdout = s.r.out

cmdString := color.Green.Sprintf("%s", strings.Join(s.command, " \\\n "))
p := color.Green.Sprintf
if s.r.options.NoColor {
p = fmt.Sprintf
}

cmdString := p("%s", strings.Join(s.command, " \\\n "))
s.print("```\n" + cmdString + "\n```\n")
if err := s.waitOrSleep(); err != nil {
return fmt.Errorf("unable to execute step: %v: %w", s, err)
Expand Down

0 comments on commit 915ca84

Please sign in to comment.