Giter Site home page Giter Site logo

0x20f / paris Goto Github PK

View Code? Open in Web Editor NEW
259.0 5.0 11.0 5.09 MB

Logger and ANSI formatter in Rust for pretty colors and text in the terminal with no dependencies. Aiming for a relatively simple API

License: Mozilla Public License 2.0

Rust 100.00%
logger terminal-colors terminal-icons cli simple-api icons customisation crates rust rust-lang

paris's Introduction

paris

Simple way to output beautiful text in your CLI applications. Only limit is your imagination.

Build status badge Crates io badge Repository version badge License badge



Notice

If the provided logging API is too simple for your use case (it's definitely not meant for anything too complex) but you still like the formatting style (<blue></> etc) then you can still make use of this crate! See here for a quick example.

By importing the format module, you get access to the Formatter structure itself. This is what the Logger uses in the back end anyway, you're basically stripping away the API.

For even more ease of use the formatter module includes a colorize_string function which you can use to directly colorize whatever without initializing the formatter. Drawbacks and caveats may apply, read the docs.

See the formatter module for more info if needed.

How to use

[dependencies]
paris = "1.5"
use paris::Logger;

let mut log = Logger::new();

log.info("It's that simple!");

Optional features

Timestamps

If you'd like timestamps with all your logs you'll have to enable the feature when adding the crate as a dependency.

[dependencies]
paris = { version = "1.5", features = ["timestamps"] }
Macros

Every common function has a macro. To make use of these macros you'll need to enable the macros feature.

[dependencies]
paris = { version = "1.5", features = ["macros"] }
No logger

If you'd prefer to only use the macros and not even have the Logger struct included in your package, that's definitely possible! All you need to do is enable the no_logger feature.

[dependencies]
paris = { version = "1.5", features = ["no_logger"] }

Simple API

// You can have icons at the start of your message!
log.info("Will add ℹ at the start");
log.error("Will add ✖ at the start");

// or as macros
info!("Will add ℹ at the start");
error!("Will add ✖ at the start");
See the Logger struct for all methods and their macro equivalents

Chaining

All methods can be chained together to build more intricate log/message combinations, in hopes of minimizing the chaos that every log string becomes when you have to concatenate a bunch of strings and add tabs and newlines everywhere.

log.info("this is some info")
   .indent(4).warn("this is now indented by 4")
   .newline(5)
   .success("and this is 5 lines under all other messages");

Customisation

Outputting text is cool. Outputting text with a colored icon at the start is even cooler! But this crate is all about customisation, about making the logs feel like home, if you will. Included in the crate are a variety of keys you can use to colorize your logs just the way you want them to be.

log.info("I can write normal text or use tags to <red>color it</>");
log.warn("Every function can contain <on-green><black>tags</>");

log.info("If you don't write them <bleu>correctly</>, you just get an ugly looking tag");

There's a key for all colors supported by the terminal (white, black, red, blue, magenta, etc.) If you add the word on to any of those colors, it becomes the background color instead (on-red, on-blue, on-green).

// How useful...
log.info("<on-red> This has red background</>");

Maybe you'd like to use your terminals brighter colors, if that's the case you just have to add bright to your tag. Makes sense.

log.info("<blue><on-bright-red> This text is blue on a bright red background</> it's a pain");

If you feel like writing a lot of colors by hand is too tedious, or if you know you're going to be using the same combination of colors over and over again you can create a custom style that encapsulates all those colors.

log.add_style("lol", vec!["green", "bold", "on-bright-blue"]);

// '<lol>' is now a key that you can use in your strings
log.info("<lol>This is has all your new styles</>");
Scroll down for a full list of keys if you're not feeling confident in your ability to name colors. It happens.

Resetting

You've probably seen the </> tag in the above logs. It's not there to "close the previously opened tag" no no. You can open as many tags as you want and only use </> once, it's just the "reset everything to default" tag, You might decide you don't ever want to use it. It's up to you.

However, resetting everything to default might not be what you want. Most of the time it'll be enough, but for those times when it isn't there are a few other tags such as:

  • <///> only resets the background
  • <//> only reset the foreground

Macros

With the macros feature enabled, you get access to macro equivalents of the logger functions.

Advantages of using macros:

  • You don't have to instantiate the logger Logger::new()
  • Simple to write
  • Can format parameters like print! and println!

Disadvantages of using macros:

  • Can't chain calls
  • Manual newlines and tabs with \n and \t
  • There's no loading animation for macros

You get to decide whether you want to use macros or not. Every macro has the same functionality as its Logger equivalent. Colors and icon keys work just the same.

See the Logger struct for all methods and their macro equivalents

Color keys

To use a key just add the color name surrounded by <, > to your log string. If you don't like the dashes(-), you can use underlines(_) or spaces( )

Foreground

black, red, green, yellow, blue, cyan, magenta, white

Bright

bright-black, bright-red, bright-green, bright-yellow, bright-blue, bright-cyan, bright-magenta, bright-white

Background

on-black, on-red, on-green, on-yellow, on-blue, on-cyan, on-magenta, on-white

Bright

on-bright-black, on-bright-red, on-bright-green, on-bright-yellow, on-bright-blue, on-bright-cyan, on-bright-magenta, on-bright-white

Styles

bold(b), underline(u), dimmed(d), italic(i), blink(l), reverse(r), hidden(h), strikethrough(s)

Styles are a bit different, they all have their usual keys, the long and painful to write ones. But they also have shorthand keys (in parenthesis).

And while they all may reset using one of the reset keys above, if you're looking to turn off a specific style you've opened, you can just use the exact same key but with a slash / in front of it.

Example: <bold> gets closed by </bold>

Icons

info, cross, warn, tick, heart

paris's People

Contributors

0x20f avatar atul9 avatar chris999 avatar dtolnay avatar manio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

paris's Issues

toggle formating on/off

Is your feature request related to a problem? Please describe.
I'm using simplelog with paris module for colourize the console output. But for logging to file, it would be nice to turn of the color format.
For simplelog it is way easer when paris already provide this feature. (Drakulix/simplelog.rs#98)

Describe the solution you'd like
Add a toggle option, to turn on and off the formater.

Describe alternatives you've considered
I can strip the text messages with an regex, but is a bit ugly.

Terminating a loading animation with .error() should look the same as .success() but with a red ❌

Describe the bug
Ending the loading animation with .error(...) or .done().error(...) leaves the loading text in the terminal and doesn't replace it like the .success(...) message does.

To Reproduce
Steps to reproduce the behavior:

  1. start a loading animation with logger.loading(format!("{test_name}..."));
  2. end the loading animation with logger.error(format!("{test_name} - {duration:?}"));
  3. Note that you see something like
    image

Expected behavior
I would expect it to format like ending it with success(...)
image
(this is an different test that passed)

*Desktop (please complete the following information):

  • OS: MacOS 12.0.1
  • Browser: Terminal in VSCode

Additional context
running cargo run --release
the test that's failing is a long running test, takes about 30s before it fails.

Move things into separate files

lib.rs is getting pretty big, start moving things out.

  • LogIcon can be moved to a different file
  • Maybe have another file for tokens that can be parsed somehow?
  • Should Logger be moved to a different file as well so lib.rs is clean?

Can't reset only background or foreground without resetting everything

There's only one reset tag </>, if you write something like <on red><italic>hahaha</> I want this to be on red too!</>. The text "I want this to be on red too" won't actually be on red because the reset tag resets everything.

Suggestion:

  • Multiple reset tags for colors (</bg>, </fg>, </>) OR (<///>, <//>, </>) OR (</_>, </->, </>)
  • Shortened style keys for styles (<b></b> for bold, etc.)

Parsing error?

Hi,
I think i found some interesting thing in paris parser:
Recently I created a debug code which is doing a dumb colored debug with the following call:

info!("<black><----------------------------- before inner_get_ref</>");

As the result, the string is properly started in black, but the final resetting code </> is not interpreted (and printed instead).
I am thinking that probably the cause of the problem is this "ascii-arrow": <----------------------

I am OK if this is hard to fix for some reason, but I was only wanted to let you know about this... :)

Macros for common actions

In case people don't want to create a new logger, add an optional feature ["macros"] to allow for macros like info!, error!, warning!, success!

Timestamp not shown correctly when using `log.loading()`

Describe the bug

The timestamp is rendered on the right hand side of the loading text. It should be on the left like the output from other log functions. See the screenshot below as an example.

Screenshots

image

To Reproduce

Try the following code snippet:

use std::{thread, time::Duration};

use paris::Logger;

fn main() {
    let mut log = Logger::new();
    log.success("This message includes a timestamp!");
    log.loading("Initializing the simulation..."); // <-- the timestamp is shown incorrectly using this method
    thread::sleep(Duration::from_secs(20));
    log.success("Done!");
}

Expected behavior

The timestamp should be visible on the left.

Desktop (please complete the following information):

  • OS: Arch Linux
  • Paris Version: 1.5.13 (with the timestamp feature enabled of course)

Log to file

Is it possible to write logs to a file? It would be nice if there was a way to both log to console/terminal and write to a .txt file to view output logs of long-running operations at a later point in time.

Consistent macros

I am curious why there isn't a debug! or trace! macro. Seems like this is pretty common across a bunch of the logging crates, but not this one.

I'd really like to see more consistency so that paris is a drop-in replacement for the other logging libraries, but also includes extra functionality like the nice color identifiers.

I could try log! in place of debug! or trace, but log! itself doesn't represent what I want to do: have a level attached to the log.

Panic in formatter

Hi,
Sorry, that is't me again bothering you... I hope you don't mind :)

I think I found a panic bug (I found it by accident when trying to use paris logger in a place of env_logger in cec project) :)

