zsh-users
 help / color / mirror / code / Atom feed
* gnu script and zsh: only log command output
@ 2022-05-12 18:45 chiasa.men
  2022-05-13 19:12 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: chiasa.men @ 2022-05-12 18:45 UTC (permalink / raw)
  To: zsh-users

When I use "script --log-output o --log-input i" I am facing two problems:

1: The output log contains the actual input: e.g. ".]0;test@test: echo 5.5"
(see below)
2: The prompts

I really just want the programs output logged - no prompts, no shell-related
output.

Is that possible?

Actually Im using a python script with "pid, master_fd = pty.fork();
pty._copy(master_fd, _my_read_function, _my_stdin_read_function)"

So would there be a way to reliably filter the zsh-related output e.g. within
the _my_read_function function?

Example logs:
output:
> .[1m.[7m%.[27m.[1m.[0m
>
> .]0;test@test: ~/script.
> .[0m.[27m.[24m.[J.[1m.[31m.[39m.[0m.[1m.[34mtest.[39m.[0m@test .[1m~/script
.[0m% .[K.[?1h.=.[?2004he.echo 5.[?1l.>.[?2004l
>
> .]0;test@test: echo 5.5
> .[1m.[7m%.[27m.[1m.[0m
>
> .]0;test@test: ~/script.
> .[0m.[27m.[24m.[J.[1m.[31m.[39m.[0m.[1m.[34mtest.[39m.[0m@test .[1m~/script
.[0m% .[K.[?1h.=.[?2004he.exit.[?1l.>.[?2004l
>
> .]0;test@test: exit.

input:
> echo 5
> exit







^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: gnu script and zsh: only log command output
  2022-05-12 18:45 gnu script and zsh: only log command output chiasa.men
@ 2022-05-13 19:12 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2022-05-13 19:12 UTC (permalink / raw)
  To: chiasa.men; +Cc: zsh-users

On Thu, May 12, 2022 at 11:46 AM chiasa.men <chiasa.men@web.de> wrote:
>
> When I use "script --log-output o --log-input i" I am facing two problems:
>
> 1: The output log contains the actual input: e.g. ".]0;test@test: echo 5.5"
> (see below)
> 2: The prompts

This is going to happen because "script" has taken over the terminal
device and ZLE must necessarily write to that terminal to display the
prompts and to echo back your keystrokes as it maintains the "editor"
state.  Everything that comes and goes to the terminal is potentially
captured by "script" and there's no way for you to see/interact with
it that bypasses that.  You can try switching off ZLE if you can stand
to work that way, but I think you'd still get the prompts captured.

> I really just want the programs output logged - no prompts, no shell-related
> output.
>
> Is that possible?

It's a bit difficult in zsh up to 5.8.1 ... once 5.9 is released, you
should be able to do something like this:

autoload add-zsh-hook
exec {stdout}>&1 {stderr}>&2
preexec_outputs() {
  exec 2>>|stderr.log 2>&$stderr
  exec 1>>|stdout.log 1>&$stdout
}
precmd_outputs() {
  exec 1>&$stdout 2>&$stderr
}
add-zsh-hook preexec preexec_outputs
add-zsh-hook precmd precmd_outputs

However, this won't capture output of interactive programs like "vim"
which will either fail or will re-open the terminal device directly
(printing an error message first, in the case of vim).

> So would there be a way to reliably filter the zsh-related output e.g. within
> the _my_read_function function?

Using preexec and precmd you could write markers to the output that
would indicate when ZLE becomes active (precmd) and when it finishes
(preexec).  Your python script could then discard anything between
those two markers.  You'd have to be very sure that those markers
would never appear in output you actually want captured.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-05-13 19:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-12 18:45 gnu script and zsh: only log command output chiasa.men
2022-05-13 19:12 ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).