Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skipping the start of TUI line renders correctly #91

Merged
merged 4 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
util::parent_dir_containing,
};
use serde::{de, Deserialize, Deserializer};
use std::{collections::HashMap, env, fs, io, path::Path};
use std::{collections::HashMap, env, fs, io, iter::successors, path::Path};
use tracing::{error, warn};

pub const DEFAULT_CONFIG: &str = include_str!("../data/config.toml");
Expand Down Expand Up @@ -181,20 +181,21 @@ impl Default for ColorScheme {
}

impl ColorScheme {
/// Determine UI [Style]s to be applied for a given syntax tag.
/// Determine UI [Styles] to be applied for a given syntax tag.
///
/// If the full tag does not have associated styling but its dotted prefix does (e.g.
/// "function.macro" -> "function") then the styling of the prefix is used. Otherwise default
/// styling will be used ([TK_DEFAULT]).
/// If the full tag does not have associated styling but its dotted prefix does then the
/// styling of the prefix is used, otherwise default styling will be used ([TK_DEFAULT]).
///
/// For key "foo.bar.baz" this will return the first value found out of the following keyset:
/// - "foo.bar.baz"
/// - "foo.bar"
/// - "foo"
/// - [TK_DEFAULT]
pub fn styles_for(&self, tag: &str) -> &Styles {
match self.syntax.get(tag) {
Some(styles) => styles,
None => tag
.split_once('.')
.and_then(|(prefix, _)| self.syntax.get(prefix))
.or(self.syntax.get(TK_DEFAULT))
.expect("to have default styles"),
}
successors(Some(tag), |s| Some(s.rsplit_once('.')?.0))
.find_map(|k| self.syntax.get(k))
.or(self.syntax.get(TK_DEFAULT))
.expect("to have default styles")
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const ENABLE_MOUSE_SUPPORT: &str = "\x1b[?1000h\x1b[?1002h\x1b[?1015h\x1b[?1006h
const DISABLE_MOUSE_SUPPORT: &str = "\x1b[?1006l\x1b[?1015l\x1b[?1002l\x1b[?1000l";
const ENABLE_ALTERNATE_SCREEN: &str = "\x1b[?1049h";
const DISABLE_ALTERNATE_SCREEN: &str = "\x1b[?1049l";
pub const RESET_STYLE: &str = "\x1b[m";

/// Used for storing and checking whether or not we've received a signal that our window
/// size has changed.
Expand Down Expand Up @@ -113,6 +114,28 @@ pub struct Styles {
pub underline: bool,
}

impl fmt::Display for Styles {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(fg) = self.fg {
write!(f, "{}", Style::Fg(fg))?;
}
if let Some(bg) = self.bg {
write!(f, "{}", Style::Bg(bg))?;
}
if self.bold {
write!(f, "{}", Style::Bold)?;
}
if self.italic {
write!(f, "{}", Style::Italic)?;
}
if self.underline {
write!(f, "{}", Style::Underline)?;
}

Ok(())
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Style {
Fg(Color),
Expand Down
4 changes: 2 additions & 2 deletions src/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ impl Tokenizer {
/// Byte offsets within a Buffer
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct ByteRange {
from: usize,
to: usize,
pub(crate) from: usize,
pub(crate) to: usize,
}

impl ByteRange {
Expand Down
4 changes: 2 additions & 2 deletions src/ui/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,15 +789,15 @@ impl View {
}

if y >= self.row_off + screen_rows {
self.row_off = y - screen_rows + 1;
self.row_off = y + 1 - screen_rows;
}

if self.rx < self.col_off {
self.col_off = self.rx;
}

if self.rx >= self.col_off + screen_cols - w_sgncol {
self.col_off = self.rx - screen_cols + w_sgncol + 1;
self.col_off = self.rx + w_sgncol + 1 - screen_cols;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub(crate) enum StateChange {
StatusMessage { msg: String },
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
pub(crate) enum Ui {
Headless,
Expand Down
Loading