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

Implement seq (#45) #63

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft

Conversation

FrancisMurillo
Copy link

@FrancisMurillo FrancisMurillo commented Oct 1, 2019

Implement seq.

Drafted the PR to show progress and any possible early reviews if needed.

TODO:

  • Unit tests
    • NaN ranges
  • Support other seq options
    • --format
    • --separator
    • --equal-width
  • Squash and rebase

@@ -11,7 +11,7 @@ fn main() {
Some(values) => values,
None => {
eprintln!("sleep: Missing operand.\nTry 'sleep --help' for more information.");
process::exit(1);
process::exit(1);2
Copy link
Owner

Choose a reason for hiding this comment

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

Be careful with this, remember to check compiling for the whole workspace

Copy link
Author

Choose a reason for hiding this comment

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

Okay... sorry about that. Must have commited that by accident.

@GrayJack
Copy link
Owner

GrayJack commented Oct 1, 2019

For next commits follow the git commit message guideline please

seq/src/seq.yml Outdated
[OPTIONS] <FIRST> <INCREMENT> <LAST>
args:
- FIRST:
help: >
Copy link
Owner

Choose a reason for hiding this comment

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

When possible, use single line string

Copy link
Author

Choose a reason for hiding this comment

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

Alright, will apply that to the remaining single help values

seq/src/main.rs Outdated

fn next(&mut self) -> Option<Self::Item> {
if self.step.is_sign_positive() && self.current <= self.stop {
let result = self.current.clone();
Copy link
Owner

Choose a reason for hiding this comment

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

f64 is Copy, so you don't need to clone

Copy link
Author

Choose a reason for hiding this comment

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

Understood

seq/src/main.rs Outdated
self.current += self.step;

Some(result)
} else if self.step.is_sign_negative() && self.current >= self.stop
Copy link
Owner

Choose a reason for hiding this comment

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

The logic of the inside this else if is the same as the first if logic, it's that right? If so, you could just use logical OR (||) on the first if and remove this else if

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, I refactored this to be one if condition since they are the same.

seq/src/main.rs Outdated

use clap::{load_yaml, App, AppSettings};

#[derive(Clone,Debug)]
Copy link
Owner

Choose a reason for hiding this comment

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

Since they are all small Copy types, you could also derive Copy

Copy link
Author

Choose a reason for hiding this comment

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

Okie dokie

@FrancisMurillo
Copy link
Author

FrancisMurillo commented Oct 2, 2019

Running cargo run --bin seq 10 -1.001 1 gives me this output:

10
8.999
7.998000000000001
6.997000000000002
5.996000000000002
4.995000000000003
3.994000000000003
2.993000000000003
1.992000000000003

Which is expected when creating floating point sequences because of how floating point numbers are added or subtracted. To address this precision loss, I'm planning to use rust_decimal instead of f64 in the iteration step. Is it valid to import a dependency or no dependencies at all?

@FrancisMurilloDigix
Copy link

As part of the PR, I have to run the rustfmt. Running cargo fmt, it keeps giving me an error:

> cargo fmt -p seq
Could not parse TOML: expected an equals, found a newline at line 3

So checking out line 3 of rustfmt.toml, it has some trouble with the following lines:

...
# Could not parse TOML: expected an equals, found a newline at line 3
make_backup
...
# Could not parse TOML: duplicate key: `wrap_comments`
wrap_comments = true
# Could not parse TOML: invalid escape character in string: `y` at line 54
license_template_path = "Copyright {\y} Eric Shimizu Karbstein."
,,,
# invalid type: integer `2018`, expected string for key `edition`
edition = 2018

Commenting those lines, produces an error:

Warning: can't set `skip_children = false`, unstable features are only available in nightly channel.

I was able to make it run with cargo +nightly fmt. Is that valid or should I do something else to use rustfmt?

@GrayJack
Copy link
Owner

GrayJack commented Oct 2, 2019

I think it's ok to use nightly just for formatting

Also, I'm ok with adding crates when makes sense, and your use makes sense, but try to keep on a minimum

@FrancisMurilloDigix
Copy link

FrancisMurilloDigix commented Oct 8, 2019

@GrayJack I'm at a crossroad. Currently looking at implementing --format and --equal-width which is simply the printf functionality. My initial tactic was to use parse the printf format and convert it into print! format. However, Rust does not support dynamic print! formats, it must be a static string.

I came across runtime-fmt that may be a solution but requires nightly so out of the question. So another alternative is to use printf directly via libc. The tactic is to convert rust_decimal to f32 to c_double and use the format accordingly. I am conflicted whether this solution is the right direction. Another idea is to reimplement printf formatting for f32with pure Rust but might take longer.

Any suggested direction or idea for my dilemma?

@GrayJack
Copy link
Owner

GrayJack commented Oct 8, 2019

Still, using printf from c would require unsafe, so I don't think you should use them
In the echo code, you will find a good part of formatting there, missing only \uHHHH, \UHHHHHHHH, %%, %b and %q

@FrancisMurilloDigix
Copy link

So I should parse the printf format and implement the display myself? Should be fun.

Using this as a reference for the format

@GrayJack
Copy link
Owner

GrayJack commented Oct 8, 2019

Yeah, I think at one point it will be in the coreutil_core, since echo, seq and printf will use that. but for now it's ok to be a copy&paste plus additions

@FrancisMurilloDigix
Copy link

Sorry I did not understand, what do you mean by "copy&paste plus additions"?

@GrayJack
Copy link
Owner

GrayJack commented Oct 8, 2019

Copy and paste the formatting code from echo, add the cases that are missing (additions)

bors bot added a commit that referenced this pull request Oct 5, 2021
150: Seq: Add --terminator flag r=GrayJack a=reastyn

Issue #63 

Co-authored-by: Tomas Rokos <[email protected]>
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.

3 participants