Use cursive-syntect for syntax highlighting
maincursive-syntect crate provides a thin adapter that allows you to style strings for use in a cursive application using highlighters from the syntect crate.repository·main·Indexed 26 days ago
https://github.com/gyscos/cursiveA Text User Interface (TUI) library for Rust designed for ease-of-use, safety, and flexibility. It enables the creation of terminal applications ranging from simple scripts to complex real-time interfaces and games. The ecosystem includes cursive-core for stable third-party library development and cursive-syntect for syntax highlighting using the syntect crate.
cursive-syntect crate provides a thin adapter that allows you to style strings for use in a cursive application using highlighters from the syntect crate.To start a new Cursive application, create a new Cargo binary project and add cursive to your Cargo.toml dependencies.
% cargo new --bin cursive_exampleCargo.toml:[package]
name = "cursive_example"
version = "0.1.0"
[dependencies]
cursive = "*"cursive-core instead of the main cursive crate. This helps avoid unnecessary semver breaking updates, as cursive-core is more stable and contains the core definitions of the framework, excluding the backends.Cursive::pop_layer() to remove the current top-most layer and Cursive::add_layer() to add a new one. This is the standard way to implement transitions, such as moving from a question dialog to a results dialog.A Cursive application requires a root object (created via cursive::default()) to manage the event loop and layers. You build interfaces by adding layers (like Dialog) to this root.
In the example below, we create a dialog containing a TextView and a button that calls siv.quit() when clicked. Finally, siv.run() is called to start the application's event loop.
use cursive::views::{Dialog, TextView};
fn main() {
// Creates the cursive root - required for every application.
let mut siv = cursive::default();
// Creates a dialog with a single "Quit" button
siv.add_layer(Dialog::around(TextView::new("Hello Dialog!"))
.title("Cursive")
.button("Quit", |s| s.quit()));
// Starts the event loop.
siv.run();
}To use cursive-syntect for syntax highlighting in your Cursive application, add it to your Cargo.toml dependencies.
[dependencies]
cursive-syntect = "0.2"A Cursive application typically follows three phases centered around the Cursive root object:
Cursive object using cursive::default()..run().Basic lifecycle example:
fn main() {
let mut siv = cursive::default();
siv.run();
}fn main() {
let mut siv = cursive::default();
siv.run();
}To use Cursive in your Rust project, add it to your Cargo.toml dependencies. You can use the stable version from crates.io or the latest version from the GitHub repository.
[dependencies]
cursive = "0.21"[dependencies]
cursive = { git = "https://github.com/gyscos/cursive" }[dependencies]
cursive = "0.21"To add scrolling capabilities to a custom view, embed scroll::Core in your struct and implement the scroll::Scroller trait. You can use the cursive_core::impl_scroller! macro to simplify the implementation.
Once implemented, you must delegate the standard View trait methods (on_event, important_area, layout, required_size, and draw) to the provided scroll helper functions to ensure the scrolling logic (like viewport calculation and event handling) is correctly applied.
use cursive_core::event::{Event, EventResult};
use cursive_core::view::{View, scroll};
use cursive_core::{Printer, Rect, Vec2};
struct MyView {
core: scroll::Core,
}
// Use the macro to implement Scroller for MyView using its core field
cursive_core::impl_scroller!(MyView::core);
impl MyView {
fn inner_on_event(&mut self, event: Event) -> EventResult {
EventResult::Ignored
}
fn inner_important_area(&self, size: Vec2) -> Rect {
Rect::from_size((0, 0), size)
}
}
impl View for MyView {
fn draw(&self, _printer: &Printer) {}
fn on_event(&mut self, event: Event) -> EventResult {
// Delegate event handling to the scroll module
scroll::on_event(
self,
event,
Self::inner_on_event,
Self::inner_important_area,
)
}
}ncurses-backend feature must be enabled in your Cargo.toml to use this implementation. The backend requires the $TERM environment variable to be set.You can configure logging levels without changing code by using environment variables. Call set_filter_levels_from_env() before init() to apply these settings:
RUST_LOG: Sets both internal (cursive-specific) and external log levels to the specified value.CURSIVE_LOG: Sets only the internal log level. This takes precedence over RUST_LOG for internal logs.Example values for the variables include trace, debug, info, warn, or error.
init(). This sets up the global CursiveLogger and configures the log crate to use it. Note that this will panic if a logger has already been set. Once initialized, you can view logs using a DebugView or by calling Cursive::toggle_debug_console().