cobi
cobi (cobra Interactive) is a small wrapper on top of cobra and tview to build interactive cli applications
Explore the docs »
Examples
·
Report Bug
·
Request Feature
Table of Contents
About The Project
Cobra provides a great way to build cli applications whereas tview provides powerful cui. But there are scenarios where the application needs to execute a long running task, for eg port forwarding in kubernetes. This could be solved with an interactive prompt, while normal operations can still work with the default cli application.
Overview
cobi works by using command completion provided by cobra. These completions are propagated to tview providing almost the same experience in both interactive and cli modes. Since cobi implements the same interface as cobra, it becomes very easy to port your existing project to cobi.
Installing
First, use go get to install the latest version of the library. This command will download cobi with all its dependencies:
go get -u github.com/arjit95/cobi
Next, include cobi in your app:
import "github.com/arjit95/cobi"
Migrating from cobra
Suppose you have an existing cobra command:
import "github.com/spf13/cobra"
cmd := &cobra.Command{
Use: "demo",
Short: "This is a demo cobra command",
Run: func(cmd *cobra.Command, args []string) {
// Do Stuff Here
},
}
This would be re-written as
import (
"github.com/arjit95/cobi"
"github.com/arjit95/cobi/editor"
"github.com/spf13/cobra"
)
// Only wrapping the top most command is sufficient
// There is no need to touch other commands.
cmd := cobi.NewCommand(editor.NewEditor(), &cobra.Command{
Use: "demo",
Short: "This is a demo cobra command",
Run: func(cmd *cobra.Command, args []string) {
// Do Stuff Here
},
})
// Execute the command normally
cmd.Execute()
// Alternaitvely run the command in interactive mode
// Ctrl+C to exit
cmd.ExecuteInteractive()
Shortcuts
Shortcut |
Operation |
Ctrl+L |
Clear logger pane |
Ctrl+O |
Clear output pane |
Up |
Previous command |
Down |
Next command |
Tab |
Focus next suggestion |
Shift+Tab |
Focus previous suggestion |
Enter |
Execute command/Select suggestion |
TODO
- We should suggest only the next keyword instead of populating the entire command. This could be useful for long commands. Currently tview only provides the functionality for replacing the text in input field, there is no option to append the auto complete suggestion. A workaround would be to add a completely new input type based on original tview inputfield with better append suggestion support.
Contributions
Contributions are welcome, this was my first experience with golang so a lot of things may not be up to the mark. Feel free to open an issue or raise a pull request.
License
Distributed under the MIT License. See LICENSE
for more information.
Acknowledgements