lmorg / murex

A smarter shell and scripting environment with advanced features designed for usability, safety and productivity (eg smarter DevOps tooling)
https://murex.rocks
GNU General Public License v2.0
1.49k stars 27 forks source link

printf formatting #678

Open orefalo opened 1 year ago

orefalo commented 1 year ago

Describe the problem: Ability to format strings with variables.

While trying to port some of my scripts, I am seeing limitations with the out built-in In particular, the ability to output a value and string, without a space delimiter, and the formatting.

for instance

~ » a=55.33
~ » echo $aGb
Error in `echo` (0,1): variable 'aGb' does not exist
                     > Expression: echo $aGb
                     >           :         ^
                     > Character : 8
~ » echo $a Gb
55.33 Gb
~ » echo ${a}Gb
Gb
~ » echo $(a)Gb
55.33Gb

This last one worked, at the expense of a subshell.

Formatting, allows for instance to strip the .33 from the 55; or to display the value as hex, fp..etc

Maybe the implementation of a builtin similar to fish would be beneficial.

Additional context:

Fish has the printf built-in, https://fishshell.com/docs/current/cmds/printf.html

for instance

printf 'Mem:          %6.2fGb %6.2fGb %6.2fGb %6.2fGb %6.2fGb %6.2fGb\n' $total_mem $used $free $panon 
lmorg commented 1 year ago

$() doesn't create a subshell. In this instance your echo $(a)Gb code is correct.

Regarding printf, as it happens this is something I'm planning on adding at some point too. The main reason I've held off it thus far is because I suspect it might be a pretty big piece of work and printf is already available as an external executable (in fact it is a required part of POSIX https://en.wikipedia.org/wiki/List_of_Unix_commands) so it's only Windows systems that would be unsupported. This means when I do embark on a printf builtin, I have to ensure that it is at least as functional as GNU and BSD printf otherwise the builtin would become an anti-feature for most people.

orefalo commented 1 year ago

working on the doc, I found this hidden command:

out nono -> sprintf "%10s"
murex » builtin sprintf
Error in `fexec` (0,1): no builtin exists with the name `sprintf`

for some reason it is not recognized as a builtin.

mind sharing more details about it? I would solve the formatting issues I was trying to resolve originally. worth adding to the documentation?

lmorg commented 1 year ago

It's a very old function and only available on POSIX. I've been meaning to deprecate it to be honest because it's not that useful and the murex printf builtin, whenever it gets written, would absorb the one useful aspect of it (to read from STDIN)

if you type sprintf into the terminal then press [F1] you'll see the code behind it.

orefalo commented 1 year ago

this may help, I found an implementation in golang https://github.com/moxtsuan/printf

lmorg commented 1 year ago

Unfortunately there isn't a license file on there. Plus the project doesn't appear to be maintained.

I've had a thought about printf in Murex though, it might not be a big a piece of work as I first assumed. In fact I've already started prototyping some code on it.