When running commands interactively at the tclsh command line, is there a way to truncate how much of a return value gets printed to stdout?
For example, this will take a very long time because the return value will print to stdout.
tclsh> set a [lrepeat 500000000 x]
I know I can add a dummy command in the same line, but this is an ad hoc solution. Is there some I could set in my ~/.tclshrc to truncate stdout to a finite length?
tclsh> set a [lrepeat 500000000 x] ; puts ""
Maybe this is an XY-problem (as turning off or swallowing prints to
stdoutseems to satisfy the OP), but the actual question was:You can use an interceptor on
stdout(and/ or,stderr) to cap strings to a default limit:Using
chan pushandchan popyou may turn on/off capping to, e.g., 30 characters:Some remarks:
You can use an object, a namespace, or a proc offering the required interface of channel interceptors (initialize, write, ...); I prefer objects.
Ad
write: You want to cap based on a character-based limit, not a byte-level one. However,writereceives a string of bytes, not a string of characters. So, you need to be careful when enforcing the limit (back-transform the byte string into a char string, and vice versa, usingencoding convertfromandencoding convertto).Similar, whether certain values of
maxmight not be a good choice or the value range should be restricted. E.g., amaxof 1 or 0 will turn off the basic REPL (the prompt%), effectively.As for
tclshrc: You may want place the interceptor definition andchan pushcall therein, to enable capping per default?