Skip to content

Commit

Permalink
feat(tui): add support for BiDi/RTL rendering - WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Nov 16, 2024
1 parent b9322bb commit 60256c7
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ tracing = "0.1.40"
tracing-chrome = "0.7.2"
tracing-subscriber = { version = "0.3.18", default-features = false }
tun2 = "4.0.0"
unicode-bidi = "0.3.17"
unicode-width = "0.2.0"
widestring = "1.0.2"
windows-sys = "0.52.0"
Expand Down
1 change: 1 addition & 0 deletions crates/trippy-tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ toml = { workspace = true, default-features = false, features = [ "parse" ] }
tracing-chrome.workspace = true
tracing-subscriber = { workspace = true, default-features = false, features = [ "env-filter", "json" ] }
tracing.workspace = true
unicode-bidi.workspace = true
unicode-width.workspace = true

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions crates/trippy-tui/src/frontend/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ impl Display for ColumnType {

impl ColumnType {
/// The name of the column in the current locale.
#[allow(clippy::cognitive_complexity)]
pub(self) fn name(&self) -> Cow<'_, str> {
match self {
Self::Ttl => Cow::Borrowed("#"),
Expand Down
1 change: 1 addition & 0 deletions crates/trippy-tui/src/frontend/render/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ fn render_settings_info(f: &mut Frame<'_>, app: &TuiApp, rect: Rect, info: &str)
}

/// Format all settings.
#[allow(clippy::cognitive_complexity)]
fn format_all_settings(app: &TuiApp) -> Vec<(String, String, Vec<SettingsItem>)> {
let tui_settings = format_tui_settings(app);
let trace_settings = format_trace_settings(app);
Expand Down
34 changes: 32 additions & 2 deletions crates/trippy-tui/src/locale.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use std::borrow::Cow;
use unicode_bidi::BidiInfo;

const FALLBACK_LOCALE: &str = "en";

const RTL_LOCALES: &[&str] = &["ar"];

/// Set the locale for the application.
///
/// If the given locale is `None` or unsupported, the system locale is tried. If the system locale
Expand Down Expand Up @@ -48,14 +53,39 @@ fn split_locale(locale: &str) -> String {
.to_string()
}

// A macro for translating a text string.
/// A macro for translating a text string.
///
/// If the current locale is right-to-left, the text is reordered.
#[macro_export]
macro_rules! t {
($($all:tt)*) => {
rust_i18n::t!($($all)*)
{
let translated = rust_i18n::t!($($all)*);
if $crate::locale::__is_rtl_locale() {
$crate::locale::__bidi_reorder_line(&translated)
} else {
translated
}
}
}
}

/// Check if the current locale is right-to-left.
pub fn __is_rtl_locale() -> bool {
let binding = rust_i18n::locale();
let locale = &*binding;
RTL_LOCALES.contains(&locale)
}

/// Reorder a line of text according to the current locale.
pub fn __bidi_reorder_line<'a>(translated: &str) -> Cow<'a, str> {
let bidi_info = BidiInfo::new(translated, None);
let para = &bidi_info.paragraphs[0];
let line = para.range.clone();
let display = bidi_info.reorder_line(para, line).to_string();
display.into()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 60256c7

Please sign in to comment.