Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print cmd timer at end of output #3854

Closed
dxlr8r opened this issue Feb 16, 2017 · 6 comments
Closed

Print cmd timer at end of output #3854

dxlr8r opened this issue Feb 16, 2017 · 6 comments
Labels

Comments

@dxlr8r
Copy link

dxlr8r commented Feb 16, 2017

Hello, for some time I have tried to get the following working:

POC:

function fish_cmd_timer_preexec --on-event fish_preexec
	if test (string length $argv[1]) -gt 1
		set -g fish_cmd_start (date '+%s')
		set_color FFF; date +'%H:%M:%S (%Y-%m-%d)'
	end
end

function fish_cmd_timer_postexec --on-event fish_postexec
	set -g fish_cmd_stop (date '+%s')
	set -g fish_cmd_diff (math $fish_cmd_stop-$fish_cmd_start)
	set_color FFF; echo $fish_cmd_diff
end

This works great unless the command ends with a newline, but if I execute for example:

echo -n 'foobar' the "$fish_cmd_diff" will be printed out on the same line as foobar.

I've tried different methods and workaround (stty -echo, echo -en "\033[6n", commandline -f, etc.) but not found a good solution.

Can anyone help me out? This works in Bash, so I would love for something like this to work in fish as well.

Fish 2.5.0
iTerm2 v3

@terlar
Copy link
Contributor

terlar commented Feb 16, 2017

Not entirely related to the output issue, but is there any reason why you can't use the calculated variable $CMD_DURATION instead of calculating it yourself?

#1585

@dxlr8r
Copy link
Author

dxlr8r commented Feb 16, 2017

Didn't know of that. The issue still stand though, but I'll use that var instead.

@krader1961
Copy link
Contributor

The fish_postexec functions are run before the prompt is displayed so your echo $fish_cmd_diff will write that starting with the current cursor position. If the output of the previous command didn't end with a newline then quite naturally your echo will be on the same line rather than starting in column one of a new line.

You could try to implement the PROMPT_SP hack in your post_exec function that was pioneered by zsh and that fish uses to deal with this in a visually nice fashion. But my recommendation is to add echo $CMD_DURATION to either the fish_prompt or fish_right_prompt function.

@faho
Copy link
Member

faho commented Feb 16, 2017

I second the recommendation to use the prompt for this.

Now, the fish shell project could add something like the PROMPT_SP hack (which, simplified, fills the current line with spaces and then resets the cursor onto the beginning of the next line) also to event-triggered functions, but that would add more code complexity and more noise to fish's output, and at the end you still shouldn't use these functions to output things because the ordering between them isn't defined (and we don't want to get into that business).

In general, event-triggered functions should not be used for user-visible output, but should instead compute things in the background.

@dxlr8r
Copy link
Author

dxlr8r commented Feb 17, 2017

That was one of the things I tried before posting here as well, but that isn't optimal with my terminal iTerm. It has a feature called mark, which will mark the first line of the prompt. Not a big technical problem ofcourse, but it doesn't look to good to mark the previous command's execution time instead of the prompt.

Never heard of "PROMPT_SP" hack, but I'll look into it. But the fish_prompt does print at a new line no matter the output of the last command. So it's not impossible, but I guess that logic isn't easily available in scripts etc. outside the source code and compiled binaries.

@krader1961
Copy link
Contributor

Honestly, if you absolutely have to do it this way I would just include an extra newline: printf "\n%s\n" $CMD_DURATION. Or live with the execution duration sometimes appearing on the last line of output. Things might be different for you but for me it is exceedingly rare that I run a command whose output does not end with a newline. Implementing the PROMPT_SP hack in a fish function is technically possible but is going to be complicated. Personally I couldn't justify the work to do so. Rather than doing that I think you'd be better served by figuring out why simply including some arbitrary text, like $CMD_DURATION, in you prompt is affecting the iTerm2 command prompt mark mechanism in an undesirable way. Good luck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants