It's not too difficult, lines have enough words that there's always something you can change. After a while, as you approach the right margin, you can feel whether the rest of the sentence will fit, and you rephrase it as you write.
I see OP more as a feat of tenacity than of brilliance.