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
2 changes: 1 addition & 1 deletion drivers/completer/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
"unicode"

"github.com/gohxs/readline"
"github.com/chzyer/readline"
"github.com/xo/usql/drivers/metadata"
"github.com/xo/usql/env"
"github.com/xo/usql/text"
Expand Down
2 changes: 1 addition & 1 deletion drivers/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/gohxs/readline"
"github.com/chzyer/readline"
"github.com/xo/dburl"
"github.com/xo/usql/drivers/completer"
"github.com/xo/usql/drivers/metadata"
Expand Down
2 changes: 1 addition & 1 deletion drivers/metadata/mysql/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mysql
import (
"time"

"github.com/gohxs/readline"
"github.com/chzyer/readline"
"github.com/xo/usql/drivers"
"github.com/xo/usql/drivers/completer"
"github.com/xo/usql/drivers/metadata"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/btnguyen2k/gocosmos v1.1.0
github.com/btnguyen2k/godynamo v1.3.0
github.com/chaisql/chai v0.16.1-0.20240218103834-23e406360fd2
github.com/chzyer/readline v1.5.2-0.20250620033330-9dfc369f8652
github.com/couchbase/go_n1ql v0.0.0-20220303011133-0ed4bf93e31d
github.com/databricks/databricks-sql-go v1.7.1
github.com/datafuselabs/databend-go v0.7.5
Expand All @@ -28,7 +29,6 @@ require (
github.com/go-sql-driver/mysql v1.9.3
github.com/gocql/gocql v1.7.0
github.com/godror/godror v0.49.0
github.com/gohxs/readline v0.0.0-20171011095936-a780388e6e7c
github.com/google/go-cmp v0.7.0
github.com/google/goexpect v0.0.0-20210430020637-ab937bf7fd6f
github.com/googleapis/go-sql-spanner v1.16.0
Expand Down
11 changes: 7 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -840,11 +840,15 @@ github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chaisql/chai v0.16.1-0.20240218103834-23e406360fd2 h1:vLtnPMqbTuTQcnfn5EDipZI0PISMSn7AMmz6HhAu+GU=
github.com/chaisql/chai v0.16.1-0.20240218103834-23e406360fd2/go.mod h1:ix/NVvPO+dv6wsmln8R5H22yJb0iIj6KrGpouHaJmrE=
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/logex v1.2.1 h1:XHDu3E6q+gdHgsdTPH6ImJMIp436vR6MPtH8gP05QzM=
github.com/chzyer/logex v1.2.1/go.mod h1:JLbx6lG2kDbNRFnfkgvh4eRJRPX1QCoOIWomwysCBrQ=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
github.com/chzyer/readline v1.5.2-0.20250620033330-9dfc369f8652 h1:C4n4XsMqctHvEAJj7j9xLeoOZVd2Jfyz4ZHmrinfmlI=
github.com/chzyer/readline v1.5.2-0.20250620033330-9dfc369f8652/go.mod h1:Eh+b79XXUwfKfcPLepksvw2tcLE/Ct21YObkaSkeBlk=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/chzyer/test v1.0.0 h1:p3BQDXSxOhOG0P9z6/hGnII4LGiEPOYBhs8asl/fC04=
github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
Expand Down Expand Up @@ -1064,8 +1068,6 @@ github.com/godror/knownpb v0.3.0 h1:+caUdy8hTtl7X05aPl3tdL540TvCcaQA6woZQroLZMw=
github.com/godror/knownpb v0.3.0/go.mod h1:PpTyfJwiOEAzQl7NtVCM8kdPCnp3uhxsZYIzZ5PV4zU=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/gohxs/readline v0.0.0-20171011095936-a780388e6e7c h1:yE35fKFwcelIte3q5q1/cPiY7pI7vvf5/j/0ddxNCKs=
github.com/gohxs/readline v0.0.0-20171011095936-a780388e6e7c/go.mod h1:9S/fKAutQ6wVHqm1jnp9D9sc5hu689s9AaTWFS92LaU=
github.com/golang-jwt/jwt/v4 v4.5.2 h1:YtQM7lnr8iZ+j5q71MGKkNw9Mn7AjHM68uc9g5fXeUI=
github.com/golang-jwt/jwt/v4 v4.5.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-jwt/jwt/v5 v5.2.3 h1:kkGXqQOBSDDWRhWNXTFpqGSCMyh/PLnqUvMGJPDJDs0=
Expand Down Expand Up @@ -1968,6 +1970,7 @@ golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220328115105-d36c6a25d886/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220502124256-b6088ccd6cba/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
56 changes: 48 additions & 8 deletions rline/rline.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io"
"os"

"github.com/gohxs/readline"
"github.com/chzyer/readline"
)

var (
Expand Down Expand Up @@ -47,8 +47,10 @@ type Rline struct {
Inst *readline.Instance
N func() ([]rune, error)
C func() error
Out io.Writer
Err io.Writer
Out io.Writer // This is the currently active stdout writer (could be filtered)
Err io.Writer // This is the currently active stderr writer
originalStdout io.WriteCloser // The base stdout writer, used for filtering
originalStderr io.Writer // The base stderr writer
Int bool
Cyg bool
P func(string)
Expand Down Expand Up @@ -123,9 +125,45 @@ func (l *Rline) Password(prompt string) (string, error) {
return "", ErrPasswordNotAvailable
}

// filterWriter implements io.Writer and applies a string filter.
type filterWriter struct {
writer io.Writer
filter func(string) string
}

func (fw *filterWriter) Write(p []byte) (n int, err error) {
if fw.filter == nil {
return fw.writer.Write(p)
}
filtered := fw.filter(string(p))
return fw.writer.Write([]byte(filtered))
}

// Close implements io.Closer if the underlying writer is an io.Closer.
func (fw *filterWriter) Close() error {
if closer, ok := fw.writer.(io.Closer); ok {
return closer.Close()
}
return nil
}

// SetOutput sets the output format func.
func (l *Rline) SetOutput(f func(string) string) {
l.Inst.Config.Output = f
cfg := l.Inst.Config.Clone() // Get a mutable copy of the config

var currentWriter io.WriteCloser
if f != nil {
// Wrap the original stdout with the filter
currentWriter = &filterWriter{writer: l.originalStdout, filter: f}
} else {
// If filter is nil, revert to the original stdout
currentWriter = l.originalStdout
}
Comment on lines +154 to +161

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to write this this way

Suggested change
var currentWriter io.WriteCloser
if f != nil {
// Wrap the original stdout with the filter
currentWriter = &filterWriter{writer: l.originalStdout, filter: f}
} else {
// If filter is nil, revert to the original stdout
currentWriter = l.originalStdout
}
var currentWriter io.WriteCloser
// use original stdout by default
currentWriter = l.originalStdout
if f != nil {
// Wrap the original stdout with the filter
currentWriter = &filterWriter{writer: l.originalStdout, filter: f}
}

What to you think?


// Update both the Rline's exposed writer and the readline instance's config
l.Out = currentWriter
cfg.Stdout = currentWriter
l.Inst.SetConfig(cfg) // Apply the modified config to the readline instance
}

// New creates a new readline input/output handler.
Expand Down Expand Up @@ -173,8 +211,8 @@ func New(interactive, cygwin, forceNonInteractive bool, out, histfile string) (I
InterruptPrompt: "^C",
HistorySearchFold: true,
Stdin: stdin,
Stdout: stdout,
Stderr: stderr,
Stdout: stdout, // Pass the original stdout
Stderr: stderr, // Pass the original stderr
FuncIsTerminal: func() bool {
return interactive || cygwin
},
Expand Down Expand Up @@ -209,8 +247,10 @@ func New(interactive, cygwin, forceNonInteractive bool, out, histfile string) (I
}
return nil
},
Out: stdout,
Err: stderr,
Out: stdout, // Rline.Stdout() returns this original writer initially
Err: stderr, // Rline.Stderr() returns this original writer
originalStdout: stdout, // Store the original for filtering
originalStderr: stderr, // Store the original for filtering (though SetOutput only affects stdout)
Int: interactive || cygwin,
Cyg: cygwin,
P: l.SetPrompt,
Expand Down
Loading