Skip to content

Commit 44bdd99

Browse files
committed
refactor title work
1 parent 02275b8 commit 44bdd99

File tree

7 files changed

+260
-141
lines changed

7 files changed

+260
-141
lines changed

docs/docs/config/title.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ Rio allows you to configure window and tabs title through configuration via temp
77

88
- `content` - Configure window title using template
99

10-
- Default: `{{ TITLE || PROGRAM }}`
10+
- Default: `{{ title || program }}`
1111

1212
- `placeholder` - Configure initial title
1313

1414
- Default: ``
1515

1616
## content
1717

18+
Note: Variables are not case sensitive.
19+
1820
Possible options:
1921

2022
- `TITLE`: terminal title via OSC sequences for setting terminal title
@@ -37,7 +39,7 @@ Result: `fish - .../Documents/a/rio`.
3739

3840
```toml
3941
[title]
40-
content = "{{ PROGRAM }} ({{COLUMNS}}x{{LINES}})"
42+
content = "{{ program }} ({{columns}}x{{lines}})"
4143
```
4244

4345
Result: `fish (85x23)`.

frontends/rioterm/src/context/mod.rs

+27-99
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
pub mod grid;
22
pub mod renderable;
3-
mod title;
3+
pub mod title;
44

55
use crate::ansi::CursorShape;
66
use crate::context::grid::ContextDimension;
77
use crate::context::grid::ContextGrid;
88
use crate::context::grid::Delta;
9+
use crate::context::title::{update_title, ContextManagerTitles};
910
use crate::event::sync::FairMutex;
1011
use crate::event::RioEvent;
1112
use crate::ime::Ime;
@@ -21,7 +22,6 @@ use rio_backend::event::WindowId;
2122
use rio_backend::selection::SelectionRange;
2223
use rio_backend::sugarloaf::{font::SugarloafFont, Object, SugarloafErrors};
2324
use std::borrow::Cow;
24-
use std::collections::HashMap;
2525
use std::error::Error;
2626
use std::sync::Arc;
2727
use std::time::{Duration, Instant};
@@ -130,43 +130,7 @@ pub struct ContextManagerConfig {
130130
pub is_native: bool,
131131
pub should_update_titles: bool,
132132
pub split_color: [f32; 4],
133-
}
134-
135-
pub struct ContextManagerTitles {
136-
last_title_update: Option<Instant>,
137-
pub titles: HashMap<usize, [String; 3]>,
138-
pub key: String,
139-
}
140-
141-
impl ContextManagerTitles {
142-
pub fn new(
143-
idx: usize,
144-
program: String,
145-
terminal_title: String,
146-
path: String,
147-
) -> ContextManagerTitles {
148-
ContextManagerTitles {
149-
key: format!("{}{}{};", idx, program, terminal_title),
150-
titles: HashMap::from([(idx, [program, terminal_title, path])]),
151-
last_title_update: None,
152-
}
153-
}
154-
155-
#[inline]
156-
pub fn set_key_val(
157-
&mut self,
158-
idx: usize,
159-
program: String,
160-
terminal_title: String,
161-
path: String,
162-
) {
163-
self.titles.insert(idx, [program, terminal_title, path]);
164-
}
165-
166-
#[inline]
167-
pub fn set_key(&mut self, key: String) {
168-
self.key = key;
169-
}
133+
pub title: rio_backend::config::title::Title,
170134
}
171135

172136
pub struct ContextManager<T: EventListener> {
@@ -236,7 +200,7 @@ pub fn create_mock_context<
236200
is_native: false,
237201
should_update_titles: false,
238202
use_current_path: false,
239-
split_color: [0., 0., 0., 0.],
203+
..ContextManagerConfig::default()
240204
};
241205
ContextManager::create_context(
242206
(&Cursor::default(), false),
@@ -408,7 +372,6 @@ impl<T: EventListener + Clone + std::marker::Send + 'static> ContextManager<T> {
408372
let titles = ContextManagerTitles::new(
409373
0,
410374
String::from("tab"),
411-
String::new(),
412375
ctx_config.working_dir.clone().unwrap_or_default(),
413376
);
414377

@@ -462,7 +425,7 @@ impl<T: EventListener + Clone + std::marker::Send + 'static> ContextManager<T> {
462425
is_native: false,
463426
should_update_titles: false,
464427
use_current_path: false,
465-
split_color: [0., 0., 0., 0.],
428+
..ContextManagerConfig::default()
466429
};
467430
let initial_context = ContextManager::create_context(
468431
(&Cursor::default(), false),
@@ -474,8 +437,7 @@ impl<T: EventListener + Clone + std::marker::Send + 'static> ContextManager<T> {
474437
&config,
475438
)?;
476439

