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

watch: implement interval #48

Merged
merged 5 commits into from
Apr 1, 2024
Merged

watch: implement interval #48

merged 5 commits into from
Apr 1, 2024

Conversation

maxer137
Copy link
Contributor

@maxer137 maxer137 commented Apr 1, 2024

This merge aims to add an interval parsing implementation for watch.

      -n, --interval seconds
              Specify update interval.  The command will not allow quicker than 0.1 second inter-
              val,  in  which the smaller values are converted. Both '.' and ',' work for any lo-
              cales. The WATCH_INTERVAL environment can be used to persistently set a non-default
              interval (following the same rules and formatting).

In watch.rs, a new function named parse_interval is added, which takes in a &str and returns a result with Duration or an error with ParseIntError.

The function will first see if the string contains either a '.' or a ',' character. If it does not, it attempts to parse it into seconds and return a duration with that many seconds.
Otherwise, it will attempt to split the string into the seconds and the remainder.
The remainder is then parsed and zero padded. This way we ensure 0.5 becomes 500000000 nanoseconds, and not 5.
If the string has a missing component (such as .5 or 5.) the function will assume 0.
Nanosecond strings that except 9 characters will first be checked if they are valid, then trimmed.
Finally, return the calculated duration or 100 milliseconds. Whichever is longer. This ensures the minimum interval of 0.1 seconds.

Other thoughts:

  1. Currently, I have used "a".parse::<u8>()? to get rust to return a ParseIntError. This is obviously not ideal, but it seems ParseIntError is private to rust, and we'd need to make a custom return type instead. This is possible, but it seemed overkill for this scenario.
  2. There may be a more efficient calculation for padding the nanoseconds. I used 10u32.pow() but maybe a while loops would be more efficient.
  3. The WATCH_INTERVAL environment variable has not been implemented yet.

Copy link

codecov bot commented Apr 1, 2024

Codecov Report

Attention: Patch coverage is 49.12281% with 29 lines in your changes are missing coverage. Please review.

Project coverage is 49.22%. Comparing base (c5e7417) to head (7e93abd).
Report is 2 commits behind head on main.

Files Patch % Lines
src/uu/watch/src/watch.rs 16.12% 22 Missing and 4 partials ⚠️
tests/by-util/test_watch.rs 88.46% 0 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #48      +/-   ##
==========================================
+ Coverage   45.53%   49.22%   +3.68%     
==========================================
  Files          15       15              
  Lines        1803     1804       +1     
  Branches      240      244       +4     
==========================================
+ Hits          821      888      +67     
+ Misses        853      776      -77     
- Partials      129      140      +11     
Flag Coverage Δ
macos_latest ?
ubuntu_latest 49.22% <49.12%> (+3.81%) ⬆️
windows_latest ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

#[test]
fn test_invalid_interval() {
let args = vec!["-n", "definitely-not-valid", "true"];
new_ucmd!().args(&args).fails();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please check stderr too here?
(at least with .contains, doesn't have to be a full match)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in latest commit

@sylvestre sylvestre merged commit 77143e5 into uutils:main Apr 1, 2024
11 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants