Remember the TUI server selection

This commit is contained in:
Georg Bauer
2026-08-05 17:50:09 +02:00
parent 61e96cc6cf
commit c1cfaa3d23
4 changed files with 101 additions and 1 deletions

View File

@@ -257,6 +257,15 @@ impl App {
})
}
pub fn persist_selected_server(&mut self) -> Result<(), String> {
let selected = self
.config
.servers
.contains_key(&self.server_name)
.then_some(self.server_name.as_str());
self.config.set_tui_last_server(selected)
}
pub async fn handle_key(&mut self, key: KeyEvent) {
self.last_input = Instant::now();
self.status_deadline = None;
@@ -1675,6 +1684,14 @@ fn initial_selection(config: &Config, requested: Option<&str>) -> Result<Selecti
if let Some(name) = requested {
return config.select(Some(name), None);
}
if let Some(name) = config
.tui
.last_server
.as_deref()
.filter(|name| config.servers.contains_key(*name))
{
return config.select(Some(name), None);
}
config.select(None, None).or_else(|_| {
let name = config
.servers
@@ -1929,6 +1946,30 @@ fn clock() -> String {
mod tests {
use super::*;
fn config_with_servers() -> Config {
let mut config = Config::default();
config.servers = [
(
"first.example".into(),
gotcha_gitea::ServerProfile {
url: "https://first.example".into(),
token: "first".into(),
provider: Provider::Gitea,
},
),
(
"second.example".into(),
gotcha_gitea::ServerProfile {
url: "https://second.example".into(),
token: "second".into(),
provider: Provider::Forgejo,
},
),
]
.into();
config
}
#[test]
fn changed_files_open_only_their_own_diff() {
let diff = "diff --git a/one.txt b/one.txt\n--- a/one.txt\n+++ b/one.txt\n+one\ndiff --git a/two.txt b/two.txt\n--- a/two.txt\n+++ b/two.txt\n+two\n";
@@ -1941,6 +1982,29 @@ mod tests {
);
}
#[test]
fn startup_prefers_the_last_server_unless_explicitly_overridden() {
let mut config = config_with_servers();
config.tui.last_server = Some("second.example".into());
assert_eq!(
initial_selection(&config, None).unwrap().name.as_deref(),
Some("second.example")
);
assert_eq!(
initial_selection(&config, Some("first.example"))
.unwrap()
.name
.as_deref(),
Some("first.example")
);
config.tui.last_server = Some("removed.example".into());
assert_eq!(
initial_selection(&config, None).unwrap().name.as_deref(),
Some("first.example")
);
}
#[test]
fn list_clicks_follow_the_rendered_offset_and_rows() {
let area = ratatui::layout::Rect::new(10, 4, 30, 10);

View File

@@ -44,7 +44,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
let result = run(&mut terminal, &mut app).await;
execute!(io::stdout(), DisableMouseCapture)?;
ratatui::restore();
result
result?;
app.persist_selected_server()?;
Ok(())
}
async fn run(