The following line:

log.info("<< powering on 'TV' (0)");

is causing:

thread 'main' panicked at 'byte index 23 is out of bounds of `< powering on 'TV' (0)`', /usr/src/paris/src/formatter/keys/key_list.rs:43:35
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The last 6 characters of the loading message remain after calling `done()`

Describe the bug
On Windows, after calling loading() and done(), 6 characters remain from the loading message. As far as I can tell, it's only the last 6 characters. I haven't tested this on other platforms, as I'm assuming it's a Windows problem.

To Reproduce
Using version 1.5 with default features, this code shows the example. main.rs:

use std::thread::sleep;
use std::time::Duration;

use paris::Logger;

fn main() {
    let mut log = Logger::new();

    log.loading("Loading");
    sleep(Duration::from_secs(2));
    log.done();
    // "oading" will remain in the terminal
}

Expected behavior
The entire message to clear

Screenshots
While loading, the animation plays but there's a visible <cyan> and </>. That's another issue though:
https://imgur.com/XORZDVr

After loading stops, this is what remains:
https://imgur.com/zVgEdAj

Desktop (please complete the following information):

  • OS: Windows 10
  • Tested with Git Bash, CMD, and VSCode integrated terminal

Update docs url

documentation field in Cargo.toml should lead to https://docs.rs/paris/1.0.2/paris/struct.Logger.html

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.