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

feat: qual of life improvements for pubkey display #164

Merged
merged 1 commit into from Feb 7, 2023
Merged

feat: qual of life improvements for pubkey display #164

merged 1 commit into from Feb 7, 2023

Conversation

CjS77
Copy link
Contributor

@CjS77 CjS77 commented Feb 7, 2023

The Formatter used in the rust Display trait has several lesser-known features that can be leveraged for making displaying public keys much nicer:

Given some public key instance, pk, equal to
e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76,

  1. truncated Width formating

format!("{pk:16}") gives you e2f2ae...08d2d76

  1. Padding and fills Widths > 64 characters let you pad and fill:

format!("{pk:*<66}") gives
e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76**

  1. Implementing lowerHex and UpperHex traits

Displaying a public key writes the lower hex by default, but you can now specify the hex format explicitly, including using the expected alternate forms:

assert_eq!(
            format!("{pk:#x}"),
            "0xe2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76"
        );
        assert_eq!(
            format!("{pk:x}"),
            "e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76"
        );
        assert_eq!(
            format!("{pk:#X}"),
            "0xE2F2AE0A6ABC4E71A884A961C500515F58E30B6AA582DD8DB6A65945E08D2D76"
        );
        assert_eq!(
            format!("{pk:X}"),
            "E2F2AE0A6ABC4E71A884A961C500515F58E30B6AA582DD8DB6A65945E08D2D76"
        );
  1. Combined formats

And of course, all these format specifiers can be combined:

assert_eq!(
            format!("{pk:*^#68X}"),
            "*0xE2F2AE0A6ABC4E71A884A961C500515F58E30B6AA582DD8DB6A65945E08D2D76*"
        );

The rust `Display` trait has several lesser know features that can be
leveraged for making displaying public keys much nicer:

Given some public key instance, `pk`, equal to
`e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76`,

1. truncated Width formating

`format!("{pk:16}")` gives you `e2f2ae...08d2d76`

2. Padding and fills
Widths > 64 characters let you pad and fill:

 `format!("{pk:*<66}")` gives
`e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76**`

3. Implementing lowerHex and UpperHex traits

Displaying a public key writes the lower hex by default, but you can now
specify the hex format explicitly (including using the [expected
alternate forms](https://doc.rust-lang.org/std/fmt/trait.LowerHex.html):

```rust
assert_eq!(
            format!("{pk:#x}"),
            "0xe2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76"
        );
        assert_eq!(
            format!("{pk:x}"),
            "e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76"
        );
        assert_eq!(
            format!("{pk:#X}"),
            "0xE2F2AE0A6ABC4E71A884A961C500515F58E30B6AA582DD8DB6A65945E08D2D76"
        );
        assert_eq!(
            format!("{pk:X}"),
            "E2F2AE0A6ABC4E71A884A961C500515F58E30B6AA582DD8DB6A65945E08D2D76"
        );
```

4. Combined formats

And of course, all these format specifiers can be combined:

```rust
assert_eq!(
            format!("{pk:*^#68X}"),
            "*0xE2F2AE0A6ABC4E71A884A961C500515F58E30B6AA582DD8DB6A65945E08D2D76*"
        );
```
@CjS77 CjS77 requested a review from sdbondi February 7, 2023 14:49
@CjS77
Copy link
Contributor Author

CjS77 commented Feb 7, 2023

Should make logs a lot nicer if this is used in Tari - esp the width specifier

Copy link
Member

@sdbondi sdbondi left a comment

Choose a reason for hiding this comment

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

Hellz yeah!

@SWvheerden
Copy link
Contributor

utACK

@CjS77 CjS77 merged commit 1a71aff into main Feb 7, 2023
@CjS77 CjS77 deleted the qol branch February 7, 2023 21:25
@hansieodendaal
Copy link
Contributor

Nice

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.

None yet

4 participants