Skip to content

Commit d393802

Browse files
authored
allow custom colors, remove invalid categories from cargo config and bump ver (#13)
1 parent 3b02470 commit d393802

File tree

5 files changed

+62
-26
lines changed

5 files changed

+62
-26
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 1.1.0
2+
- Allow custom colors
3+
14
# 1.0.0
25
- Convert project to crate 🎉
36

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rust_logging"
3-
version = "1.0.0"
3+
version = "1.1.0"
44
edition = "2021"
55
authors = ["Léopold Koprivnik Ibghy, @SkwalExe <skwal.net@gmail.com>"]
66
description = "A couple of functions to make logging in Rust easier."
@@ -10,7 +10,6 @@ homepage = "https://github.com/SkwalExe/rust-logging"
1010
repository = "https://github.com/SkwalExe/rust-logging"
1111
license = "MIT"
1212
keywords = ["rust", "logging", "rust-logging", "logger"]
13-
categories = ["logging", "logger", "logs"]
1413

1514
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1615

README.md

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
```toml
1010
# Cargo.toml
1111
[dependencies]
12-
rust_logging = "1.0.0"
12+
rust_logging = "1.1.0"
1313
```
1414

1515
# Usage 📝
@@ -33,7 +33,7 @@ fn main() {
3333
log_file: "log.txt", // the file to log to when logging to a file
3434
highlight: true, // highlight text after ":" in the message
3535
..Default::default() // the default values for the non specified settings
36-
}
36+
};
3737

3838
let custom_logger = settings.get_logger();
3939
}
@@ -70,7 +70,7 @@ This can be disabled by setting the `highlight` field to `false` inside the cust
7070
let settings = LoggerOptions {
7171
highlight: false,
7272
..Default::default()
73-
}
73+
};
7474

7575
let logger = settings.get_logger();
7676
```
@@ -116,13 +116,39 @@ let settings = LoggerOptions {
116116
info_icon: "ℹ️",
117117
success_icon: "",
118118
..Default::default()
119-
}
119+
};
120120

121121
let logger = settings.get_logger();
122122
```
123123

124124
![](images/4.png)
125125

126+
## Custom Colors 🎨
127+
128+
You can change the colors of the messages by setting the `colors` field inside the custom settings.
129+
130+
The value must be a `rust_logging::Colors` struct.
131+
132+
```rust
133+
let my_colors = Colors {
134+
// \x1b is the escape character (ASCII 27) and [xxm is the color 'code'
135+
red: "\x1b[93m", // escape sequence for yellow foreground
136+
bg_red: "\x1b[42m", // escape sequence for green background
137+
..Default::default()
138+
};
139+
140+
let settings = LoggerOptions {
141+
colors: my_colors,
142+
..Default::default()
143+
};
144+
145+
let logger = settings.get_logger();
146+
147+
logger.error("a command failed : hello");
148+
```
149+
150+
![](images/6.png)
151+
126152
## Log file 📄
127153

128154
You can specify the log file path to write the messages to with the `log_file` field.

images/6.png

17 KB
Loading

src/lib.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,40 @@ use std::default::Default;
33
use std::fmt::Display;
44

55
pub struct Colors {
6-
red: String,
7-
green: String,
8-
yellow: String,
9-
cyan: String,
10-
white: String,
11-
reset: String,
12-
bg_red: String,
13-
bg_cyan: String,
14-
bg_green: String,
15-
bg_yellow: String,
6+
pub red: String,
7+
pub green: String,
8+
pub yellow: String,
9+
pub cyan: String,
10+
pub white: String,
11+
pub reset: String,
12+
pub bg_red: String,
13+
pub bg_cyan: String,
14+
pub bg_green: String,
15+
pub bg_yellow: String,
16+
}
17+
18+
impl Default for Colors {
19+
fn default() -> Self {
20+
Colors {
21+
red: "\x1b[91m".to_string(),
22+
green: "\x1b[92m".to_string(),
23+
yellow: "\x1b[93m".to_string(),
24+
cyan: "\x1b[96m".to_string(),
25+
white: "\x1b[97m".to_string(),
26+
reset: "\x1b[0m".to_string(),
27+
bg_red: "\x1b[41m".to_string(),
28+
bg_cyan: "\x1b[46m".to_string(),
29+
bg_green: "\x1b[42m".to_string(),
30+
bg_yellow: "\x1b[43m".to_string(),
31+
}
32+
}
1633
}
1734

1835
impl Colors {
1936
pub fn new() -> Colors {
2037
if stderr_isatty() && stdout_isatty() {
2138
Colors {
22-
red: "\x1b[91m".to_string(),
23-
green: "\x1b[92m".to_string(),
24-
yellow: "\x1b[93m".to_string(),
25-
cyan: "\x1b[96m".to_string(),
26-
white: "\x1b[97m".to_string(),
27-
reset: "\x1b[0m".to_string(),
28-
bg_red: "\x1b[41m".to_string(),
29-
bg_cyan: "\x1b[46m".to_string(),
30-
bg_green: "\x1b[42m".to_string(),
31-
bg_yellow: "\x1b[43m".to_string(),
39+
..Default::default()
3240
}
3341
} else {
3442
Colors {

0 commit comments

Comments
 (0)