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

wide_msg can't be used with wide_bar #328

Closed
sigmaSd opened this issue Dec 8, 2021 · 16 comments · Fixed by #329
Closed

wide_msg can't be used with wide_bar #328

sigmaSd opened this issue Dec 8, 2021 · 16 comments · Fixed by #329

Comments

@sigmaSd
Copy link
Contributor

sigmaSd commented Dec 8, 2021

Here is an example of the issue ouch-org/ouch#214

I think this line https://github.com/mitsuhiko/indicatif/blob/c43d7b3d8789027835771888310f2a1416665de1/src/style.rs#L293 only expects one wide element

If this is an intended behavior, I can open a pr to document this

@djc
Copy link
Collaborator

djc commented Dec 8, 2021

Yes, that is intended behavior. PR for doc improvements welcome!

@sigmaSd
Copy link
Contributor Author

sigmaSd commented Dec 9, 2021

What do you think about:

pub const fn check_template(s: &str) {
    let s = s.as_bytes();
    let mut i = 0;
    let mut n_wide = 0;
    while i + 9 < s.len() {
        match [
            &s[i],
            &s[i + 1],
            &s[i + 2],
            &s[i + 3],
            &s[i + 4],
            &s[i + 5],
            &s[i + 6],
            &s[i + 7],
            &s[i + 8],
            &s[i + 9],
        ] {
            [&b'{', &b'w', &b'i', &b'd', &b'e', &b'_', &b'm', &b's', &b'g', &b'}']
            | [&b'{', &b'w', &b'i', &b'd', &b'e', &b'_', &b'b', &b'a', &b'r', &b'}'] => {
                n_wide += 1;
                if n_wide == 2 {
                    panic!(
                        "{}",
                        "Only one wide element can be used in the progress bar."
                    );
                }
            }
            _ => (),
        }
        i += 1;
    }
}
pub struct Template(&'static str);
impl Template {
    const fn new(msg: &'static str) -> Self {
        check_template(msg);
        Self(msg)
    }
}

To test:
const _: Template = Template::new("{wide_msg} qsd qsd sq {wide_msg}");

Maybe its a bit overboard, but I thought it was cool 'that it is possible' if you force the user to provide the Template struct it can do some checks at compile time.

@sigmaSd
Copy link
Contributor Author

sigmaSd commented Dec 9, 2021

It can be even more powerful:

        const fn check_template(string: &str) {
            let string = string.as_bytes();
            let mut string_index = 0;
            let mut n_wide = 0;

            let mut tag_start = false;
            let mut tag_buffer = [0; 30];
            let mut tag_index = 0;
            while string_index < string.len() {
                match &string[string_index] {
                    &b'{' => tag_start = true,
                    &b'}' => {
                        match tag_buffer {
                            [b'w', b'i', b'd', b'e', b'_', b'b', b'a', b'r', ..]
                            | [b'w', b'i', b'd', b'e', b'_', b'm', b's', b'g', ..] => {
                                n_wide += 1;
                                if n_wide > 1 {
                                    panic!("Only one wide element is allowed per template");
                                }
                            }
                            [b'b', b'a', b'r', ..]
                            | [b's', b'p', b'i', b'n', b'n', b'e', b'r', ..]
                            | [b'p', b'r', b'e', b'f', b'i', b'x', ..]
                            | [b'm', b's', b'g', ..]
                            | [b'p', b'o', b's', ..]
                            | [b'l', b'e', b'n', ..]
                            | [b'b', b'y', b't', b'e', b's', ..]
                            | [b'p', b'e', b'r', b'c', b'e', b'n', b't', ..]
                            | [b't', b'o', b't', b'a', b'l', b'_', b'b', b'y', b't', b'e', b's', ..]
                            | [b'e', b'l', b'a', b'p', b's', b'e', b'd', b'_', b'p', b'r', b'e', b'c', b'i', b's', b'e', ..]
                            | [b'e', b'l', b'a', b'p', b's', b'e', b'd', ..]
                            | [b'p', b'e', b'r', b'_', b's', b'e', b'c', ..]
                            | [b'b', b'y', b't', b'e', b's', b'_', b'p', b'e', b'r', b'_', b's', b'e', b'c', ..]
                            | [b'b', b'i', b'n', b'a', b'r', b'y', b'_', b'b', b'y', b't', b'e', b's', b'_', b'p', b'e', b'r', b'_', b's', b'e', b'c', ..]
                            | [b'e', b't', b'a', b'_', b'p', b'r', b'e', b'c', b'i', b's', b'e', ..]
                            | [b'e', b't', b'a', ..]
                            | [b'd', b'u', b'r', b'a', b't', b'i', b'o', b'n', b'_', b'p', b'r', b'e', b'c', b'i', b's', b'e', ..]
                            | [b'd', b'u', b'r', b'a', b't', b'i', b'o', b'n', ..] => (),
                            _ => panic!("Unrecognized tag"),
                        }

                        tag_buffer = [0; 30];
                        tag_index = 0;
                        tag_start = false;
                    }
                    c => {
                        if tag_start {
                            tag_buffer[tag_index] = *c;
                            tag_index += 1
                        }
                    }
                }
                string_index += 1;
            }
        }

@djc
Copy link
Collaborator

djc commented Dec 9, 2021

Well, I think I should merge #319 first, then it becomes pretty natural...

@sigmaSd
Copy link
Contributor Author

sigmaSd commented Dec 10, 2021

I just found this as an opportunity to try how far can const in rust go, here is one more version:

        const fn check_template(string: &str) {
            let string = string.as_bytes();
            let mut string_index = 0;
            let mut n_wide = 0;

            let mut tag_start = false;
            let mut tag_buffer = [0; 30];
            let mut tag_index = 0;
            while string_index < string.len() {
                match &string[string_index] {
                    &b'{' => tag_start = true,
                    &b'}' => {
                        if !matches!(tag_buffer.first(), Some(0)) {
                            let mut actual_tag = tag_buffer.as_slice();
                            // Remove trailing zeroes
                            loop {
                                match actual_tag.split_last() {
                                    Some((a, b)) => {
                                        if *a == 0 {
                                            actual_tag = b;
                                            continue;
                                        }
                                    }
                                    _ => (),
                                };
                                break;
                            }
                            match actual_tag {
                                b"wide_bar" | b"wide_msg" => {
                                    n_wide += 1;
                                    if n_wide > 1 {
                                        panic!("Only one wide element is allowed per template");
                                    }
                                }
                                b"bar"
                                | b"spinner"
                                | b"prefix"
                                | b"msg"
                                | b"pos"
                                | b"len"
                                | b"bytes"
                                | b"percent"
                                | b"total_bytes"
                                | b"elapsed_precise"
                                | b"elapsed"
                                | b"per_sec"
                                | b"bytes_per_sec"
                                | b"binary_bytes_per_sec"
                                | b"eta_precise"
                                | b"eta"
                                | b"duration_precise"
                                | b"duration" => (),
                                _ => panic!("Unrecognized tag"),
                            }
                        }

                        tag_buffer = [0; 30];
                        tag_index = 0;
                        tag_start = false;
                    }
                    c => {
                        if tag_start {
                            tag_buffer[tag_index] = *c;
                            tag_index += 1
                        }
                    }
                }
                string_index += 1;
            }
        }

@djc
Copy link
Collaborator

djc commented Dec 10, 2021

That stuff only works on nightly, right?

@sigmaSd
Copy link
Contributor Author

sigmaSd commented Dec 10, 2021

Its on stable! (panic on compile time was just stabilized)
rustc 1.57.0 (f1edd0429 2021-11-29)

@djc
Copy link
Collaborator

djc commented Dec 10, 2021

Well, want to see if you can make my parser in #319 const?

@sigmaSd
Copy link
Contributor Author

sigmaSd commented Dec 10, 2021

Ill check it out, I doubt its possible though

I think its only possible with the konst crate, but it might hurt compile time

Actually I feel like this is interesting I will see if I can add it with konst under an optional compile time flag

@djc
Copy link
Collaborator

djc commented Dec 10, 2021

I don't think I want to add the konst crate as a dependency.

@sigmaSd
Copy link
Contributor Author

sigmaSd commented Dec 10, 2021

Here is a try (stable rust no dep): https://github.com/sigmaSd/konster/blob/master/src/main.rs

Running

const Q: Template = Template::from_str(r#"{ "foo": "{foo}", "bar": {bar} }"#);
const R: Template = Template::from_str("{foo:^54.red.on_blue/green.on_cyan}");
#[test]
fn a() {
    dbg!(Q);
    dbg!(R);
}
[examples/indicatif.rs:11] Q = Template {
    parts: CKVec {
        inner: [
            Literal(
                { ,
            ),
            Literal(
                "foo": ",
            ),
            Placeholder {
                key: foo,
                align: Left,
                width: None,
                truncate: false,
                style: None,
                alt_style: None,
            },
            Literal(
                ", "bar": ,
            ),
            Placeholder {
                key: bar,
                align: Left,
                width: None,
                truncate: false,
                style: None,
                alt_style: None,
            },
            Literal(
                 },
            ),
        ],
    },
}
[examples/indicatif.rs:12] R = Template {
    parts: CKVec {
        inner: [
            Placeholder {
                key: foo,
                align: Center,
                width: Some(
                    54,
                ),
                truncate: false,
                style: Some(
                    Style {
                        fg: Some(
                            Red,
                        ),
                        bg: Some(
                            Blue,
                        ),
                        fg_bright: false,
                        bg_bright: false,
                        attrs: CKSet {
                            set: KSet {
                                vec: KVec {
                                    inner: [],
                                },
                            },
                        },
                        force: None,
                        for_stderr: false,
                    },
                ),
                alt_style: Some(
                    Style {
                        fg: Some(
                            Green,
                        ),
                        bg: Some(
                            Cyan,
                        ),
                        fg_bright: false,
                        bg_bright: false,
                        attrs: CKSet {
                            set: KSet {
                                vec: KVec {
                                    inner: [],
                                },
                            },
                        },
                        force: None,
                        for_stderr: false,
                    },
                ),
            },
        ],
    },
}



@sigmaSd
Copy link
Contributor Author

sigmaSd commented Dec 10, 2021

updated link: https://github.com/sigmaSd/konster/blob/master/src/lib.rs (updated the above test results as well)

@sigmaSd
Copy link
Contributor Author

sigmaSd commented Dec 11, 2021

Update link: https://github.com/sigmaSd/konster/blob/master/examples/indicatif.rs (also updated the test results above)

@djc
Copy link
Collaborator

djc commented Dec 15, 2021

This is very impressive work, thanks for demonstrating this! I don't think it makes sense to merge this now, as I think being const adds a whole bunch of complexity and substantially bumps the MSRV while the benefit ends up being minor.

If you want to submit a PR that changes the template() method on ProgressStyle to take a Template (I've since merged #319), make Template::from_str() fallible and make it fail when both wide_ variables are used, that might be a good solution here?

@sigmaSd
Copy link
Contributor Author

sigmaSd commented Dec 15, 2021

Thanks! This is more of a demo of what might be possible in futures releases hopefully.

I don't this this is really worth breaking the api, if you think its a good idea I can add it of-course.

@djc
Copy link
Collaborator

djc commented Dec 15, 2021

Well, the next release is an API-breaking release anyway, but might still not be worth breaking this particular API. I'm probably inclined to agree that it isn't worth it on its own.

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 a pull request may close this issue.

2 participants