Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yazaldefilimone/code_highlighter.rs
An simple code block highlighter in Rust.
https://github.com/yazaldefilimone/code_highlighter.rs
highlight rust
Last synced: about 1 month ago
JSON representation
An simple code block highlighter in Rust.
- Host: GitHub
- URL: https://github.com/yazaldefilimone/code_highlighter.rs
- Owner: yazaldefilimone
- Created: 2024-07-26T21:22:55.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-09-10T09:46:29.000Z (2 months ago)
- Last Synced: 2024-10-06T04:40:02.725Z (about 1 month ago)
- Topics: highlight, rust
- Language: Rust
- Homepage:
- Size: 77.1 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Rust util that highlights an error.
- `code_highlighter` lets you highlight errors in your code and show extra lines around the error for better context.
Example
The code below:
```rust
let code = "functon is_zero (x) {
if (x == 0) [
return true;
] else {
return false;
}
}";
println!("Error:");
println!("{}", code_highlighter::highlight_error(38, 64, &code));
println!("");println!("Warning:");
println!("{}", code_highlighter::highlight_warning(38, 64, &code));
println!("");println!("Custom color:");
println!("{}", code_highlighter::highlight(38, 64, &code, "\x1b[4m\x1b[32m"));
println!("");let code = "(Foo x) = 7[0 ]\n";
println!("Error:");
println!("{}", code_highlighter::highlight_error(16, 17, &code));
println!("");
```Will output:
![example](./example.png)
## Usage
1. Install using cargo
```shell
cargo add code_highlighter```
2. `main.rs`
```rust
use code_highlighter::highlight_error_with_context;fn main() {
// set the number of lines of context you want to show
let context = 2; // Adds two lines above and below the error// assuming `range` has the error position and `source.raw` is your code
let code = highlight_error_with_context(range.start, range.end, &source.raw, context);// print the highlighted code
println!("{}", code);
}
```