add comments to the repl package
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
// Package cli provides the command-line interface for the PM System.
|
||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -7,7 +8,7 @@ import (
|
|||||||
"github.com/LazyBachelor/LazyPM/pkg/cli/commands"
|
"github.com/LazyBachelor/LazyPM/pkg/cli/commands"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CLIConfig is an alias for service.Config, which contains all the necessary
|
// CLIConfig is an alias for service.Config, used to configure the CLI.
|
||||||
type CLIConfig = service.Config
|
type CLIConfig = service.Config
|
||||||
|
|
||||||
// Run initializes the services and executes the CLI commands.
|
// Run initializes the services and executes the CLI commands.
|
||||||
|
|||||||
@@ -6,23 +6,28 @@ import (
|
|||||||
"github.com/c-bata/go-prompt"
|
"github.com/c-bata/go-prompt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// completer provides suggestions for the REPL input based on the current input text.
|
||||||
func completer(d prompt.Document) []prompt.Suggest {
|
func completer(d prompt.Document) []prompt.Suggest {
|
||||||
text := d.TextBeforeCursor()
|
text := d.TextBeforeCursor() // Gets the text before the cursor as a string.
|
||||||
words := strings.Fields(text)
|
words := strings.Fields(text) // Split the string into words as []string.
|
||||||
|
|
||||||
|
// If there are no words, return no suggestions.
|
||||||
if len(words) == 0 {
|
if len(words) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the first word is not "pm", only provide root-level suggestions.
|
||||||
if words[0] != "pm" {
|
if words[0] != "pm" {
|
||||||
return filterByPrefix(rootSuggestions, words[0])
|
return filterByPrefix(rootSuggestions, words[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the first word is "pm", provide command and flag suggestions based on the context.
|
||||||
pmWords := words[1:]
|
pmWords := words[1:]
|
||||||
if len(words) == 1 || (len(pmWords) == 1 && !strings.HasSuffix(text, " ")) {
|
if len(words) == 1 || (len(pmWords) == 1 && !strings.HasSuffix(text, " ")) {
|
||||||
return commandSuggestions(pmWords)
|
return commandSuggestions(pmWords)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the last word starts with a "-", provide flag suggestions for the current command.
|
||||||
if len(pmWords) >= 1 {
|
if len(pmWords) >= 1 {
|
||||||
return flagSuggestions(pmWords[0], pmWords, text)
|
return flagSuggestions(pmWords[0], pmWords, text)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ import (
|
|||||||
"github.com/LazyBachelor/LazyPM/pkg/cli/commands"
|
"github.com/LazyBachelor/LazyPM/pkg/cli/commands"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// execute processes the input command and returns the output
|
||||||
|
// or an error if it occurs. It handles what type of command is being executed,
|
||||||
|
// whether it's a PM command or a shell command, and routes it accordingly.
|
||||||
func execute(input string) (string, error) {
|
func execute(input string) (string, error) {
|
||||||
if input == "" {
|
if input == "" {
|
||||||
return "", nil
|
return "", nil
|
||||||
@@ -26,6 +29,8 @@ func execute(input string) (string, error) {
|
|||||||
return executeShellCommand(input)
|
return executeShellCommand(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// executeShellCommand executes a shell command
|
||||||
|
// and returns its output or an error if it occurs.
|
||||||
func executeShellCommand(input string) (string, error) {
|
func executeShellCommand(input string) (string, error) {
|
||||||
parts := strings.Fields(input)
|
parts := strings.Fields(input)
|
||||||
if len(parts) == 0 {
|
if len(parts) == 0 {
|
||||||
@@ -37,6 +42,8 @@ func executeShellCommand(input string) (string, error) {
|
|||||||
return string(output), err
|
return string(output), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// executePMCommand executes a PM command using the commands package
|
||||||
|
// and returns its output or an error if it occurs.
|
||||||
func executePMCommand(input string) (string, error) {
|
func executePMCommand(input string) (string, error) {
|
||||||
parts := strings.Fields(input)
|
parts := strings.Fields(input)
|
||||||
if len(parts) == 0 {
|
if len(parts) == 0 {
|
||||||
|
|||||||
@@ -5,13 +5,8 @@ import "github.com/c-bata/go-prompt"
|
|||||||
const PromptPrefix = "> "
|
const PromptPrefix = "> "
|
||||||
const OptionMaxSuggestions = 5
|
const OptionMaxSuggestions = 5
|
||||||
|
|
||||||
const (
|
// promptOptions returns a slice of prompt.Option
|
||||||
ReplHelp = `Type 'pm help' for available PM commands.
|
// to configure the behavior and appearance of the REPL prompt.
|
||||||
You can also run shell commands directly. Type 'exit' or 'quit' to leave.`
|
|
||||||
|
|
||||||
ReplTitle = "Welcome to Project Management CLI! " + ReplHelp
|
|
||||||
)
|
|
||||||
|
|
||||||
func promptOptions(history []string) []prompt.Option {
|
func promptOptions(history []string) []prompt.Option {
|
||||||
return []prompt.Option{
|
return []prompt.Option{
|
||||||
prompt.OptionPrefixTextColor(prompt.Cyan),
|
prompt.OptionPrefixTextColor(prompt.Cyan),
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// Package repl implements the Read-Eval-Print Loop (REPL) for the PM CLI.
|
||||||
package repl
|
package repl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -14,45 +15,63 @@ import (
|
|||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ReplHelp = `Type 'pm help' for available PM commands.
|
||||||
|
You can also run shell commands directly. Type 'exit' or 'quit' to leave.`
|
||||||
|
|
||||||
|
ReplTitle = "Welcome to Project Management CLI! " + ReplHelp
|
||||||
|
)
|
||||||
|
|
||||||
|
// RunREPL starts the interactive Read-Eval-Print Loop for the PM CLI.
|
||||||
func RunREPL(ctx context.Context, config cli.CLIConfig) error {
|
func RunREPL(ctx context.Context, config cli.CLIConfig) error {
|
||||||
|
// Set terminal to raw mode to capture input properly in the REPL.
|
||||||
|
// This allows us to handle input character by character and provide a better user experience.
|
||||||
|
// We also ensure that the terminal state is restored when the REPL exits, even if an error occurs.
|
||||||
oldState, err := term.GetState(int(os.Stdin.Fd()))
|
oldState, err := term.GetState(int(os.Stdin.Fd()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get terminal state: %w", err)
|
return fmt.Errorf("failed to get terminal state: %w", err)
|
||||||
}
|
}
|
||||||
defer term.Restore(int(os.Stdin.Fd()), oldState)
|
defer term.Restore(int(os.Stdin.Fd()), oldState)
|
||||||
|
|
||||||
|
// Initialize services for beads, config and stats.
|
||||||
svc, cleanup, err := service.NewServices(ctx, config)
|
svc, cleanup, err := service.NewServices(ctx, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to initialize services: %w", err)
|
return fmt.Errorf("failed to initialize services: %w", err)
|
||||||
}
|
}
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
|
// Make sure to set services, to ensure they are available.
|
||||||
commands.SetServices(svc)
|
commands.SetServices(svc)
|
||||||
|
|
||||||
fmt.Println(styles.TitleStyle.Render(ReplTitle))
|
fmt.Println(styles.TitleStyle.Render(ReplTitle)) // Print REPL title.
|
||||||
|
|
||||||
|
// history keeps track of command history.
|
||||||
|
// This enables navigating through previous commands.
|
||||||
var history []string
|
var history []string
|
||||||
|
|
||||||
|
// Start the REPL loop, which continues until the user types "exit" or "quit".
|
||||||
for {
|
for {
|
||||||
|
// Prompt the user for input, and provide suggestions.
|
||||||
input := prompt.Input(
|
input := prompt.Input(
|
||||||
PromptPrefix,
|
PromptPrefix,
|
||||||
completer,
|
completer,
|
||||||
promptOptions(history)...,
|
promptOptions(history)...,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Trim whitespace from the input to ensure consistent command processing.
|
||||||
input = strings.TrimSpace(input)
|
input = strings.TrimSpace(input)
|
||||||
|
|
||||||
|
// If the user types "exit" or "quit", break the loop and exit the REPL.
|
||||||
if input == "exit" || input == "quit" {
|
if input == "exit" || input == "quit" {
|
||||||
fmt.Println("Goodbye!")
|
fmt.Println("Goodbye!")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add the input to the history for future navigation.
|
||||||
history = append(history, input)
|
history = append(history, input)
|
||||||
|
|
||||||
// Ignore errors for now, gives better ux
|
output, _ := execute(input) // Ignore errors for now, gives better ux
|
||||||
output, _ := execute(input)
|
fmt.Println(styles.CommandStyle.Render(output)) // Print the output of the command in a styled format.
|
||||||
|
|
||||||
fmt.Println(styles.CommandStyle.Render(output))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/muesli/reflow/truncate"
|
"github.com/muesli/reflow/truncate"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// rootSuggestions is a list of prompt suggestions for root-level commands.
|
||||||
var rootSuggestions = []prompt.Suggest{
|
var rootSuggestions = []prompt.Suggest{
|
||||||
{Text: "pm", Description: "Project Management System"},
|
{Text: "pm", Description: "Project Management System"},
|
||||||
{Text: "exit", Description: "Exit pm CLI"},
|
{Text: "exit", Description: "Exit pm CLI"},
|
||||||
@@ -17,6 +18,7 @@ var rootSuggestions = []prompt.Suggest{
|
|||||||
{Text: "git", Description: "Version control system"},
|
{Text: "git", Description: "Version control system"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// commandSuggestions is a list of prompt suggestions for PM commands.
|
||||||
var baseSuggestions = []prompt.Suggest{
|
var baseSuggestions = []prompt.Suggest{
|
||||||
{Text: "help", Description: "Show help information"},
|
{Text: "help", Description: "Show help information"},
|
||||||
{Text: "delete", Description: "Delete an issue by ID"},
|
{Text: "delete", Description: "Delete an issue by ID"},
|
||||||
@@ -26,6 +28,7 @@ var baseSuggestions = []prompt.Suggest{
|
|||||||
{Text: "list", Description: "List all issues"},
|
{Text: "list", Description: "List all issues"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// createFlags is a list of prompt suggestions for the create command flags.
|
||||||
var createFlags = []prompt.Suggest{
|
var createFlags = []prompt.Suggest{
|
||||||
{Text: "--desc", Description: "Issue description"},
|
{Text: "--desc", Description: "Issue description"},
|
||||||
{Text: "--status", Description: "Issue status (open, closed, in_progress)"},
|
{Text: "--status", Description: "Issue status (open, closed, in_progress)"},
|
||||||
@@ -33,6 +36,7 @@ var createFlags = []prompt.Suggest{
|
|||||||
{Text: "--priority", Description: "Issue priority (0-5)"},
|
{Text: "--priority", Description: "Issue priority (0-5)"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// listFlags is a list of prompt suggestions for the list command flags.
|
||||||
var listFlags = []prompt.Suggest{
|
var listFlags = []prompt.Suggest{
|
||||||
{Text: "--title", Description: "Filter by title"},
|
{Text: "--title", Description: "Filter by title"},
|
||||||
{Text: "--desc", Description: "Filter by description"},
|
{Text: "--desc", Description: "Filter by description"},
|
||||||
@@ -42,27 +46,42 @@ var listFlags = []prompt.Suggest{
|
|||||||
{Text: "--limit", Description: "Limit number of results"},
|
{Text: "--limit", Description: "Limit number of results"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// statusValues is a list of prompt suggestions for status types
|
||||||
var statusValues = []prompt.Suggest{
|
var statusValues = []prompt.Suggest{
|
||||||
{Text: "open", Description: "Open status"},
|
{Text: "open", Description: "Open status"},
|
||||||
{Text: "closed", Description: "Closed status"},
|
{Text: "closed", Description: "Closed status"},
|
||||||
{Text: "in_progress", Description: "In progress status"},
|
{Text: "in_progress", Description: "In progress status"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// typeValues is a list of prompt suggestions for issue types
|
||||||
var typeValues = []prompt.Suggest{
|
var typeValues = []prompt.Suggest{
|
||||||
{Text: "bug", Description: "Bug issue type"},
|
{Text: "bug", Description: "Bug issue type"},
|
||||||
{Text: "feature", Description: "Feature issue type"},
|
{Text: "feature", Description: "Feature issue type"},
|
||||||
{Text: "task", Description: "Task issue type"},
|
{Text: "task", Description: "Task issue type"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// priorityValues is a list of prompt suggestions for issue priority levels
|
||||||
var priorityValues = []prompt.Suggest{
|
var priorityValues = []prompt.Suggest{
|
||||||
{Text: "0", Description: "Lowest priority"},
|
{Text: "0", Description: "Lowest priority"},
|
||||||
{Text: "1", Description: "Low priority"},
|
{Text: "1", Description: "Low priority"},
|
||||||
{Text: "2", Description: "Medium-low priority"},
|
{Text: "2", Description: "Medium-low priority"},
|
||||||
{Text: "3", Description: "Medium priority"},
|
{Text: "3", Description: "Medium priority"},
|
||||||
{Text: "4", Description: "High priority"},
|
{Text: "4", Description: "High priority"},
|
||||||
{Text: "5", Description: "Highest priority"},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isIDCommand maps command names to a boolean indicating whether they expect an issue ID as an argument.
|
||||||
|
var isIDCommand = map[string]bool{
|
||||||
|
"describe": true,
|
||||||
|
"delete": true,
|
||||||
|
"del": true,
|
||||||
|
"rm": true,
|
||||||
|
"remove": true,
|
||||||
|
"get": true,
|
||||||
|
"read": true,
|
||||||
|
"close": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// commandSuggestions returns a list of prompt suggestions based on the current input words.
|
||||||
func commandSuggestions(words []string) []prompt.Suggest {
|
func commandSuggestions(words []string) []prompt.Suggest {
|
||||||
if len(words) == 0 {
|
if len(words) == 0 {
|
||||||
return baseSuggestions
|
return baseSuggestions
|
||||||
@@ -70,6 +89,7 @@ func commandSuggestions(words []string) []prompt.Suggest {
|
|||||||
return filterByPrefix(baseSuggestions, words[0])
|
return filterByPrefix(baseSuggestions, words[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// flagSuggestions returns a list of prompt suggestions for command flags based on the current input.
|
||||||
func flagSuggestions(cmd string, words []string, text string) []prompt.Suggest {
|
func flagSuggestions(cmd string, words []string, text string) []prompt.Suggest {
|
||||||
lastWord, prevWord := parseWords(words, text)
|
lastWord, prevWord := parseWords(words, text)
|
||||||
|
|
||||||
@@ -77,8 +97,7 @@ func flagSuggestions(cmd string, words []string, text string) []prompt.Suggest {
|
|||||||
return filterByPrefix(values, lastWord)
|
return filterByPrefix(values, lastWord)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd == "describe" || cmd == "delete" || cmd == "del" ||
|
if isIDCommand[cmd] {
|
||||||
cmd == "rm" || cmd == "remove" || cmd == "get" || cmd == "read" || cmd == "close" {
|
|
||||||
// For ID-oriented commands, pass the current partial argument (lastWord)
|
// For ID-oriented commands, pass the current partial argument (lastWord)
|
||||||
// so issueIDSuggestions can distinguish between completing the command
|
// so issueIDSuggestions can distinguish between completing the command
|
||||||
// name and completing the ID itself.
|
// name and completing the ID itself.
|
||||||
@@ -101,6 +120,7 @@ func flagSuggestions(cmd string, words []string, text string) []prompt.Suggest {
|
|||||||
return filterByPrefix(flags, lastWord)
|
return filterByPrefix(flags, lastWord)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// issueIDSuggestions returns a list of prompt suggestions for issue IDs based on the current partial input.
|
||||||
func issueIDSuggestions(partial string, hasCommand bool) []prompt.Suggest {
|
func issueIDSuggestions(partial string, hasCommand bool) []prompt.Suggest {
|
||||||
// Only show suggestions if we've typed the command already
|
// Only show suggestions if we've typed the command already
|
||||||
if !hasCommand {
|
if !hasCommand {
|
||||||
@@ -119,6 +139,7 @@ func issueIDSuggestions(partial string, hasCommand bool) []prompt.Suggest {
|
|||||||
return suggestions
|
return suggestions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseWords extracts the last and previous words from the input for flag suggestion logic.
|
||||||
func parseWords(words []string, text string) (lastWord, prevWord string) {
|
func parseWords(words []string, text string) (lastWord, prevWord string) {
|
||||||
if len(words) > 0 && !strings.HasSuffix(text, " ") {
|
if len(words) > 0 && !strings.HasSuffix(text, " ") {
|
||||||
lastWord = words[len(words)-1]
|
lastWord = words[len(words)-1]
|
||||||
@@ -131,6 +152,7 @@ func parseWords(words []string, text string) (lastWord, prevWord string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getFlagValues returns a list of prompt suggestions for flag values based on the given flag.
|
||||||
func getFlagValues(flag string) []prompt.Suggest {
|
func getFlagValues(flag string) []prompt.Suggest {
|
||||||
switch flag {
|
switch flag {
|
||||||
case "-s", "--status":
|
case "-s", "--status":
|
||||||
@@ -143,6 +165,7 @@ func getFlagValues(flag string) []prompt.Suggest {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// filterByPrefix filters a list of prompt suggestions based on a given prefix.
|
||||||
func filterByPrefix(suggestions []prompt.Suggest, prefix string) []prompt.Suggest {
|
func filterByPrefix(suggestions []prompt.Suggest, prefix string) []prompt.Suggest {
|
||||||
if prefix == "" {
|
if prefix == "" {
|
||||||
return suggestions
|
return suggestions
|
||||||
|
|||||||
Reference in New Issue
Block a user