How to achieve sth similar?
If I get you right you want to print something without an automatic newline at the end?
You can use: stdout.write x to achieve that.
Python allows large number of variables to be printed on the same line (without line return) Please compare:
var x = "aa"
var y = "bb"
stdout.write(x, " ", y, "\n")
By something like this:
import strutils
var
x = "aa"
y = "bb"
z = "cc"
stdout.write [x,y,z].join " "
import strutils
stdout.write [x, y, z].join(" ")
import std/sugar
var
x = "Hello"
y = 100
z = 42.0
#dump x, y, z # Compile error
dump (x, y, z)
dump x & $y
dump z
Output:
(x, y, z) = ("Hello", 100, 42.0)
x & $y = Hello100
z = 42.0
If you are using Vim/Neovim, it can automatically insert 2 spaces when you press a tab key by setting following options:
set expandtab
set tabstop=2
set shiftwidth=2
Also you can decrease/increase indent with '<'/'>' key in normal mode. If you are not using Vim/Neovim and you think your editor is better than Vim, it should have features that can indent your code without pressing space key so hard.
Sometimes I guess that Nim is sponsored by keyboard manufacturers -- the space bare of my keyboard will soon dye, as tabs are not permitted.
Nah, you just don't know to configure your editor — I just press tab key to insert two indentation spaces. (and four for Python)
And of course, lines following : are auto-indented, no need to press anything to get the correct indentation.
And typing a custom output proc again and again is not that much fun...
You wrapped GTK3 successfully, you are more than capable of writing a tiny support library containing jecho and make it a Nimble package. It could even offer isOdd/isEven.
If you are one of the dozens of packages already needing cligen, then you can just import cligen/prints https://github.com/c-blake/cligen/commit/c4d8b1f6f0d51b9cae50bf931b96ef00261b8bd0 and probably never use anything but print and printn.
If you are not one of those dozens of programs/packages - why not?!!? - just kidding. To each his own. Nim is choice. Copy paste prints.nim to anywhere in your --path if you want.
(I wouldn't be even a little surprised if this FAQ-level set of things exists in 5 other nimble packages already. LOL.)
Incidentally, some might prefer something like:
template joins*(sep=" ", a: varargs[string, `$`]): untyped =
var result: string
for i, x in pairs(a):
if i != 0: result.add sep
result.add $x
result
echo " ".joins(1, 2.0, [3], "four")
or maybe joinstr or joinstring.