Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# tui
# tui

## Maintenance

- [readline / console upstream sync](docs/readline-console-upstream-sync.md)
4 changes: 2 additions & 2 deletions console/.github/workflows/codacy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
steps:
# Checkout the repository to the GitHub Actions runner
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v6

# Execute Codacy Analysis CLI and generate a SARIF output with the security issues identified during the analysis
- name: Run Codacy Analysis CLI
Expand All @@ -56,6 +56,6 @@ jobs:

# Upload the SARIF file generated in the previous step
- name: Upload SARIF results file
uses: github/codeql-action/upload-sarif@v2
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: results.sarif
8 changes: 4 additions & 4 deletions console/.github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v6

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -58,7 +58,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
uses: github/codeql-action/autobuild@v3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
Expand All @@ -71,6 +71,6 @@ jobs:
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{matrix.language}}"
4 changes: 2 additions & 2 deletions console/.github/workflows/dependency-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: 'Checkout Repository'
uses: actions/checkout@v3
uses: actions/checkout@v6
- name: 'Dependency Review'
uses: actions/dependency-review-action@v2
uses: actions/dependency-review-action@v5
17 changes: 10 additions & 7 deletions console/.github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,34 @@ jobs:
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v6

- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v6
with:
go-version: '1.21'
go-version-file: go.mod

- name: Build
run: go build -v ./...

- name: Run coverage
run: go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v6
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false

windows:
runs-on: windows-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v6

- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v6
with:
go-version: '1.21'
go-version-file: go.mod

- name: Build
run: go build -v ./...
Expand Down
2 changes: 1 addition & 1 deletion console/.github/workflows/label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ jobs:
pull-requests: write

steps:
- uses: actions/labeler@v4
- uses: actions/labeler@v6
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
2 changes: 2 additions & 0 deletions console/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
console.wiki
.gemini
.claude
49 changes: 20 additions & 29 deletions console/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@

<!-- Badges -->
<p align="center">
<a href="https://github.com/reeflective/console/actions/workflows/go.yml">
<img src="https://github.com/reeflective/console/actions/workflows/go.yml/badge.svg?branch=main"
<a href="https://github.com/chainreactors/tui/console/actions/workflows/go.yml">
<img src="https://github.com/chainreactors/tui/console/actions/workflows/go.yml/badge.svg?branch=main"
alt="Github Actions (workflows)" />
</a>

<a href="https://github.com/reeflective/console">
<a href="https://github.com/chainreactors/tui/console">
<img src="https://img.shields.io/github/go-mod/go-version/reeflective/console.svg"
alt="Go module version" />
</a>

<a href="https://pkg.go.dev/github.com/reeflective/console">
<a href="https://pkg.go.dev/github.com/chainreactors/tui/console">
<img src="https://img.shields.io/badge/godoc-reference-blue.svg"
alt="GoDoc reference" />
</a>

<a href="https://goreportcard.com/report/github.com/reeflective/console">
<img src="https://goreportcard.com/badge/github.com/reeflective/console"
<a href="https://goreportcard.com/report/github.com/chainreactors/tui/console">
<img src="https://goreportcard.com/badge/github.com/chainreactors/tui/console"
alt="Go Report Card" />
</a>

Expand All @@ -40,7 +40,7 @@
</a>
</p>

