Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [#387] Enhance the ability of the command feature #476

Merged
merged 21 commits into from
May 19, 2024

Conversation

kkumar-gcc
Copy link
Member

@kkumar-gcc kkumar-gcc commented May 15, 2024

Closes goravel/goravel#387

📑 Description

  • Ask: Prompts the user for input with a question.
  • Choice: Prompts the user to select from a list of options.
  • Confirm: Asks for a yes or no confirmation.
  • Secret: Prompts for a secret without showing input.
  • MultiSelect: Allows selecting multiple options from a list.
  • Info: Writes an informational message to the console.
  • Error: Writes an error message to the console.
  • Line: Writes a string to the console.
  • NewLine: Writes a newline character to the console.
  • Spinner: Creates a new spinner instance.
  • Comment: Writes a comment message to the console.
  • Warn: Writes a warning message to the console.
  • WithProgressBar: executes a callback with a progress bar.
  • CreateProgressBar: creates a new progress bar instance.

Example

Ask

question := "How are you feeling today?"
answer, err := ctx.Ask(question, console.AskOption{
	Placeholder: "Enter your feeling here",
	Lines:       2,
})
if err != nil {
	return err
}
ctx.Error(fmt.Sprintf("You said: %s", answer))

image
image
image

Confirm

confirmationQuestion := "Are you sure you want to continue?"
confirm, err := ctx.Confirm(confirmationQuestion, console.ConfirmOption{
	Description: "Please confirm your action",
	Affirmative: "yes",
	Negative:    "no",
})
if err != nil {
	return err
}
if confirm {
	ctx.Line("You confirmed to continue.")
} else {
	ctx.Line("You chose not to continue.")
}

image
image

Secret

secretQuestion := "Enter your password:"
password, err := ctx.Secret(secretQuestion, console.SecretOption{
	Placeholder: "Your password",
	Validate: func(s string) error {
		if len(s) < 8 {
			return errors.New("password length should be at least 8")
		}
		return nil
	},
})
if err != nil {
	return err
}
ctx.Info(fmt.Sprintf("Your password is: %s", password))

image
image
image
image

MultiSelect

choices := []console.Choice{
	{Key: "1", Value: "Choice 1", Selected: false},
	{Key: "2", Value: "Choice 2", Selected: false},
	{Key: "3", Value: "Choice 3", Selected: false},
}

choiceQuestion := "Choose one of the following options:"
selectedChoice, err := ctx.MultiSelect(choiceQuestion, choices, console.MultiSelectOption{
	Default:     []string{"1"},
	Description: "Please select one of the options",
	Validate: func(s []string) error {
		if len(s) == 0 {
			return errors.New("you must select one of the options")
		}
		return nil
	},
})
if err != nil {
	return err
}
ctx.Info(fmt.Sprintf("You selected: %s", selectedChoice))

image
image
image

Spinner

action := func() { time.Sleep(5 * time.Second) }
err := ctx.Spinner("Hello World!", console.SpinnerOption{
	Action: action,
})
if err != nil {
	return err
}

image

image

ProgressBar

bar := ctx.CreateProgressBar(1000)
err := bar.Start()
if err != nil {
	return err
}
for i := 1; i <= 1000; i++ {
	bar.SetTitle("Processing user: " + cast.ToString(i))
	bar.Advance()
	time.Sleep(time.Millisecond * 50)
}
err = bar.Finish()
if err != nil {
	return err
}

image

✅ Checks

  • My pull request adheres to the code style of this project
  • My code requires changes to the documentation
  • I have updated the documentation as required
  • All the tests have passed

ℹ Additional Information

@kkumar-gcc kkumar-gcc closed this May 15, 2024
@kkumar-gcc kkumar-gcc reopened this May 15, 2024
@kkumar-gcc kkumar-gcc requested a review from a team May 15, 2024 10:18
Copy link

codecov bot commented May 15, 2024

Codecov Report

Attention: Patch coverage is 0% with 150 lines in your changes are missing coverage. Please review.

Project coverage is 70.30%. Comparing base (24c6f74) to head (8a41b62).

Current head 8a41b62 differs from pull request most recent head d66a7da

Please upload reports for the commit d66a7da to get more accurate results.

Files Patch % Lines
console/cli_context.go 0.00% 120 Missing ⚠️
console/progress_bar.go 0.00% 30 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #476      +/-   ##
==========================================
- Coverage   71.34%   70.30%   -1.04%     
==========================================
  Files         171      172       +1     
  Lines       10388    10538     +150     
==========================================
- Hits         7411     7409       -2     
- Misses       2410     2562     +152     
  Partials      567      567              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can test the logic? And could you add some screenshots for this feature in description?

@kkumar-gcc
Copy link
Member Author

kkumar-gcc commented May 15, 2024

I have tested it locally (works great) and will include screenshots. Additionally, I will try to test it programmatically.

@kkumar-gcc kkumar-gcc closed this May 15, 2024
@kkumar-gcc kkumar-gcc reopened this May 15, 2024
Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great optimization

console/cli_context.go Outdated Show resolved Hide resolved
return answer, nil
}

func (r *CliContext) Spinner(message string, option ...console.SpinnerOption) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to use this function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an example for it.

Copy link
Contributor

@hwbrzzl hwbrzzl May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we implement it like below? The use case is a bit strange now.
image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually spinner is different from progressBar. I will try to implement it similar to image.

@kkumar-gcc kkumar-gcc closed this May 16, 2024
@kkumar-gcc kkumar-gcc reopened this May 16, 2024
@kkumar-gcc kkumar-gcc changed the title feat: enhance the ability of the command feature feat: [#387] Enhance the ability of the command feature May 17, 2024
Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great 👍

console/cli_context.go Outdated Show resolved Hide resolved
console/cli_context.go Show resolved Hide resolved
contracts/console/command.go Outdated Show resolved Hide resolved
contracts/console/command.go Outdated Show resolved Hide resolved
contracts/console/command.go Outdated Show resolved Hide resolved
console/cli_context_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The progress is a bit ugly, could you please optimize it a little? And what's the 0s% stand for?

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0s% shouldn't be there. I didn't get anything like this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can try this:

ctx := &console.CliContext{}
bar := ctx.CreateProgressBar(10)
err := bar.Start()
if err != nil {
ctx.Error(err.Error())
return
}

for i := 0; i < 100; i++ {
// performTask()
if i%2 == 0 {
	bar.Advance(2)
} else {
	bar.Advance()
}
time.Sleep(time.Millisecond * 50)
}

err = bar.Finish()
if err != nil {
ctx.Error(err.Error())
return
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's because of the code mistake.

@kkumar-gcc kkumar-gcc closed this May 18, 2024
@kkumar-gcc kkumar-gcc reopened this May 18, 2024
@kkumar-gcc kkumar-gcc requested a review from hwbrzzl May 18, 2024 15:59
hwbrzzl
hwbrzzl previously approved these changes May 18, 2024
Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work

@hwbrzzl
Copy link
Contributor

hwbrzzl commented May 19, 2024

How about the green?

image image

@kkumar-gcc
Copy link
Member Author

How about the green?
image image

Looks cool will apply it.

@kkumar-gcc
Copy link
Member Author

image
image

@kkumar-gcc kkumar-gcc requested a review from hwbrzzl May 19, 2024 02:15
@kkumar-gcc kkumar-gcc merged commit bae87f1 into master May 19, 2024
6 checks passed
@kkumar-gcc kkumar-gcc deleted the kkumar-gcc/#398 branch May 19, 2024 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

✨ [Feature] enhance the ability of the command feature
2 participants