From 4fc9b16ba11008cf46cbbf2558e9252aa0ac6f61 Mon Sep 17 00:00:00 2001
From: Finn Bear <finnbearlabs@gmail.com>
Date: Tue, 26 Apr 2022 22:38:42 -0700
Subject: [PATCH] Add syntax hylighting to code display.

---
 Cargo.lock            | 66 +++++++++++++++++++++++++++++++++++++++++++
 Cargo.toml            |  1 +
 src/component/code.rs | 26 +++++++++++++++--
 src/main.rs           |  4 +++
 4 files changed, 95 insertions(+), 2 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 75d9977..93e0013 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -265,6 +265,21 @@ dependencies = [
  "libc",
 ]
 
+[[package]]
+name = "chrono"
+version = "0.4.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
+dependencies = [
+ "js-sys",
+ "libc",
+ "num-integer",
+ "num-traits",
+ "time",
+ "wasm-bindgen",
+ "winapi",
+]
+
 [[package]]
 name = "clipboard-win"
 version = "3.1.1"
@@ -623,6 +638,19 @@ dependencies = [
  "winit",
 ]
 
+[[package]]
+name = "egui_demo_lib"
+version = "0.17.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "43ed5e8c26d1d7f9f56e51f32469b13e9880fceda61199bd0a7cae79b88d5dd5"
+dependencies = [
+ "chrono",
+ "egui",
+ "enum-map",
+ "epi",
+ "unicode_names2",
+]
+
 [[package]]
 name = "egui_glow"
 version = "0.17.0"
@@ -668,6 +696,27 @@ dependencies = [
  "bytemuck",
 ]
 
+[[package]]
+name = "enum-map"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0348b2a57c82f98b9dbd8098b1abb2416f221823d3e50cbe24eaebdd16896826"
+dependencies = [
+ "enum-map-derive",
+ "serde",
+]
+
+[[package]]
+name = "enum-map-derive"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a63b7a0ddec6f38dcec5e36257750b7a8fcaf4227e12ceb306e341d63634da05"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
 [[package]]
 name = "enumflags2"
 version = "0.7.5"
@@ -817,6 +866,7 @@ name = "generative_art_cartoon"
 version = "0.1.0"
 dependencies = [
  "eframe",
+ "egui_demo_lib",
  "image",
  "rand",
 ]
@@ -1810,6 +1860,16 @@ dependencies = [
  "syn",
 ]
 
+[[package]]
+name = "time"
+version = "0.1.43"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
+dependencies = [
+ "libc",
+ "winapi",
+]
+
 [[package]]
 name = "tinyvec"
 version = "1.6.0"
@@ -1893,6 +1953,12 @@ version = "0.2.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
 
+[[package]]
+name = "unicode_names2"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "eec8e807a365e5c972debc47b8f06d361b37b94cfd18d48f7adc715fb86404dd"
+
 [[package]]
 name = "url"
 version = "2.2.2"
diff --git a/Cargo.toml b/Cargo.toml
index 1430cd2..ac28fab 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,5 +5,6 @@ edition = "2021"
 
 [dependencies]
 eframe = "0.17"
+egui_demo_lib = "0.17"
 image = {version = "0.24", default-features = false, features=["png"]}
 rand = {version = "0.8"}
\ No newline at end of file
diff --git a/src/component/code.rs b/src/component/code.rs
index bea7172..f7d7e2d 100644
--- a/src/component/code.rs
+++ b/src/component/code.rs
@@ -1,4 +1,5 @@
-use eframe::egui::{Align2, Color32, Context, Vec2, Window};
+use eframe::egui;
+use eframe::egui::{Align2, Color32, Context, Vec2, Widget, Window};
 
 #[derive(Clone)]
 pub struct Code {
@@ -42,7 +43,28 @@ impl Code {
             .fixed_size(Vec2::splat(self.size_pixels))
             .show(ctx, |ui| {
                 //let (_id, _rect) = ui.allocate_space(Vec2::splat(self.size_pixels));
-                ui.code_editor(&mut self.code.as_str());
+
+                let theme = egui_demo_lib::syntax_highlighting::CodeTheme::from_memory(ui.ctx());
+
+                let mut layouter = |ui: &egui::Ui, string: &str, wrap_width: f32| {
+                    let mut layout_job = egui_demo_lib::syntax_highlighting::highlight(
+                        ui.ctx(),
+                        &theme,
+                        string,
+                        "rs",
+                    );
+                    layout_job.wrap_width = wrap_width;
+                    ui.fonts().layout_job(layout_job)
+                };
+
+                egui::TextEdit::multiline(&mut self.code.as_str())
+                    .font(egui::TextStyle::Monospace) // for cursor height
+                    .code_editor()
+                    //.desired_rows(10)
+                    .lock_focus(true)
+                    .desired_width(f32::INFINITY)
+                    .layouter(&mut layouter)
+                    .ui(ui);
             });
     }
 }
diff --git a/src/main.rs b/src/main.rs
index f0c37ac..15b1b05 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -91,6 +91,10 @@ impl epi::App for Cartoon {
             let small_style = style.text_styles.get_mut(&TextStyle::Small).unwrap();
             small_style.size = 22.0;
         }
+        {
+            let monospaced_style = style.text_styles.get_mut(&TextStyle::Monospace).unwrap();
+            monospaced_style.size = 22.0;
+        }
         ctx.set_style(style);
     }
 
-- 
GitLab