From c53d9fe30b9e751b388dd7b16644df593374f344 Mon Sep 17 00:00:00 2001 From: Andrew Stormont Date: Wed, 10 Apr 2019 15:10:16 +0100 Subject: [PATCH 01/11] syscall.SYS_IOCTL is not portable --- input_posix.go | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/input_posix.go b/input_posix.go index 14cf8c3..f104303 100644 --- a/input_posix.go +++ b/input_posix.go @@ -4,9 +4,9 @@ package prompt import ( "syscall" - "unsafe" "github.com/c-bata/go-prompt/internal/term" + "golang.org/x/sys/unix" ) const maxReadBytes = 1024 @@ -50,25 +50,11 @@ func (t *PosixParser) Read() ([]byte, error) { return buf[:n], nil } -// winsize is winsize struct got from the ioctl(2) system call. -type ioctlWinsize struct { - Row uint16 - Col uint16 - X uint16 // pixel value - Y uint16 // pixel value -} - // GetWinSize returns WinSize object to represent width and height of terminal. func (t *PosixParser) GetWinSize() *WinSize { - ws := &ioctlWinsize{} - retCode, _, errno := syscall.Syscall( - syscall.SYS_IOCTL, - uintptr(t.fd), - uintptr(syscall.TIOCGWINSZ), - uintptr(unsafe.Pointer(ws))) - - if int(retCode) == -1 { - panic(errno) + ws, err := unix.IoctlGetWinsize(t.fd, unix.TIOCGWINSZ) + if err != nil { + panic(err) } return &WinSize{ Row: ws.Row, From ea6035601114c4fb96ea02b79935762df24a99e5 Mon Sep 17 00:00:00 2001 From: c-bata Date: Tue, 21 May 2019 20:56:22 +0900 Subject: [PATCH 02/11] Add abs-lang/abs as a project using go-prompt --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5406610..fa9b1d8 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ func main() { * [CrushedPixel/moshpit: A Command-line tool for datamoshing.](https://github.com/CrushedPixel/moshpit) * [last-ent/testy-go: Testy Go: A tool for easy testing!](https://github.com/last-ent/testy-go) * [tiagorlampert/CHAOS: a PoC that allow generate payloads and control remote operating systems.](https://github.com/tiagorlampert/CHAOS) +* [abs-lang/abs: ABS is a scripting language that works best on terminal. It tries to combine the elegance of languages such as Python, or Ruby, to the convenience of Bash.](https://github.com/abs-lang/abs) * (If you create a CLI utility using go-prompt and want your own project to be listed here, please submit a GitHub issue.) ## Features From e697e0224d6571276c4560232f4c2a9642e82cb5 Mon Sep 17 00:00:00 2001 From: c-bata Date: Wed, 22 May 2019 11:31:43 +0900 Subject: [PATCH 03/11] Add takashabe/btcli as a project using go-prompt --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fa9b1d8..fb1a4f6 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ func main() { * [last-ent/testy-go: Testy Go: A tool for easy testing!](https://github.com/last-ent/testy-go) * [tiagorlampert/CHAOS: a PoC that allow generate payloads and control remote operating systems.](https://github.com/tiagorlampert/CHAOS) * [abs-lang/abs: ABS is a scripting language that works best on terminal. It tries to combine the elegance of languages such as Python, or Ruby, to the convenience of Bash.](https://github.com/abs-lang/abs) +* [takashabe/btcli: btcli is a CLI client for the Bigtable. Has many read options and auto-completion.](https://github.com/takashabe/btcli) * (If you create a CLI utility using go-prompt and want your own project to be listed here, please submit a GitHub issue.) ## Features From 25ab56b982635230e00bbbfc48a4069eb25b49d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Gara=C3=B1a?= Date: Fri, 28 Jun 2019 12:28:00 +0200 Subject: [PATCH 04/11] Fix http-prompt example (no exit) Implement the exit function on the http-prompt example. --- _example/http-prompt/main.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/_example/http-prompt/main.go b/_example/http-prompt/main.go index 7c16e2d..b372a35 100644 --- a/_example/http-prompt/main.go +++ b/_example/http-prompt/main.go @@ -105,6 +105,9 @@ func executor(in string) { var method, body string blocks := strings.Split(in, " ") switch blocks[0] { + case "exit": + fmt.Println("Bye!") + os.Exit(0) case "cd": if len(blocks) < 2 { ctx.url.Path = "/" From 6b41a4182153f4167d726f621bec7fcea0f0e26e Mon Sep 17 00:00:00 2001 From: c-bata Date: Mon, 8 Jul 2019 21:22:29 +0900 Subject: [PATCH 05/11] Add kafka-prompt as a project using go-prompt --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fb1a4f6..39c1457 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ func main() { * [tiagorlampert/CHAOS: a PoC that allow generate payloads and control remote operating systems.](https://github.com/tiagorlampert/CHAOS) * [abs-lang/abs: ABS is a scripting language that works best on terminal. It tries to combine the elegance of languages such as Python, or Ruby, to the convenience of Bash.](https://github.com/abs-lang/abs) * [takashabe/btcli: btcli is a CLI client for the Bigtable. Has many read options and auto-completion.](https://github.com/takashabe/btcli) +* [ysn2233/kafka-prompt: An interactive kafka-prompt(kafka-shell) built on existing kafka command client](https://github.com/ysn2233/kafka-prompt) * (If you create a CLI utility using go-prompt and want your own project to be listed here, please submit a GitHub issue.) ## Features From 6badcefd40c7869b9a6dce1b65b0f6bb3efaeac1 Mon Sep 17 00:00:00 2001 From: c-bata Date: Sun, 28 Jul 2019 16:38:42 +0900 Subject: [PATCH 06/11] Remove OpenCollective links from README --- .github/FUNDING.yml | 11 +++++++++++ README.md | 25 ------------------------- 2 files changed, 11 insertions(+), 25 deletions(-) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..127b0d1 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,11 @@ +github: [c-bata] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] +patreon: # Replace with a single Patreon username +open_collective: c-bata # Replace with a single Open Collective username +ko_fi: # Replace with a single Ko-fi username +tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel +community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry +liberapay: # Replace with a single Liberapay username +issuehunt: # Replace with a single IssueHunt username +otechie: # Replace with a single Otechie username +custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] + diff --git a/README.md b/README.md index 39c1457..4839352 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ [![Go Report Card](https://goreportcard.com/badge/github.com/c-bata/go-prompt)](https://goreportcard.com/report/github.com/c-bata/go-prompt) ![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square) -[![Backers on Open Collective](https://opencollective.com/go-prompt/backers/badge.svg)](#backers) [![Sponsors on Open Collective](https://opencollective.com/go-prompt/sponsors/badge.svg)](#sponsors) [![GoDoc](https://godoc.org/github.com/c-bata/go-prompt?status.svg)](https://godoc.org/github.com/c-bata/go-prompt) [![Build Status](https://travis-ci.org/c-bata/go-prompt.svg?branch=master)](https://travis-ci.org/c-bata/go-prompt) @@ -112,30 +111,6 @@ Masashi Shibata * Twitter: [@c\_bata\_](https://twitter.com/c_bata_/) * Github: [@c-bata](https://github.com/c-bata/) -## Supporting go-prompt - -### Contributors - -This project exists thanks to all the people who contribute. - - - -### Backers and Sponsors (OpenCollective) - -Started getting support via opencollective. If you support this project by becoming a sponsor, your logo will show up here with a link to your website. - -[![Become a backer](https://opencollective.com/go-prompt/tiers/backer.svg?avatarHeight=64)](https://opencollective.com/go-prompt#backers) - - - - - - - - - - - ## License This software is licensed under the MIT license, see [LICENSE](./LICENSE) for more information. From 2492e11b988922f7b40af506cde2ac47e93a98f7 Mon Sep 17 00:00:00 2001 From: c-bata Date: Wed, 31 Jul 2019 22:12:35 +0900 Subject: [PATCH 07/11] Separate issue template --- .../bug_report.md} | 12 +++++++----- .github/ISSUE_TEMPLATE/feature_request.md | 13 +++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) rename .github/{ISSUE_TEMPLATE.md => ISSUE_TEMPLATE/bug_report.md} (78%) create mode 100644 .github/ISSUE_TEMPLATE/feature_request.md diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE/bug_report.md similarity index 78% rename from .github/ISSUE_TEMPLATE.md rename to .github/ISSUE_TEMPLATE/bug_report.md index 83b2e63..4c8c031 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -1,13 +1,15 @@ -# Suggestions - -*Please write your suggestion here and remove "Bug reports" section.* - +--- +name: "Bug report" +about: Create a bug report to improve go-prompt +title: "[Bug]" +labels: bug +assignees: '' --- # Bug reports -*Please file a bug report here and remove "Suggestions" section.* +*Please file a bug report here.* ## Expected Behavior diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..cb816a2 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,13 @@ +--- +name: "Feature request" +about: Suggest an idea for new features in go-prompt. +title: "[Feature Request]" +labels: enhancement +assignees: '' + +--- + +# Feature Request + +*Please write your suggestion here.* + From ea717205ca73412c085f2b2296f11c674f359f5c Mon Sep 17 00:00:00 2001 From: odino Date: Fri, 16 Aug 2019 04:09:32 +0400 Subject: [PATCH 08/11] Added OptionBreakLineCallback, to run a callback every time there's a line break It's useful to run a function everytime there's a line break -- Enter as well as, for example, ControlC. With this PR, a new option is added to assign a callback that gets called every time `renderer.BreakLine()` is called. Added a test that makes sure the renderer doesn't break if the callback is not specified, as well as to check that it runs ok when the callback executes. Just to give a bit more of context: in [ABS](https://github.com/abs-lang/abs) we are trying to implement ControlR (reverse search), and need to clear the search selection every time the user "clears" the console, either by pressing enter or by clearing the current line (eg. ControlC). --- option.go | 8 ++++++++ render.go | 4 ++++ render_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/option.go b/option.go index 9d843a7..cecdf5e 100644 --- a/option.go +++ b/option.go @@ -234,6 +234,14 @@ func OptionShowCompletionAtStart() Option { } } +// OptionBreakLineCallback to run a callback at every break line +func OptionBreakLineCallback(fn func()) Option { + return func(p *Prompt) error { + p.renderer.BreakLineCallback = fn + return nil + } +} + // New returns a Prompt with powerful auto-completion. func New(executor Executor, completer Completer, opts ...Option) *Prompt { defaultWriter := NewStdoutWriter() diff --git a/render.go b/render.go index c3c89b4..2a38d19 100644 --- a/render.go +++ b/render.go @@ -12,6 +12,7 @@ type Render struct { out ConsoleWriter prefix string livePrefixCallback func() (prefix string, useLivePrefix bool) + BreakLineCallback func() title string row uint16 col uint16 @@ -235,6 +236,9 @@ func (r *Render) BreakLine(buffer *Buffer) { r.out.WriteStr(buffer.Document().Text + "\n") r.out.SetColor(DefaultColor, DefaultColor, false) debug.AssertNoError(r.out.Flush()) + if r.BreakLineCallback != nil { + r.BreakLineCallback() + } r.previousCursor = 0 } diff --git a/render_test.go b/render_test.go index a87aea3..8968af3 100644 --- a/render_test.go +++ b/render_test.go @@ -2,6 +2,7 @@ package prompt import ( "reflect" + "syscall" "testing" ) @@ -65,3 +66,48 @@ func TestFormatCompletion(t *testing.T) { } } } + +func TestBreakLineCallback(t *testing.T) { + var i int + r := &Render{ + prefix: "> ", + out: &PosixWriter{ + fd: syscall.Stdin, // "write" to stdin just so we don't mess with the output of the tests + }, + livePrefixCallback: func() (string, bool) { return "", false }, + prefixTextColor: Blue, + prefixBGColor: DefaultColor, + inputTextColor: DefaultColor, + inputBGColor: DefaultColor, + previewSuggestionTextColor: Green, + previewSuggestionBGColor: DefaultColor, + suggestionTextColor: White, + suggestionBGColor: Cyan, + selectedSuggestionTextColor: Black, + selectedSuggestionBGColor: Turquoise, + descriptionTextColor: Black, + descriptionBGColor: Turquoise, + selectedDescriptionTextColor: White, + selectedDescriptionBGColor: Cyan, + scrollbarThumbColor: DarkGray, + scrollbarBGColor: Cyan, + col: 1, + } + b := NewBuffer() + r.BreakLine(b) + + if i != 0 { + t.Errorf("i should initially be 0, before applying a break line callback") + } + + r.BreakLineCallback = func() { + i++ + } + r.BreakLine(b) + r.BreakLine(b) + r.BreakLine(b) + + if i != 3 { + t.Errorf("BreakLine callback not called, i should be 3") + } +} From dbd5467376115fc7cf936d30f65e3cd5cf46303c Mon Sep 17 00:00:00 2001 From: odino Date: Fri, 16 Aug 2019 14:37:33 +0400 Subject: [PATCH 09/11] Added document as an argument to the LineBreakCallback --- go.mod | 2 +- option.go | 2 +- render.go | 4 ++-- render_test.go | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 45f3b12..570b588 100644 --- a/go.mod +++ b/go.mod @@ -6,5 +6,5 @@ require ( github.com/mattn/go-runewidth v0.0.3 github.com/mattn/go-tty v0.0.0-20180219170247-931426f7535a github.com/pkg/term v0.0.0-20180423043932-cda20d4ac917 - golang.org/x/sys v0.0.0-20180620133508-ad87a3a340fa // indirect + golang.org/x/sys v0.0.0-20180620133508-ad87a3a340fa ) diff --git a/option.go b/option.go index cecdf5e..e8f73d1 100644 --- a/option.go +++ b/option.go @@ -235,7 +235,7 @@ func OptionShowCompletionAtStart() Option { } // OptionBreakLineCallback to run a callback at every break line -func OptionBreakLineCallback(fn func()) Option { +func OptionBreakLineCallback(fn func(*Document)) Option { return func(p *Prompt) error { p.renderer.BreakLineCallback = fn return nil diff --git a/render.go b/render.go index 2a38d19..ac65f76 100644 --- a/render.go +++ b/render.go @@ -12,7 +12,7 @@ type Render struct { out ConsoleWriter prefix string livePrefixCallback func() (prefix string, useLivePrefix bool) - BreakLineCallback func() + BreakLineCallback func(*Document) title string row uint16 col uint16 @@ -237,7 +237,7 @@ func (r *Render) BreakLine(buffer *Buffer) { r.out.SetColor(DefaultColor, DefaultColor, false) debug.AssertNoError(r.out.Flush()) if r.BreakLineCallback != nil { - r.BreakLineCallback() + r.BreakLineCallback(buffer.Document()) } r.previousCursor = 0 diff --git a/render_test.go b/render_test.go index 8968af3..63dd0bd 100644 --- a/render_test.go +++ b/render_test.go @@ -100,7 +100,7 @@ func TestBreakLineCallback(t *testing.T) { t.Errorf("i should initially be 0, before applying a break line callback") } - r.BreakLineCallback = func() { + r.BreakLineCallback = func(doc *Document) { i++ } r.BreakLine(b) From c7735f732df3783cc2c5b8cfde35755236b2c13c Mon Sep 17 00:00:00 2001 From: odino Date: Mon, 26 Aug 2019 17:41:23 +0400 Subject: [PATCH 10/11] make BreaklineCallback private (`breaklineCallback`) --- option.go | 2 +- render.go | 6 +++--- render_test.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/option.go b/option.go index e8f73d1..abce19a 100644 --- a/option.go +++ b/option.go @@ -237,7 +237,7 @@ func OptionShowCompletionAtStart() Option { // OptionBreakLineCallback to run a callback at every break line func OptionBreakLineCallback(fn func(*Document)) Option { return func(p *Prompt) error { - p.renderer.BreakLineCallback = fn + p.renderer.breakLineCallback = fn return nil } } diff --git a/render.go b/render.go index ac65f76..90bdf9d 100644 --- a/render.go +++ b/render.go @@ -12,7 +12,7 @@ type Render struct { out ConsoleWriter prefix string livePrefixCallback func() (prefix string, useLivePrefix bool) - BreakLineCallback func(*Document) + breakLineCallback func(*Document) title string row uint16 col uint16 @@ -236,8 +236,8 @@ func (r *Render) BreakLine(buffer *Buffer) { r.out.WriteStr(buffer.Document().Text + "\n") r.out.SetColor(DefaultColor, DefaultColor, false) debug.AssertNoError(r.out.Flush()) - if r.BreakLineCallback != nil { - r.BreakLineCallback(buffer.Document()) + if r.breakLineCallback != nil { + r.breakLineCallback(buffer.Document()) } r.previousCursor = 0 diff --git a/render_test.go b/render_test.go index 63dd0bd..98cb27c 100644 --- a/render_test.go +++ b/render_test.go @@ -100,7 +100,7 @@ func TestBreakLineCallback(t *testing.T) { t.Errorf("i should initially be 0, before applying a break line callback") } - r.BreakLineCallback = func(doc *Document) { + r.breakLineCallback = func(doc *Document) { i++ } r.BreakLine(b) From 97fbae615bbe468e4365a749bf1217c2d6bc9d23 Mon Sep 17 00:00:00 2001 From: c-bata Date: Sat, 30 Nov 2019 16:05:24 +0900 Subject: [PATCH 11/11] Add fishi0x01/vsh in the project using go-prompt --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4839352..86e6da3 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ func main() { * [abs-lang/abs: ABS is a scripting language that works best on terminal. It tries to combine the elegance of languages such as Python, or Ruby, to the convenience of Bash.](https://github.com/abs-lang/abs) * [takashabe/btcli: btcli is a CLI client for the Bigtable. Has many read options and auto-completion.](https://github.com/takashabe/btcli) * [ysn2233/kafka-prompt: An interactive kafka-prompt(kafka-shell) built on existing kafka command client](https://github.com/ysn2233/kafka-prompt) +* [fishi0x01/vsh: HashiCorp Vault interactive shell](https://github.com/fishi0x01/vsh) * (If you create a CLI utility using go-prompt and want your own project to be listed here, please submit a GitHub issue.) ## Features