Console is an all-in-one console application library built on top of a [readline](https://github.com/reeflective/readline) shell and using [Cobra](https://github.com/spf13/cobra) commands.
Console is an all-in-one console application library built on top of a [readline](https://github.com/chainreactors/tui/readline) shell and using [Cobra](https://github.com/spf13/cobra) commands.
It aims to provide users with a modern interface at at minimal cost while allowing them to focus on developing
their commands and application core: the console will then transparently interface with these commands, and provide
the various features below almost for free.
Expand All @@ -55,7 +55,7 @@ the various features below almost for free.
- Bind handlers to special interrupt errors (eg. `CtrlC`/`CtrlD`), per menu.

### Shell interface
- Shell is powered by a [readline](https://github.com/reeflective/readline) instance, with full `inputrc` support and extended functionality.
- Shell is powered by a [readline](https://github.com/chainreactors/tui/readline) instance, with full `inputrc` support and extended functionality.
- All features of readline are supported in the console. It also allows the console to give:
- Configurable bind keymaps, commands and options, sane defaults, and per-application configuration.
- Out-of-the-box, advanced completions for commands, flags, positional and flag arguments.
Expand All @@ -71,23 +71,23 @@ the various features below almost for free.

## Documentation

You can install and use the [example application console](https://github.com/reeflective/console/tree/main/example). This example application
You can install and use the [example application console](https://github.com/chainreactors/tui/console/tree/main/example). This example application
will give you a taste of the behavior and supported features. The following documentation
is also available in the [wiki](https://github.com/reeflective/console/wiki):
is also available in the [wiki](https://github.com/chainreactors/tui/console/wiki):

* [Getting started](https://github.com/reeflective/console/wiki/Getting-Started)
* [Menus](https://github.com/reeflective/console/wiki/Menus)
* [Prompts](https://github.com/reeflective/console/wiki/Prompts)
* [Binding commands](https://github.com/reeflective/console/wiki/Binding-Commands)
* [Interrupt handlers](https://github.com/reeflective/console/wiki/Interrupt-Handlers)
* [History Sources](https://github.com/reeflective/console/wiki/History-Sources)
* [Logging](https://github.com/reeflective/console/wiki/Logging)
* [Readline shell](https://github.com/reeflective/readline/wiki)
* [Other utilities](https://github.com/reeflective/console/wiki/Other-Utililites)
* [Getting started](https://github.com/chainreactors/tui/console/wiki/Getting-Started)
* [Menus](https://github.com/chainreactors/tui/console/wiki/Menus)
* [Prompts](https://github.com/chainreactors/tui/console/wiki/Prompts)
* [Binding commands](https://github.com/chainreactors/tui/console/wiki/Binding-Commands)
* [Interrupt handlers](https://github.com/chainreactors/tui/console/wiki/Interrupt-Handlers)
* [History Sources](https://github.com/chainreactors/tui/console/wiki/History-Sources)
* [Logging](https://github.com/chainreactors/tui/console/wiki/Logging)
* [Readline shell](https://github.com/chainreactors/tui/readline/wiki)
* [Other utilities](https://github.com/chainreactors/tui/console/wiki/Other-Utililites)


## Showcase
![console](https://github.com/reeflective/console/blob/assets/console.gif)
![console](https://github.com/chainreactors/tui/console/blob/assets/console.gif)


## Status
Expand All @@ -99,12 +99,3 @@ The library is in a pre-release candidate status:

Please open a PR or an issue if you wish to bring enhancements to it.
Other contributions, as well as bug fixes and reviews are also welcome.


## Possible Improvements

The following is a currently moving list of possible enhancements to be made in order to reach `v1.0`:
- [ ] Ensure to the best extent possible a thread-safe access to the command API.
- [ ] Clearer integration/alignment of the various I/O references between raw readline and commands.
- [ ] Clearer and sane model for asynchronous control/cancel of commands (with OnKillRun in cobra)
- [ ] Test suite for most important or risky code paths.
34 changes: 12 additions & 22 deletions console/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type Commands func() *cobra.Command

// SetCommands requires a function returning a tree of cobra commands to be used.
func (m *Menu) SetCommands(cmds Commands) {
m.mutex.RLock()
defer m.mutex.RUnlock()
m.mutex.Lock()
defer m.mutex.Unlock()
m.cmds = cmds
}

Expand All @@ -32,6 +32,9 @@ func (m *Menu) SetCommands(cmds Commands) {
// If "windows" is used as the argument here, all windows commands for the current
// menu are subsequently hidden, until ShowCommands("windows") is called.
func (c *Console) HideCommands(filters ...string) {
c.mutex.Lock()
defer c.mutex.Unlock()

next:
for _, filt := range filters {
for _, filter := range c.filters {
Expand All @@ -51,8 +54,8 @@ next:
// Use this function if you have previously called HideCommands("filter") and want
// these commands to be available back under their respective menu.
func (c *Console) ShowCommands(filters ...string) {
c.mutex.RLock()
defer c.mutex.RUnlock()
c.mutex.Lock()
defer c.mutex.Unlock()

updated := make([]string, 0)

Expand All @@ -76,23 +79,11 @@ next:
}

// resetFlagsDefaults resets all flags to their default values.
//
// Slice flags accumulate per execution (and do not reset),
//
// so we must reset them manually.
//
// Example:
//
// Given cmd.Flags().StringSlice("comment", nil, "")
// If you run a command with --comment "a" --comment "b" you will get
// the expected [a, b] slice.
//
// If you run a command again with no --comment flags, you will get
// [a, b] again instead of an empty slice.
//
// If you run the command again with --comment "c" --comment "d" flags,
// you will get [a, b, c, d] instead of just [c, d].
func resetFlagsDefaults(target *cobra.Command) {
if target == nil {
return
}

target.Flags().VisitAll(func(flag *pflag.Flag) {
flag.Changed = false
switch value := flag.Value.(type) {
Expand All @@ -104,9 +95,8 @@ func resetFlagsDefaults(target *cobra.Command) {
}

value.Replace(res)

default:
flag.Value.Set(flag.DefValue)
_ = flag.Value.Set(flag.DefValue)
}
})
}
Loading