Skip to content

Commit

Permalink
Merge pull request #66 from matheuswhite/39-horizontal-scroll
Browse files Browse the repository at this point in the history
Fix horizontal scroll bug on multiple richtexts
  • Loading branch information
matheuswhite authored Mar 15, 2024
2 parents 0e55c49 + af0dd4c commit 9498a91
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 37 deletions.
18 changes: 5 additions & 13 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,21 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install libudev-sys and Lua5.4
run: sudo apt-get install -y libudev-dev liblua5.4-dev
- name: Install libudev-sys
run: sudo apt-get install -y libudev-dev
- name: Build
run: cargo build --verbose
run: cargo build --release --verbose
build-windows:
name: Check on Windows
runs-on: windows-latest
defaults:
run:
shell: msys2 {0}
steps:
- uses: msys2/setup-msys2@v2
- uses: actions/checkout@v3
- name: Install Rust & Lua
run: pacman -S --noconfirm mingw-w64-x86_64-rust mingw-w64-x86_64-lua mingw-w64-x86_64-luajit mingw-w64-x86_64-pkg-config
- name: Build
run: cargo build --verbose
run: cargo build --release --verbose
build-macos:
name: Check on MacOS
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- name: Install Lua5.4
run: brew install lua
- name: Build
run: cargo build --verbose
run: cargo build --release --verbose
72 changes: 72 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ serde = "1.0"
serde_yaml = "0.9"
rand = "0.8.5"
clap = { version = "4.1.9", features = ["derive"] }
mlua = { version = "0.9.6", features = ["lua54", "async", "send"] }
mlua = { version = "0.9.6", features = ["lua54", "vendored", "async", "send"] }
anyhow = "1.0.79"
homedir = "0.2.1"
regex = { version = "1.10.3", features = [] }
Expand Down
8 changes: 6 additions & 2 deletions src/command_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::plugin_manager::PluginManager;
use crate::serial::SerialIF;
use crate::text::TextView;
use chrono::Local;
use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, KeyModifiers, MouseEventKind};
use crossterm::event::{
Event, EventStream, KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseEventKind,
};
use futures::StreamExt;
use rand::seq::SliceRandom;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
Expand Down Expand Up @@ -99,7 +101,9 @@ impl CommandBar {
_ => {}
}
}
Event::Key(key) => sender.send(Key(key)).unwrap(),
Event::Key(key) if key.kind == KeyEventKind::Press => {
sender.send(Key(key)).unwrap()
}
Event::Mouse(mouse_evt) => match mouse_evt.kind {
MouseEventKind::ScrollUp => sender.send(VerticalScroll(-1)).unwrap(),
MouseEventKind::ScrollDown => sender.send(VerticalScroll(1)).unwrap(),
Expand Down
49 changes: 35 additions & 14 deletions src/rich_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ratatui::style::{Color, Style};
use ratatui::text::Span;
use std::collections::HashMap;

#[derive(Clone)]
pub struct RichText {
content: Vec<u8>,
fg: Color,
Expand Down Expand Up @@ -38,16 +39,34 @@ impl RichText {
)
}

pub fn crop_prefix_len(&self, len: usize) -> Self {
Self {
content: if len >= self.content.len() {
vec![]
} else {
self.content[len..].to_vec()
},
fg: self.fg,
bg: self.bg,
}
pub fn crop_rich_texts(rich_texts: &[RichText], cursor: usize) -> Vec<Span> {
rich_texts
.iter()
.fold((cursor, vec![]), |(mut cursor, mut acc), text| {
if cursor == 0 {
acc.push(text.clone().to_span());
return (0, acc);
}

match cursor {
x if x < text.content.len() => {
acc.push(
Self {
content: text.content[cursor..].to_vec(),
fg: text.fg,
bg: text.bg,
}
.to_span(),
);
cursor = 0;
}
x if x == text.content.len() => cursor = 0,
_ => cursor -= text.content.len(),
}

(cursor, acc)
})
.1
}
}

Expand Down Expand Up @@ -79,9 +98,11 @@ impl RichTextWithInvisible {
|(buffer, state, acc), byte| match state {
State::None => (
vec![byte],
Self::is_visible(byte)
.then_some(State::Visible)
.unwrap_or(State::Invisible),
if Self::is_visible(byte) {
State::Visible
} else {
State::Invisible
},
acc,
),
State::Visible => {
Expand Down Expand Up @@ -140,7 +161,7 @@ impl RichTextWithInvisible {
}

fn is_visible(byte: u8) -> bool {
0x20 <= byte && byte <= 0x7E
(0x20..=0x7E).contains(&byte)
}

fn bytes_to_rich(bytes: Vec<u8>) -> Vec<u8> {
Expand Down
8 changes: 1 addition & 7 deletions src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,7 @@ impl TextView {
);
let content = vec![timestamp_span]
.into_iter()
.chain(data.iter().enumerate().map(|(i, rich_text)| {
if i == 0 {
rich_text.crop_prefix_len(scroll.1 as usize).to_span()
} else {
rich_text.to_span()
}
}))
.chain(RichText::crop_rich_texts(data, scroll.1 as usize))
.collect::<Vec<_>>();

Line::from(content)
Expand Down

0 comments on commit 9498a91

Please sign in to comment.