thepyedpiper / pyp

The Pyed Piper: Python Power At the Prompt
13 stars 0 forks source link

Don't add indices and color when stdout is not a tty #4

Open krackers opened 1 year ago

krackers commented 1 year ago

When doing ls | pyp 'pp.sort()' each line of the output is prefixed with the index, and color escape codes are used. However, when the output is further sent to be processed like

ls | pyp 'pp.sort()' | my_cool_cmd

the indices and escape codes are unwanted. This could be worked around by doing pyp 'pp.sort() | p' but it would be cleaner if we only pretty-printed the output if isatty is true.

This can be done by changing

            #update actual output
            for out in user_input:
                traced_output = self.array_tracer(out,analysis_mode, power_pipe) if sys.stdout.isatty() else out
                output_array.append(traced_output) # for output

Arguably the same could be done for list outputs of non power-pipe:

echo 'this is a test' | pyp '[p[0], p[1]]'

where you assume that if the output is a list and stdout is not a tty, then we should try to join the list back together. This can be done via

                if sys.stdout.isatty():
                    traced_output = self.array_tracer(out,analysis_mode, power_pipe)
                elif contains_list:
                    traced_output = ' '.join([str(x) for x in out])

but it probably needs more thought as to the handling of arrays, as you mentioned in #1