💾 Archived View for bbs.geminispace.org › u › oldernow › 13097 captured on 2023-12-28 at 16:09:18. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

Comment by ☯️ oldernow

Re: "Thin and to the point"

In: u/oldernow

@stack Here be the Lua scripts. You'll see they're not well documented, but read from stdin and write to stdout, making the perfect to run against lines in a vim session a la:

NOTE: The source code below contains tab characters.

center

Looks like "center" takes a couple optional arguments related to some kind of additional padding.

Let's say you start with this on lines 1 through 4 in a vim session:

this
is a test
to see
what happens

running ":1,4!center" leads to:

    this
 is a test
   to see
what happens

running ":1,4!center @" leads to:

@@@@this@@@@
@is a test@@
@@@to see@@@
what happens

(see how it used argument one as a padding character?)

running ":1,4!center @ x" leads to:

@@@@this
@is a test
@@@to see
what happens

So apparently the second argument (which can be anything) leads to the padding occurring only at the front of lines.

"center" source

#! /usr/bin/env lua
local pad = ' '
if arg[1] then
	pad = arg[1]
end
t = {}
max = 0
for line in io.stdin:lines() do
	line = string.gsub(line, '^%s*', '')
	line = string.gsub(line, '%s*


, '')
	table.insert(t, line)
	if #line > max then max = #line end
end
for i,line in ipairs(t) do
	if #line > 0 then
		for j=1, (max-#line)/2, 1 do
			io.stdout:write(pad)
		end
		if arg[1] then
			io.stdout:write(line)
			if not arg[2] then
				for j=1, (max-#line)/2, 1 do
					io.stdout:write(pad)
				end
				if (max-#line)%2 > 0 then
					io.stdout:write(pad)
				end
			end
			print()
		else
			print(line)
		end
	else
		print(line)
	end
end

box

Starting with this in lines 1 through 4 of a vim session:

    this
 is a test
   to see
what happens

running ":1,4!box" leads to:

================
|     this     |
|  is a test   |
|    to see    |
| what happens |
================

running ":1,4!box x" (i.e. with optional argument that means 'use - instead of = for the top/bottom lines') leads to:

----------------
|     this     |
|  is a test   |
|    to see    |
| what happens |
----------------

Looks like it also uses an optional TAB environment variable for some reason (sorry...), but will also recognize the presence of a "-c" flag, directing it to center text within the box... which I *think* obviates the "center script".

So starting with this in lines 1 through 4 of a vim session:

this
is a test
to see
what happens

running ":1,4!box" leads to:

================
| this         |
| is a test    |
| to see       |
| what happens |
================

running ":1,4!box -c" leads to:

================
|     this     |
|  is a test   |
|    to see    |
| what happens |
================

Yeah, I know... flakey... not documented for sh*t, etc. But I tend to forget what I've written before, generally don't have patience to refactor for reuse, etc.

In other words, I'm no @skyjake. :-)

Good luck!

box source

#! /usr/bin/env lua
local tab = os.getenv('TAB')
if tab then
	if tonumber(tab) then
		tab = string.rep(' ', tonumber(tab))
	end
end
local center = false
for i,v in ipairs(arg) do
	if v == '-c' then
		center = true
		table.remove(arg, i)
	end
end
local lines = {}
local max = 0
for line in io.stdin:lines() do
	if tab then
		line = string.gsub(line, '\t', tab)
	end
	local len = string.len(line)
	if len > max then
		max = len
	end
	table.insert(lines, line)
end
local c = '='
if arg[1] then
	c = '-'
end
print(string.rep(c, max+4))
if center then
	for i,line in ipairs(lines) do
		local front = math.floor((max - string.len(line)) / 2)
		local back = max - string.len(line) - front
		print('| ' .. string.rep(' ', front) .. line .. string.rep(' ', back) .. ' |')
	end
else
	for i,line in ipairs(lines) do
		print('| ' .. line .. string.rep(' ', max - string.len(line)) .. ' |')
	end
end
print(string.rep(c, max+4))

☯️ oldernow

20 hours ago

3 Later Comments ↓

🚀 stack · 20 hours ago:

@oldernow: many thanks. I can't believe there are no simple linux utils to do that! FreeBSD has fmt -c, but it does not work in linux to center...

vim does have a %center command...

There is a linux utility called 'boxes', should be in most repos. It comes with simple ones, and a bunch of ridiculous ASCII-art boxes too...

☯️ oldernow · 17 hours ago:

@stack Oh wow, didn't know about vim's "center", although needing to specify a width or go with an ugly default seems a bit more work than my script, which bases the centering on length of the longest line in a set. But I could easily be not completely understanding vim's "center"

🚀 stack · 10 minutes ago:

After a more careful RTFMing, the boxes utility will actually center text too. It comes with dozens of stupid presets, and you can make your own desings. And it integrates into vim's visual mode nicely. Not to poo-poo your code - I have tons of utilities I wrote that later turn out to be unnecessary as someone already did it in 1982...

— https://boxes.thomasjensen.com/editors-vim.html

Original Post

☯️ oldernow

Thin and to the point — [preformatted]

💬 8 comments · 1 like · 24 hours ago