Skip to content

Commit

Permalink
feat: add code highlihting support (rust only ATM)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chleba committed Jul 9, 2024
1 parent 8dbf81c commit 5fe0b0a
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 2 deletions.
100 changes: 100 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ tracing-error = "0.2.0"
tracing-subscriber = { version = "0.3.17", features = ["env-filter", "serde"] }
tui-big-text = "0.4.3"
base64 = "0.22.1"
syntect = "5.2.0"
syntect-tui = "3.0.2"

[build-dependencies]
vergen = { version = "8.2.6", features = [ "build", "git", "gitoxide", "cargo" ]}
39 changes: 37 additions & 2 deletions src/components/slides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use ratatui::{
widgets::{block::Title, *},
};
use ratatui_image::{picker::Picker, protocol::StatefulProtocol, Image, Resize, StatefulImage};
use syntect::{
easy::HighlightLines, highlighting::ThemeSet, parsing::SyntaxSet, util::LinesWithEndings,
};
use syntect_tui::into_span;
use tokio::sync::mpsc::UnboundedSender;
use tui_big_text::{BigText, PixelSize};

Expand Down Expand Up @@ -185,11 +189,17 @@ impl Slides {
fn make_slide_items<'a>(
slide: &SlideJson,
json_slides: String,
) -> Vec<(ReturnSlideWidget<'a>, Option<Rect>, Option<Vec<u64>>)> {
) -> Vec<(
ReturnSlideWidget<'a>,
String,
Option<Rect>,
Option<Vec<u64>>,
)> {
let mut slide_items = vec![];
for item in &slide.content {
slide_items.push((
make_slide_content(item.clone(), json_slides.clone()),
get_slide_content_string(&item),
item.rect,
item.data.clone(),
));
Expand Down Expand Up @@ -251,7 +261,7 @@ impl Component for Slides {

// -- render slide widgets
let mut img_index = 0;
for (slide, r, d) in slide_items {
for (slide, c, r, d) in slide_items {
let slide_rect = self.get_slide_rect(rect.content, r);

let mut data = vec![];
Expand Down Expand Up @@ -293,6 +303,31 @@ impl Component for Slides {
s = s.data(&data);
f.render_widget(s, slide_rect);
}
ReturnSlideWidget::CodeHighlight(mut l) => {
let ps = SyntaxSet::load_defaults_newlines();
let ts = ThemeSet::load_defaults();
let syntax = ps.find_syntax_by_extension("rs").unwrap();
let mut h = HighlightLines::new(syntax, &ts.themes["base16-ocean.dark"]);

let mut lines: Vec<Line> = vec![];
let c_lines: Vec<&str> = c.split('\n').collect();

for c_line in c_lines {
for line in LinesWithEndings::from(c_line) {
let l_spans: Vec<Span> = h
.highlight_line(line, &ps)
.unwrap()
.into_iter()
.filter_map(|seg| into_span(seg).ok())
.collect();
let line = Line::from(l_spans);
lines.push(line);
}
}
l = Paragraph::new(lines);

f.render_widget(l, slide_rect);
}
}
}
Ok(())
Expand Down
3 changes: 3 additions & 0 deletions src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub enum ReturnSlideWidget<'a> {
Image(DynamicImage),
Block(Block<'a>),
Sparkline(Sparkline<'a>),
CodeHighlight(Paragraph<'a>),
// CodeHighlight(Line<'a>),
}

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
Expand All @@ -21,6 +23,7 @@ pub enum SlideContentType {
Image,
Block,
Sparkline,
CodeHighlight
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
Expand Down
13 changes: 13 additions & 0 deletions src/slide_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ pub fn make_slide_sparkline<'a>(slide: ContentJson) -> ReturnSlideWidget<'a> {
)
}

// -------------
// -- CODE HIGHLIGHT
// -------------
pub fn make_slide_code_highlight<'a>(slide: ContentJson) -> ReturnSlideWidget<'a> {
let content = get_slide_content_string(&slide);
let color = get_slide_content_color(&slide);

ReturnSlideWidget::CodeHighlight(Paragraph::new(content))
}



pub fn make_slide_content<'a>(
slide_content: ContentJson,
slide_path: String,
Expand All @@ -148,5 +160,6 @@ pub fn make_slide_content<'a>(
SlideContentType::Image => make_slide_image(slide_content, slide_path),
SlideContentType::Block => make_slide_block(slide_content),
SlideContentType::Sparkline => make_slide_sparkline(slide_content),
SlideContentType::CodeHighlight => make_slide_code_highlight(slide_content),
}
}
8 changes: 8 additions & 0 deletions talk_example/slides.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
},

"slides" : [

{
"title": "Block",
"content": [
{"type": "CodeHighlight", "content": "Block::new()\n.borders(Borders::ALL)\n.title(\"Block title\")", "rect": {"x": 10, "y": 10, "width": 40, "height": 10}}
]
},

{
"title": "RATATUI",
"content": [
Expand Down

0 comments on commit 5fe0b0a

Please sign in to comment.