477-
let titles =
478-
ContextManagerTitles::new(0, String::new(), String::new(), String::new());
440+
let titles = ContextManagerTitles::new(0, String::new(), String::new());
479441

480442
Ok(ContextManager {
481443
current_index: 0,
@@ -693,64 +655,29 @@ impl<T: EventListener + Clone + std::marker::Send + 'static> ContextManager<T> {
693655
.unwrap_or(true)
694656
{
695657
self.titles.last_title_update = Some(Instant::now());
696-
#[cfg(unix)]
697-
{
698-
let mut id = String::default();
699-
for (i, context) in self.contexts.iter_mut().enumerate() {
700-
let program = teletypewriter::foreground_process_name(
701-
*context.current().main_fd,
702-
context.current().shell_pid,
703-
);
704-
705-
let path = teletypewriter::foreground_process_path(
706-
*context.current().main_fd,
707-
context.current().shell_pid,
708-
)
709-
.map(|p| p.to_string_lossy().to_string())
710-
.unwrap_or_default();
711-
712-
let terminal_title = {
713-
let terminal = context.current().terminal.lock();
714-
terminal.title.to_string()
715-
};
716-
717-
let window_title = if terminal_title.is_empty() {
718-
program.to_owned()
719-
} else {
720-
format!("{} ({})", terminal_title, program)
721-
};
722-
723-
if cfg!(target_os = "macos") {
724-
self.event_proxy.send_event(
725-
RioEvent::TitleWithSubtitle(window_title, path.clone()),
726-
self.window_id,
727-
);
728-
} else {
729-
self.event_proxy
730-
.send_event(RioEvent::Title(window_title), self.window_id);
731-
}
658+
let mut id = String::default();
659+
for (i, context) in self.contexts.iter_mut().enumerate() {
660+
let content = update_title(&self.config.title.content, context.current());
661+
662+
#[cfg(unix)]
663+
let path = teletypewriter::foreground_process_path(
664+
*context.current().main_fd,
665+
context.current().shell_pid,
666+
)
667+
.map(|p| p.to_string_lossy().to_string())
668+
.unwrap_or_default();
732669

733-
id.push_str(&format!("{}{}{};", i, program, terminal_title));
734-
self.titles.set_key_val(i, program, terminal_title, path);
735-
}
736-
self.titles.set_key(id);
737-
}
670+
#[cfg(not(unix))]
671+
let path = String::default();
738672

739-
#[cfg(not(unix))]
740-
{
741-
let mut id = String::from("");
742-
for (i, _context) in self.contexts.iter().enumerate() {
743-
let program = self.config.shell.program.to_owned();
744-
id.push_str(&format!("{}{}{};", i, program, String::default()));
745-
self.titles.set_key_val(
746-
i,
747-
program,
748-
String::default(),
749-
String::default(),
750-
);
751-
}
752-
self.titles.set_key(id);
673+
self.event_proxy
674+
.send_event(RioEvent::Title(content.to_owned()), self.window_id);
675+
676+
id.push_str(&format!("{}{};", i, content));
677+
self.titles.set_key_val(i, content, path);
753678
}
679+
680+
self.titles.set_key(id);
754681
}
755682
}
756683

@@ -985,6 +912,7 @@ impl<T: EventListener + Clone + std::marker::Send + 'static> ContextManager<T> {
985912
should_update_titles: !(config.navigation.is_collapsed_mode()
986913
&& config.navigation.color_automation.is_empty()),
987914
split_color: config.colors.split,
915+
title: config.title,
988916
};
989917

990918
self.acc_current_route += 1;

0 commit comments

Comments
 (0)