There’s this layout library in C called clay which is basically a renderer agnostic flex box style layout engine. You might be interested in reading its source!
Yeah, there's a few such libraries that I'm aware of but I haven't had time to evaluate them. I do plan to at least look into them and make decision from there.
nucleic/kiwi uses the same algorithm that autolayout uses. It's also a tried and true implementation I've used many times, including in console environments.