How can I display single spaced, multi-line text in Pluto?

Dear Pluto developers and users, I know that with Pluto, “print” goes to console, and that the (best?) way to get text displayed is to have cell result as markdown.
I wanted to have “versioninfo()” displayed, which prints only, so I cooked the following :slight_smile: (originally without the commented “replace” line !)

begin
	iob=IOBuffer()
	versioninfo(iob)
	vinfo=read(seekstart(iob), String)
	# vinfo=replace(vinfo, "\n" => "\n\n")
	vmd=Markdown.parse(vinfo)
end

Then I got all concatened text :

Julia Version 1.5.0 Commit 96786e22cc (2020-08-01 23:44 UTC) Platform Info: OS: Windows (x8664-w64-mingw32) CPU: AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx WORDSIZE: 64 LIBM: libopenlibm LLVM: libLLVM-9.0.1 (ORCJIT, znver1) Environment: JULIA_EDITOR = vim

If I uncomment the “replace” line, then I correctly get line breaks, but, two of them for each line !
Does somebody know I could get single spaced consecutive lines ?

Thanks ,
JDAD

3 Likes

I’m guessing, but how about…

vmd=Markdown.parse("```\n$(vinfo)\n```")

Not exactly elegant, but it does what you want. Ideally, you’d be able to insert <br> for this, but Julia’s markdown parser isn’t a superset of html like standard markdown is.

let io = IOBuffer()
	versioninfo(io)
	seekstart(io)
	vinfo = mapreduce(l -> "- " * l, *, eachline(io; keep=true))
	Markdown.parse(vinfo)
end

Alternatively instead of making it an unsorted list, you could wrap it in triple backticks or indent it by 4 spaces to indicate the text is pre-formatted. For some usecases involving preformatted text, this has the downside of generally being displayed in a fixed-width font.

let io = IOBuffer()
	write(io, "```\n")
	versioninfo(io)
	write(io, "\n```")
	seekstart(io)
	Markdown.parse(io)
end

Ideally you wouldn’t have to resort to hacks like this and Pluto would give you some way of a cell outputting plain, unformatted text. If there really isn’t a way of doing this, I think it’d be worthwhile to open an issue for it. EDIT: submitted https://github.com/fonsp/Pluto.jl/issues/264.

Hey! You can use this combination of magic spell words:

Text(sprint(versioninfo))

I believe that sprint does exactly the hack that you also invented (check the docs), and Text is a wrapper around str, such that when you show it, it writes the original characters, not their escape codes.

8 Likes

### A Pluto.jl notebook ###
# v0.11.4

using Markdown
using InteractiveUtils

# ╔═╡ 45a0f8b8-d948-11ea-0691-0b4b411a2674
md"`sprint` works by giving an `IOBuffer` as the first argument of the function that you gave. This only works if the function takes an `IO` as its first argument. 

The `Pkg` functions take `IO` as an optional keyword argument, so we have to do another trick:"

# ╔═╡ 8240a3aa-d947-11ea-083b-858ef1451c4e
Text(sprint(versioninfo))

# ╔═╡ 3409c97e-d948-11ea-3536-5999e4418ec9
Text(sprint(dump, [1,2,[3]]))

# ╔═╡ 81be742e-d948-11ea-2c29-b93dcb78b33f
import Pkg

# ╔═╡ 703a3256-d948-11ea-08de-0bbd3edecea6
Text(sprint(io -> Pkg.status(io=io)))

# ╔═╡ Cell order:
# ╠═8240a3aa-d947-11ea-083b-858ef1451c4e
# ╠═3409c97e-d948-11ea-3536-5999e4418ec9
# ╟─45a0f8b8-d948-11ea-0691-0b4b411a2674
# ╠═81be742e-d948-11ea-2c29-b93dcb78b33f
# ╠═703a3256-d948-11ea-08de-0bbd3edecea6

2 Likes

Dear @non-Jedi, @lyonsquark, many thanks for your help, that also gave me useful hints to possible other uses in my future programmings (Markdown and IOBuffer ones).

Dear @fonsp, I am honored by your detailed answer ! And, it is a very clean solution that works perfectly. Also, I didnt know of “sprint(function)”, so I am really enlighted now.

Could I suggest that in some future (when you have time/ if it suits you) you put this tip in your FAQ, for future reference to other newbies ?

Thank you again for your help and the wonderful “Pluto” work !

JDAD

3 Likes