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

otto/repl: pretty print expression results #338

Open
robfig opened this issue Feb 18, 2019 · 1 comment
Open

otto/repl: pretty print expression results #338

robfig opened this issue Feb 18, 2019 · 1 comment

Comments

@robfig
Copy link

robfig commented Feb 18, 2019

First, thank you for the wonderful project! It's been really helpful.

I recently started using the repl module for a project I was hoping to implement in Go rather than Node.js, and I quickly ran into a stakeholder concern of lacking the helpful REPL feature of pretty-printing the results of expressions.

For example, using repl.Run, referencing a JS object results in this being printed

[object Object]

instead of something helpful like the Node.js repl prints:

> entities
EntityDB {
  db:
   Datastore {
     inMemoryOnly: true,
     autoload: false,
     timestampData: false,
     filename: null,
     compareStrings: undefined,
 ....

Looking into the code, I don't see any way to customize the printed output, so I was wondering if you would be open to a pull request?

Here is the relevant code:

# otto/repl/repl.go

			v, err := vm.Eval(s) // <--
			if err != nil {
				if oerr, ok := err.(*otto.Error); ok {
					io.Copy(rl.Stdout(), strings.NewReader(oerr.String()))
				} else {
					io.Copy(rl.Stdout(), strings.NewReader(err.Error()))
				}
			} else {
				rl.Stdout().Write([]byte(v.String() + "\n"))
			}

One way to permit this use case is to add an optional Eval func to the repl.Options struct that replaces the line above, if set.

How does that sound?

Thank you,
Rob

@stevenh
Copy link
Collaborator

stevenh commented Nov 29, 2022

Would definitely like to see this.

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

No branches or pull requests

2 participants