question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`print` and `println` instead of `echo` on CliktCommand

See original GitHub issue

After having built some CLI’s lately using the library, I’m wondering if it would be beneficial to just expose print and println methods to mimic printing to the screen directly with System.out or System.err but in scope with the console’s Context instead

Example proposed methods:

protected fun println() = echo("")
protected fun println(message: String) = echo(message)
protected fun println(message: Any?) = echo(message?.toString())

protected fun print(message: String) = echo(message, trailingNewLine = false)
protected fun print(message: Any?) = echo(message?.toString(), trailingNewLine = false)

We could also consider just exposing a out and err PrintStream on the CliktCommand type

class CliktCommand(...) {
    protected val out: PrintStream by lazyPrintStream { echo(it, trailingNewLine = false) }
    protected val err: PrintStream by lazyPrintStream { echo(it, trailingNewLine = false, err = true) }

    ...

    private inline fun lazyPrintStream(output: (String) -> Unit): Lazy<PrintStream> = lazy(LazyThreadSafetyMode.NONE) {
        PrintStream(object : OutputStream() {
            override fun write(b: Int) {
                output(b.toByte().toChar().toString())
            }
        })
    }
}

at which point we could just alias the recommended methods above to go to out by default

protected fun println() = out.println()
protected fun println(message: String) = out.println(message)
protected fun println(message: Any?) = out.println(message?.toString())

protected fun print(message: String) = out.print(message)
protected fun print(message: Any?) = out.print(message?.toString())

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ajaltcommented, Oct 26, 2018

I would normally avoid overriding built in functions, since that leads to unexpected behavior. But in this case, given that echo’s behavior is identical to println’s, but it fixes line ending bugs, I think that I agree with you that this is worth implementing.

I can work on it this weekend.

0reactions
jcornazcommented, Mar 19, 2019

I don’t think it is a good idea. Here is why:

First echo is easy to discover. Either by auto-completion or in the doc. For any library or tool, the best way to discover feature will always be to read the documentation. And in this case, we are talking about a feature which appears in the very first paragraph of the “get started” section and in each example everywhere in the doc. So it is hard to find only if we don’t read the “Hello world” of the technology we are about to use.

Then echo definitely doesn’t do the same as println. It does more.

And, println is the most known function in Kotlin. It is the first function you learn (again reading the “Hello world”), and the one you see the most often when reading articles, examples, and documentation. Every developer knows it and has a strong assumption about how it will behave. No one will think about reading the documentation of println. So providing a different println implementation will just be astonishing and unexpected.

Read more comments on GitHub >

github_iconTop Results From Across the Web

print command instead of echo in linux - Unix Stack Exchange
The command print is for displaying files per the contents of the mailcap file, using the designated "handler" for each particular file type, ......
Read more >
Basic Concepts - Clikt - GitHub Pages
Printing to Stdout and Stderr¶. Why does this example use echo instead of println? Although println works, it can cause problems with multi-platform...
Read more >
CliktCommand - Javadoc.io
Print the message to the screen. This is similar to print or println, but converts newlines to the system line separator. This is...
Read more >
What is the difference between echo and print?
PHP - What is the difference between echo and print? If anyboyd knows, do tell me.waiting for your prompt response.. 46 Answers are...
Read more >
What is the Difference Between Printf and Echo in Bash?
So, we have tried “printf” on the shell first to print out the number of characters in a string “Linux” on our shell....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found