Giter Club home page Giter Club logo

egui_code_editor's Introduction

Egui Code Editor

Text Editor Widget for egui with numbered lines and simple syntax highlighting based on keywords sets.

Usage with egui

use egui_code_editor::{CodeEditor, ColorTheme, Syntax};

CodeEditor::default()
  .id_source("code editor")
  .with_rows(12)
  .with_fontsize(14.0)
  .with_theme(ColorTheme::GRUVBOX)
  .with_syntax(Syntax::rust())
  .with_numlines(true)
  .show(ui, &mut self.code);

Usage as lexer without egui

Cargo.toml

[dependencies]
egui_code_editor = { version = "0.2" , default-features = false }
colorful = "0.2.2"

main.rs

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())));
    }
}

Themes

Based on themes in Helix Editor.

Font used in examples is Comic Code by Toshi Omagari.

Ayu

Ayu

Ayu Dark

Ayu Dark

Ayu Mirage

Ayu Mirage

Github Dark

Github Dark

Github Light

Github Light

Gruvbox

Gruvbox

Gruvbox Light

Gruvbox Light

Sonokai

Sonokai

egui_code_editor's People

Contributors

akiko97 avatar crumblingstatue avatar godgotzi avatar mkrueger avatar p4ymak 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

Watchers

 avatar  avatar

egui_code_editor's Issues

Allow immutable code view

I'm working on a refactoring tool which does automatic code transformations, and I would like to show the code without it being directly editable.

I made this patch for my local fork, feel free to adapt it if you think it makes sense.

diff --git a/src/lib.rs b/src/lib.rs
index a77d048..5fe8762 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -259,13 +259,13 @@ impl CodeEditor {
 
     #[cfg(feature = "egui")]
     /// Show Code Editor
-    pub fn show(&mut self, ui: &mut egui::Ui, text: &mut String) -> TextEditOutput {
+    pub fn show(&mut self, ui: &mut egui::Ui, text: &mut dyn egui::TextBuffer) -> TextEditOutput {
         let mut text_edit_output: Option<TextEditOutput> = None;
         let mut code_editor = |ui: &mut egui::Ui| {
             ui.horizontal_top(|h| {
                 self.theme.modify_style(h, self.fontsize);
                 if self.numlines {
-                    self.numlines_show(h, text);
+                    self.numlines_show(h, text.as_str());
                 }
                 egui::ScrollArea::horizontal()
                     .id_source(format!("{}_inner_scroll", self.id))

Expose `TextEditOutput`?

I'm happy with the ease of use for CodeEditor to provide syntax highlighting and such for TextEdit, but it'd be better if CodeEditor::show(Ui) returned TextEditOutput instead of Response. I need to use the various fields of the edit state for other purposes.

(I tried this locally, but then it breaks Ui::add(CodeEditor...:show()) since egui wants several other traits to be defined... not sure how you'd want to resolve this.)

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.