From eff369eac0286e9ae4ffb543a76f4c22d4088163 Mon Sep 17 00:00:00 2001 From: Roman Chumak Date: Fri, 19 Jan 2024 16:49:10 +0300 Subject: [PATCH] Python, CLI example --- README.md | 4 +++- src/lib.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4b3515f..31ee78c 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ CodeEditor::default() ## Usage as lexer -*Cargo.toml* +**Cargo.toml** ```toml [dependencies] @@ -27,6 +27,8 @@ egui_code_editor = { version = "0.2" , default-features = false } colorful = "0.2.2" ``` +**main.rs** + ```rust use colorful::{Color, Colorful}; use egui_code_editor::{Syntax, Token, TokenType}; diff --git a/src/lib.rs b/src/lib.rs index 2e8d97f..eff8a26 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ #![allow(rustdoc::invalid_rust_codeblocks)] //! Text Editor Widget for [egui](https://github.com/emilk/egui) with numbered lines and simple syntax highlighting based on keywords sets. //! -//! ## Usage +//! ## Usage with egui //! //! ```rust //! use egui_code_editor::{CodeEditor, ColorTheme, Syntax}; @@ -15,6 +15,58 @@ //! .with_numlines(true) //! .show(ui, &mut self.code); //! ``` +//! +//! ## Usage as lexer +//! +//! **Cargo.toml** +//! +//! ```toml +//! [dependencies] +//! egui_code_editor = { version = "0.2" , default-features = false } +//! colorful = "0.2.2" +//! ``` +//! +//! **main.rs** +//! +//! ```rust +//! use colorful::{Color, Colorful}; +//! use egui_code_editor::{Syntax, Token, TokenType}; +//! +//! fn color(token: TokenType) -> Color { +//! match token { +//! TokenType::Comment(_) => Color::Grey37, +//! TokenType::Function => Color::Yellow3b, +//! TokenType::Keyword => Color::IndianRed1c, +//! TokenType::Literal => Color::NavajoWhite1, +//! TokenType::Numeric(_) => Color::MediumPurple, +//! TokenType::Punctuation(_) => Color::Orange3, +//! TokenType::Special => Color::Cyan, +//! TokenType::Str(_) => Color::Green, +//! TokenType::Type => Color::GreenYellow, +//! TokenType::Whitespace(_) => Color::White, +//! TokenType::Unknown => Color::Pink1, +//! } +//! } +//! +//! fn main() { +//! let text = r#"// Code Editor +//! CodeEditor::default() +//! .id_source("code editor") +//! .with_rows(12) +//! .with_fontsize(14.0) +//! .with_theme(self.theme) +//! .with_syntax(self.syntax.to_owned()) +//! .with_numlines(true) +//! .vscroll(true) +//! .show(ui, &mut self.code); +//! "#; +//! +//! let syntax = Syntax::rust(); +//! for token in Token::default().tokens(&syntax, text) { +//! print!("{}", token.buffer().color(color(token.ty()))); +//! } +//! } +//! ``` pub mod highlighting; mod syntax;