Text Formatting in i7

Is there an easy way to say ‘This block of text should be 30 characters long’, padding it with spaces until it hits 30, or chopping off the extra down to 30 so that you can format your output?

Not easily, but this works:

To decide what indexed text is (orig - text) formatted to (len - number) characters:
	let T be an indexed text;
	now T is orig;
	let N be the number of characters in T;
	if N < len:
		repeat with I running from ( N + 1 ) to len:
			replace the regular expression "$" in T with " ";
	else if N > len:
		replace the regular expression ".{[N - len}$" in T with "";
	decide on T.

Ingenious! Many thanks.

Fixed typo in above.

Here’s another solution without regular expressions:

To say (txt - indexed text) in (width - number) characters:
	let L be the number of characters in txt;
	if L < width:
		repeat with X running from L + 1 to width:
			say " ";
		say txt;
	otherwise:
		repeat with X running from 1 to width:
			say character number X in txt.

This is faster than my first try:

To say row of (N - number) spaces: (- spaces {N}; -).

To decide what indexed text is (orig - text) formatted to (len - number) characters:
	let T be an indexed text;
	now T is orig;
	let N be the number of characters in T;
	if N < len:
		let temp be number understood;
		now number understood is len - N;
		let sub be "[row of number understood spaces]";
		replace the regular expression "$" in T with sub;
		now number understood is temp;
	else if N > len:
		replace the regular expression ".{[N - len]}$" in T with "";
	decide on T.

…Juhana’s is much faster than mine, though, particularly if you make use of my “row of N spaces” hack.

Hrm, this appears to destroy hyperlinks and formatting of any kind in the text.

Correct. Inform has no way to represent formatting in a way that can be manipulated like this.

I am saddened. This is much less useful if I can’t keep the formatting.

The only thing that needs indexed text (which is what breaks the formatting) is when you cut the text to fit the size. If you know that the text is smaller than the limit, you can just count the number of spaces you need and print them before the text (keeping the formatting).

I think I figured out how to make this work. If I just apply the width checking to pieces of the text outside the formatted spots, it’s all good.