brianstrauch / cobra-shell Goto Github PK
View Code? Open in Web Editor NEWGenerate an interactive, autocompleting shell for any Cobra CLI
License: Apache License 2.0
Generate an interactive, autocompleting shell for any Cobra CLI
License: Apache License 2.0
Hey!
I implemented a directory walker that outputs e.g. ps1
files recursively in nested folders.
Is it possible to do fuzzy searching on this parameter?
I saw fuzzy suggestions are implemented in go-prompt (https://github.com/c-bata/go-prompt/blob/82a912274504477990ecf7c852eebb7c85291772/CHANGELOG.md and c-bata/go-prompt#92).
However, I'm unsure where to set this option and if it is configurable also in cobra-shell.
You showed already a nice way to add keybindings. So I thought there may be an easy solution for this as well. ๐
Hey!
I'm trying to get a persistent history working. However, the struggle is real :D
Maybe you have an idea and better understanding of cobra.
The command has the following additional argument
...
PreRun: WriteHistory,
The following function does write the history line and append it to the file:
func WriteHistory(cmd *cobra.Command, args []string) {
enabledFlags = nil
// Open history file
f, err := os.OpenFile(".fry_history",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()
// Parse flags
cmd.Flags().SortFlags = false
cmd.Flags().Visit(checkFlags)
log.Print()
// Join flags and arguments
cmdLine := cmd.Parent().Use + " " + cmd.Use + " " + strings.Join(enabledFlags, " ") + " " + strings.Join(args, " ")
if _, err := f.WriteString(cmdLine + "\n"); err != nil {
log.Println(err)
}
}
Reading the history does happen at the start of the program execution:
func init...
hist := prompt.OptionHistory(readHistory())
myShell := shell.New(rootCmd, keyBinds, codeBinds, number, hist)
func readHistory() []string {
data, err := os.ReadFile(".fry_history")
if err != nil {
log.Println("No history yet. Nothing to parse")
}
history := strings.Split(string(data), "\n")
return history
}
Build the flag strings (this is super weird and I look for a better solution).
func checkFlags(f *pflag.Flag) {
if f.Value.Type() == "bool" {
//fmt.Println(f.Value.String())
enabledFlags = append(enabledFlags, "--"+f.Name)
} else {
enabledFlags = append(enabledFlags, "--"+f.Name+" \""+f.Value.String()+"\"")
}
fmt.Printf("Flags: %v\n", enabledFlags)
}
This approach works fine for the first execution of a command.
However, the flags do not reset after each command execution. Thus the flags add up when running multiple times in one shell.
By any chance. Do you have experience how to parse the flags correctly in order to write them to a file?
When I have e.g. two flags like the following:
I can use them one time. Afterwards they will be removed. This is expected and good.
However, when I now press up
or rewrite the commands the used completion flags are gone.
The flags should be re-enabled after each execution.
Edit: I saw you already tried to fix this issue in db07c08.
However, I'm on 0.2.4 so maybe the fix is not complete.
Currently flags do show up like this:
However, it might be a good idea to display all --flags
first and after them the -f
(lags).
Another solution would be to show only the long version or both in one line as the description has to be the same and on autocomplete insert the long one as this is the most descriptive. This might need some conditional logic I guess if there are cases with short but no long flags or the other way round.
Hey Brian,
Can't believe I'm the first person creating an issue here. This project is such a great idea! I missed this automation in other projects like ishell or promptui.
I just noticed that alt + backspace
behaves strangely. It will insert question marks instead of deleting complete words as I would expect it in an interactive shell.
As it messes with the cli-ui it might be a bug to address but also a nice feature implementation that I'd love to see!
Edit: Also ctrl+right
and ctrl+left
would be a great idea in order to skip through longer commands as I just noticed :D
Thanks in advance!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.