Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CRWriter to add \r needed in raw mode automatically and efficiently #1

Merged
merged 2 commits into from
Aug 7, 2024

Conversation

ldemailly
Copy link
Member

@ldemailly ldemailly commented Aug 7, 2024

  • move from grol.io/terminal
  • add CRWriter to add \r
  • add library CI

@github-advanced-security
Copy link

This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

@ldemailly ldemailly merged commit cc708c7 into main Aug 7, 2024
5 checks passed
@ldemailly ldemailly deleted the crwriter branch August 7, 2024 23:11
Comment on lines +33 to +39
if err != nil {
if errors.Is(err, io.EOF) {
log.Infof("EOF received, exiting.")
return 0
}
return log.FErrf("Error reading line: %v", err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Easier to read and understand

Suggested change
if err != nil {
if errors.Is(err, io.EOF) {
log.Infof("EOF received, exiting.")
return 0
}
return log.FErrf("Error reading line: %v", err)
}
if errors.Is(err, io.EOF) {
log.Infof("EOF received, exiting.")
return 0
}
if err != nil {
return log.FErrf("Error reading line: %v", err)
}

}

// CRWriter is a writer that adds \r before each \n.
// Needed for raw mode terminals (and I guess also if you want something DOS or http headers like).
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Needed for raw mode terminals (and I guess also if you want something DOS or http headers like).
// Needed for raw mode terminals (and I guess also if you want something DOS or HTTP headers like).

if !t.IsTerminal() {
t.Out = os.Stderr // no need to add \r for non raw mode.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
t.Out = os.Stderr // no need to add \r for non raw mode.
t.Out = os.Stderr // no need to add \r for non-raw mode.

Comment on lines 11 to +16
type Terminal struct {
fd int
oldState *term.State
term *term.Terminal
Out io.Writer
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
type Terminal struct {
fd int
oldState *term.State
term *term.Terminal
Out io.Writer
}
type Terminal struct {
*term.Terminal
fd int
oldState *term.State
Out io.Writer
}

Maybe this, would avoid to define Readline and SetPrompt

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm trying to control the exact exposed surface vs having users know both packages/types

but maybe... tbd

Comment on lines +98 to +105

func (t *Terminal) ReadLine() (string, error) {
return t.term.ReadLine()
}

func (t *Terminal) SetPrompt(s string) {
t.term.SetPrompt(s)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func (t *Terminal) ReadLine() (string, error) {
return t.term.ReadLine()
}
func (t *Terminal) SetPrompt(s string) {
t.term.SetPrompt(s)
}

See #1 (comment)

Comment on lines +41 to +45
/*
if isTerm {
cli.UntilInterrupted()
}
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Wip?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes it's gone now

@@ -8,6 +8,8 @@ require (
golang.org/x/term v0.23.0
)

// replace fortio.org/cli => ../cli
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was testing fortio/cli#38

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants