/solpretty

A library for pretty printing numbers in Solidity

Primary LanguageSolidity

solpretty

![Solidity][solidity-shield]

A library for pretty printing numbers in Solidity

built on Solady

Installation

To install with Foundry:

forge install devtooligan/solpretty

SolPretty Tools

Import and Inherit

Your test contracts should inherit SolPrettyTools.

import "../src/SolPrettyTools.sol";

contract MyContract is SolPrettyTools {}

pp

  • pp(uint256 value)
  • pp(uint256 value, uint256 fixedDecimals)
  • pp(uint256 value, uint256 fixedDecimals, uint256 displayDecimals)
  • pp(uint256 value, uint256 fixedDecimals, uint256 displayDecimals, uint256 fixedWidth)
  • pp(uint256 value, memory SolPrettyOptions)
  • pp(uint256 value, bool useFormatting, string memory label)
pp(123123123123) //            -> "123,123,123,123" //  default
pp(123123123123, 6) //         -> "123,123.123123" //   fixedDecimals = 6
pp(123123123123, 6, 2) //      -> "123,123.12" //       displayDecimals = 2
pp(123123123123, 6, 0, 15) //  -> "         123,123" // fixedWidth = 15

// log with no formatting
pp(123123123123, false) //     -> "123123123123" //      no formatting

// labels can be optionally appended
pp(123123123123, "myLabel") //            -> "123,123,123,123 myLabel"
pp(123123123123, 6, 0, 15, "myLabel") //  -> "         123,123 myLabel"
pp(123123123123, false, "myLabel") //     -> "123123123123 myLabel"

Customizeable options:

SolPrettyOptions memory solPrettyOptions = getDefaultOptions();

SolPrettyOptions.fixedDecimals = 6;
SolPrettyOptions.fixedWidth = 20;
SolPrettyOptions.decimalDelimeter = bytes1("*");
SolPrettyOptions.integerDelimeter = bytes1(" ");

pp(123123123123, solPrettyOptions); // -> "      123 123*123123"

struct SolPrettyOptions {
    uint256 fixedDecimals; //          default 0
    uint256 displayDecimals; //        default type(uint256).max
    bytes1 decimalDelimiter; //        default "."
    bytes1 fractionalDelimiter; //     default " "
    uint256 fractionalGroupingSize; // default 0
    bytes1 integerDelimiter; //        default ","
    uint256 integerGroupingSize; //    default 3
    uint256 fixedWidth; //             default 0 (automatic)
}

TODO: Add divider and border

SolPretty Library - TODO: Finish this section

concat

  • concat(string memory left, string memory right)
  • concat(string[] memory parts)
pp(1234).concat(" Alice's balance"); // -> "1,234 Alice's Balance";

string[] memory strings = new string[](3);
strings[0] = "a";
strings[1] = "b";
strings[2] = "c";
concat(strings); // -> "abc"


[solidity-shield]: https://img.shields.io/badge/solidity-%3E=0.8.4%20%3C=0.8.19-aa6746