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

$CMD_DURATION Weird format #1585

Closed
giodamelio opened this issue Jul 30, 2014 · 13 comments
Closed

$CMD_DURATION Weird format #1585

giodamelio opened this issue Jul 30, 2014 · 13 comments
Milestone

Comments

@giodamelio
Copy link
Contributor

I am trying to make my prompt notify me when a command that has taken a certain amount of time is done. $CMD_DURATION is awesome for that, except its has a really weird output format, which makes it hard to any math with. This does not appear to be used anywhere else, so why the weird output? Wouldn't it be better to just output it in milliseconds?

@ridiculousfish
Copy link
Member

See #1279 too, which suggests outputting using the real/user/sys triplet.

@giodamelio
Copy link
Contributor Author

That would be great, is there any change of that happening any time soon?

@ridiculousfish
Copy link
Member

Not as such, but go for it if you feel up to it.

@giodamelio
Copy link
Contributor Author

Awesome, my C is a little rusty, but this should be simple enough. Just to be clear, you're fine with me getting rid of all that formatting and just returning the duration in ms?

@ridiculousfish
Copy link
Member

I don't think you'll find many people attached to the current behavior. Start by proposing any reasonable alternatives you can think of, then give your preference and justify it - pros and cons. Focusing on the use cases is good justification. The use case that prompted your interest (doing math) is a good one, and also think about formatting for display. For example, if the user wanted to show the duration of the last command in the prompt, what would the script to do that look like?

Hope that doesn't scare you off. It's not mean to be some big process, just to put a little thought into the design.

@giodamelio
Copy link
Contributor Author

Not a problem. I definitely think setting $CMD_DURATION to the number of milliseconds is the way to go.

Pros

  • Consistent, in Unix systems, time is just a bunch of milliseconds
  • Easy on the math, keeps everything in integers so it is nicely compatible with fish's test command

Cons

  • Not backwards compatible. This should not really be a problem since this is an undocumented and largely unused feature.

Example use. Sends user notification if last command took more then ten seconds to execute.

# If a command has run for more then ten seconds, notify the user when it completes
# Put this somewhere in fish_prompt
if test $CMD_DURATION
    if test $CMD_DURATION -gt (math "1000 * 10")
        set secs (math "$CMD_DURATION / 1000")
        notify-send "$history[1]" "Returned $status, took $secs seconds"
    end
end

@arthurfabre
Copy link

I second the move to using milliseconds only.
My use case is to compare script execution time, and parsing the $CMD_DURATION format is a bit verbose - I've ended up with the following:

# Check if var is set
if set -q CMD_DURATION

    # Parse the duration into an array - and get rid of decimal points and trailing / leading newlines
    set -l time (echo $CMD_DURATION | sed -r 's/\ *([0-9]*)(\.[0-9]*)?[a-z]*/\1\n/g' | tr -d '\n')

    # If the array has more than two entries (ie command took more than 60 seconds), or the only entry (seconds) is > 5
    if begin; test (count $time) -gt 1; or test $time[1] -gt 5; end
        # Command took more than 5 seconds
    end
end

By comparison, converting a value in ms to "formated" time is a lot simpler - it can be accomplished with the following:

date -u -d @$CMD_DURATION "+%kh %Mm %Ss"

This also allows users to choose their own format.

@giodamelio
Copy link
Contributor Author

I am having a bit of trouble implementing this(I don't know much c++). When I change out set_env_cmd_duration with this:

void set_env_cmd_duration(struct timeval *after, struct timeval *before)
{
    time_t secs = after->tv_sec - before->tv_sec;
    suseconds_t usecs = after->tv_usec - before->tv_usec;
    wchar_t buf[16];

    if (after->tv_usec < before->tv_usec)
    {
        usecs += 1000000;
        secs -= 1;
    }

    swprintf(buf, sizeof(buf), L"%d", usecs);
    env_set(ENV_CMD_DURATION, buf, ENV_EXPORT);
}

I expect it to set $CMD_DURATION to the number of microseconds that is took the command to run(see man 2 gettimeofday). It is not. If I run sleep 2, it will output number anywhere from about 2500-6500. I have to clue what is going on.

@femnad
Copy link
Contributor

femnad commented Aug 1, 2014

usecs are in microseconds, namely 1 millionth of a second. Also you are ignoring the seconds part of the time difference. Lastly, the size information you provide to swprintf is not correct. You should provide the whole size of the memory to allocate, which would be equal to the size of 16 chars in your case.

@giodamelio
Copy link
Contributor Author

Ah, it both added together, thanks.

@giodamelio
Copy link
Contributor Author

It still doesn't make sense to me. If I swap in

swprintf(buf, 16, L"%d|%d", (secs / 1000), (usecs * 1000));

and run sleep 2, it will output something like 0|5137000, that is way too many milliseconds.

@femnad
Copy link
Contributor

femnad commented Aug 1, 2014

I think you should multiply secs and divide usecs if you want the result in milliseconds. But the format string you've selected would produce something like '2000|5' in that case, if you ran sleep 2. Is that what you'd like?

@giodamelio
Copy link
Contributor Author

Ugg, I can't believe I reversed those. Also, the formatting was just for debugging. Pull request incoming.

giodamelio added a commit to giodamelio/fish-shell that referenced this issue Aug 1, 2014
giodamelio added a commit to giodamelio/fish-shell that referenced this issue Aug 4, 2014
@zanchey zanchey closed this as completed in bcda3f1 Aug 4, 2014
@zanchey zanchey added this to the fish 2.2.0 milestone May 1, 2015
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants