So with the width of your given text box space you could only display that last number of characters that fill the space.
That's what I do now. It has problems when the cursor is not in that section of the input.
Maybe you need a variable which points to which character the current cursor is at (its position).
I have one, or I wouldn't have asked how to do it. I'm still not sure exactly what I should be aiming for. Do I want to try to keep the cursor as close to the middle of the inputbox as possible? Do I want to keep it a certain number of characters from the end? A certain number of characters from the beginning? There are a hundred and one variations still.