Skip to content

Commit d9c7ada

Browse files
committed
Apply fallback style for quirky terminals
1 parent 2cf01f2 commit d9c7ada

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020

2121
### Changed
2222

23+
- The visual style in certain terminals no longer uses emojis (#173)
24+
2325
### Fixed
2426

2527
- The generated project no longer contains `template.yaml`. (#142)

src/tui.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl<'app> Repository<'app> {
115115
self.path.pop();
116116
}
117117

118-
fn current_level_desc(&self, width: u16) -> Vec<(bool, String)> {
118+
fn current_level_desc(&self, width: u16, style: &UiStyle) -> Vec<(bool, String)> {
119119
let level = self.current_level();
120120
let level_active = self.current_level_is_active();
121121

@@ -129,11 +129,11 @@ impl<'app> Repository<'app> {
129129
};
130130
let indicator =
131131
if self.config.selected.iter().any(|o| o == v.name()) && level_active {
132-
"✅"
132+
style.selected
133133
} else if v.is_category() {
134-
"▶️"
134+
style.category
135135
} else {
136-
" "
136+
style.unselected
137137
};
138138
let padding = width as usize - v.title().len() - 4; // 2 spaces + the indicator
139139
(
@@ -165,21 +165,53 @@ pub fn restore_terminal() -> AppResult<()> {
165165
Ok(())
166166
}
167167

168+
struct UiStyle {
169+
selected: &'static str,
170+
unselected: &'static str,
171+
category: &'static str,
172+
}
173+
174+
impl UiStyle {
175+
const FANCY: Self = Self {
176+
selected: "✅",
177+
unselected: " ",
178+
category: "▶️",
179+
};
180+
const FALLBACK: Self = Self {
181+
selected: "*",
182+
unselected: " ",
183+
category: ">",
184+
};
185+
186+
const NEEDS_FALLBACK: &[&str] = &["vscode", "Apple_Terminal"];
187+
}
188+
168189
pub struct App<'app> {
169190
state: Vec<ListState>,
170191
repository: Repository<'app>,
171192
confirm_quit: bool,
193+
ui_style: UiStyle,
172194
}
173195

174196
impl<'app> App<'app> {
175197
pub fn new(repository: Repository<'app>) -> Self {
176198
let mut initial_state = ListState::default();
177199
initial_state.select(Some(0));
178200

201+
let use_fallback_style = match std::env::var("TERM_PROGRAM").as_deref() {
202+
Ok(s) => UiStyle::NEEDS_FALLBACK.contains(&s),
203+
_ => true,
204+
};
205+
179206
Self {
180207
repository,
181208
state: vec![initial_state],
182209
confirm_quit: false,
210+
ui_style: if use_fallback_style {
211+
UiStyle::FALLBACK
212+
} else {
213+
UiStyle::FANCY
214+
},
183215
}
184216
}
185217
pub fn selected(&self) -> usize {
@@ -326,7 +358,7 @@ impl App<'_> {
326358
// Iterate through all elements in the `items` and stylize them.
327359
let items: Vec<ListItem> = self
328360
.repository
329-
.current_level_desc(area.width)
361+
.current_level_desc(area.width, &self.ui_style)
330362
.into_iter()
331363
.map(|(enabled, value)| {
332364
ListItem::new(value).style(if enabled {

0 commit comments

Comments
 (0)