murex icon indicating copy to clipboard operation
murex copied to clipboard

printf formatting

Open orefalo opened this issue 2 years ago • 5 comments

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 

orefalo avatar Aug 10 '23 06:08 orefalo

$() 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.

lmorg avatar Aug 10 '23 11:08 lmorg

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?

orefalo avatar Aug 14 '23 14:08 orefalo

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.

lmorg avatar Aug 14 '23 20:08 lmorg

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

orefalo avatar Sep 06 '23 08:09 orefalo

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.

lmorg avatar Sep 06 '23 09:09 lmorg