From 35ab12999ae649411b2a3c56be918ae166db84cc Mon Sep 17 00:00:00 2001 From: vrubelroman Date: Fri, 23 Oct 2026 23:57:11 +0300 Subject: [PATCH] Desktop: tester's review of the unified view MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Guiding texts of a converted ChessBase database are texts again (§9.4): the converter marks them (CbText), the list shows «📄 title», the header counts them, «show texts» is offered, a text is no player and only tournament and annotator conditions keep it. - The PGN filters' index is built in the loading thread, not on the screen's. - Empty names last when sorting both ways (PGN and ChessBase). - The quick search finds a date as the table shows it (18.10.2026). - A position not applied for lack of memory: the arrow shows the order in effect. - «21 текст», «3 текста»: plural forms of games and texts in the header. Co-Authored-By: Claude Opus 5.5 --- apps/desktop/src/i18n.rs | 21 +++ apps/desktop/src/large_db.rs | 16 ++- apps/desktop/src/library.rs | 7 + crates/chess-import/src/chessbase/base.rs | 16 ++- crates/chess-import/src/chessbase/mod.rs | 4 +- crates/chess-import/src/lib.rs | 6 +- crates/chess-import/src/pgn_catalog.rs | 165 +++++++++++++++++++++- 7 files changed, 215 insertions(+), 20 deletions(-) diff --git a/apps/desktop/src/i18n.rs b/apps/desktop/src/i18n.rs index 416e760..2fcc0a9 100644 --- a/apps/desktop/src/i18n.rs +++ b/apps/desktop/src/i18n.rs @@ -119,6 +119,27 @@ pub fn games(n: usize) -> String { } } +/// "1 text", "21 texts" (guiding texts of a database), with Russian plural forms +/// ("1 текст", "3 текста", "5 текстов"). +pub fn texts(n: usize) -> String { + let num = crate::large_db::grouped(n); + match current() { + Lang::Ru => { + let word = match (n % 10, n % 100) { + (1, x) if x != 11 => "текст", + (2..=4, x) if !(12..=14).contains(&x) => "текста", + _ => "текстов", + }; + format!("{num} {word}") + } + Lang::En => format!("{num} {}", if n == 1 { "text" } else { "texts" }), + Lang::Fr => format!("{num} {}", if n <= 1 { "texte" } else { "textes" }), + Lang::De => format!("{num} {}", if n == 1 { "Text" } else { "Texte" }), + Lang::Es => format!("{num} {}", if n == 1 { "texto" } else { "textos" }), + Lang::It => format!("{num} {}", if n == 1 { "testo" } else { "testi" }), + } +} + /// "11,7 млн партий" / "267 партий": a database size in a short form. pub fn games_short(n: usize) -> String { if n >= 1_000_000 { diff --git a/apps/desktop/src/large_db.rs b/apps/desktop/src/large_db.rs index f0130bf..50ba6be 100644 --- a/apps/desktop/src/large_db.rs +++ b/apps/desktop/src/large_db.rs @@ -1498,11 +1498,12 @@ impl LargeView { &[("n", &grouped(self.view.len())), ("total", &grouped(total))], ) } - _ if db.texts() > 0 => trf( - "· {n} games · {t} texts", - &[("n", &grouped(db.games())), ("t", &grouped(db.texts()))], + _ if db.texts() > 0 => format!( + "· {} · {}", + crate::i18n::games(db.games()), + crate::i18n::texts(db.texts()) ), - _ => trf("· {n} games", &[("n", &grouped(db.games()))]), + _ => format!("· {}", crate::i18n::games(db.games())), }; let mut counter = RichText::new(counter); if progress.is_some() { @@ -1741,7 +1742,12 @@ impl LargeView { let mut hovered = None; let view = &self.view; let selected = self.selected; - let sort = self.effective_sort(); + // A position not applied (no memory) does not sort: the arrow shows the view's own order. + let sort = if self.blocked.is_some() { + self.sort + } else { + self.effective_sort() + }; let outcome = self.outcome.clone().filter(|_| use_position); let ordered = self .shown diff --git a/apps/desktop/src/library.rs b/apps/desktop/src/library.rs index bbb8a33..f6bad98 100644 --- a/apps/desktop/src/library.rs +++ b/apps/desktop/src/library.rs @@ -132,6 +132,13 @@ impl Library { }, &c, ); + // The filters' index of a PGN is built here, not on the screen's thread + // (a large download takes a second, §9.7). + if let Ok(opened) = &res + && let Database::Pgn(pgn) = &opened.db + { + pgn.catalog(); + } let _ = tx.send(res); ctx.request_repaint(); }); diff --git a/crates/chess-import/src/chessbase/base.rs b/crates/chess-import/src/chessbase/base.rs index bda5caf..24a9bd8 100644 --- a/crates/chess-import/src/chessbase/base.rs +++ b/crates/chess-import/src/chessbase/base.rs @@ -108,8 +108,13 @@ impl Names { self.shown.get(id as usize).map_or("", String::as_str) } - pub(crate) fn rank(&self, id: u32) -> u32 { - self.rank.get(id as usize).copied().unwrap_or(u32::MAX) + /// Alphabetical place of a name for sorting; an empty name has none (`u64::MAX`: + /// last in both directions, like a missing rating). + pub(crate) fn rank(&self, id: u32) -> u64 { + match self.rank.get(id as usize) { + Some(&r) if r != u32::MAX => u64::from(r), + _ => u64::MAX, + } } /// Ids whose name contains every word of `query`, as a lookup table. @@ -1211,8 +1216,8 @@ impl Base { let none_if_zero = |v: u32| if v == 0 { u64::MAX } else { u64::from(v) }; match sort { SortKey::Number => index as u64, - SortKey::White => self.players.rank(u(0x09, 3)).into(), - SortKey::Black => self.players.rank(u(0x0c, 3)).into(), + SortKey::White => self.players.rank(u(0x09, 3)), + SortKey::Black => self.players.rank(u(0x0c, 3)), SortKey::WhiteElo => none_if_zero(u(0x1f, 2)), SortKey::BlackElo => none_if_zero(u(0x21, 2)), SortKey::Result => match r.get(0x1b) { @@ -1225,8 +1230,7 @@ impl Base { SortKey::Eco => eco_number(u(0x23, 2) as u16).map_or(u64::MAX, u64::from), SortKey::Event => self .tournaments - .rank(if text { u(0x07, 3) } else { u(0x0f, 3) }) - .into(), + .rank(if text { u(0x07, 3) } else { u(0x0f, 3) }), SortKey::Date => none_if_zero(u(0x18, 3)), SortKey::AverageElo => { let (w, b) = (u(0x1f, 2), u(0x21, 2)); diff --git a/crates/chess-import/src/chessbase/mod.rs b/crates/chess-import/src/chessbase/mod.rs index b545ac5..cf2298b 100644 --- a/crates/chess-import/src/chessbase/mod.rs +++ b/crates/chess-import/src/chessbase/mod.rs @@ -172,8 +172,10 @@ pub fn convert(files: &Files) -> Result { } headers::Record::Text(t) => match text::guiding_text(cbg, t.text_offset, &decoder) { Ok(body) => { - let tags = + let mut tags = writer::text_tags(t, &body.title, &tournaments, &annotators, &decoder); + // The PGN of the library knows a guiding text by this tag (§9.4). + tags.push((crate::pgn_catalog::TEXT_TAG.into(), "1".into())); writer::text_game(&mut out.pgn, &tags, &body.text); out.texts += 1; } diff --git a/crates/chess-import/src/lib.rs b/crates/chess-import/src/lib.rs index 6a37c4c..c3a20aa 100644 --- a/crates/chess-import/src/lib.rs +++ b/crates/chess-import/src/lib.rs @@ -271,15 +271,15 @@ impl Database { /// Games (not guiding texts). pub fn games(&self) -> usize { match self { - Database::Pgn(db) => db.len(), + Database::Pgn(db) => db.len() - db.catalog().texts(), Database::ChessBase(b) => b.games(), } } - /// Guiding texts (only ChessBase databases have them). + /// Guiding texts (of a ChessBase database, or of one converted to PGN). pub fn texts(&self) -> usize { match self { - Database::Pgn(_) => 0, + Database::Pgn(db) => db.catalog().texts(), Database::ChessBase(b) => b.texts(), } } diff --git a/crates/chess-import/src/pgn_catalog.rs b/crates/chess-import/src/pgn_catalog.rs index cc88f8d..12bdd49 100644 --- a/crates/chess-import/src/pgn_catalog.rs +++ b/crates/chess-import/src/pgn_catalog.rs @@ -12,6 +12,15 @@ use crate::chessbase::base::{ words, }; +/// The tag the ChessBase converter gives a guiding text (its title is the `White` +/// tag): such a record is a text, not a game, as in the ChessBase database. +pub const TEXT_TAG: &str = "CbText"; + +/// A record of a PGN converted from ChessBase that was a guiding text. +pub fn is_text(e: &GameEntry) -> bool { + e.tag(TEXT_TAG).is_some() +} + /// What a filter and a sort need of each game, one array per field. pub struct Catalog { players: Names, @@ -38,6 +47,9 @@ pub struct Catalog { filled: Filled, /// Most games have no result: a book, whose continuations count chapters. books: bool, + /// Guiding texts (from a converted ChessBase database). + text: Vec, + texts: usize, } /// Fields at least one game of the database has. @@ -66,6 +78,18 @@ pub fn date_number(date: &str) -> u32 { year * 10000 + num(parts.next(), 12) * 100 + num(parts.next(), 31) } +/// yyyymmdd → the date as the table shows it ("18.10.2026", "10.2026", "2026"), for the +/// quick search. +fn shown_date(d: u32) -> String { + let (y, m, day) = (d / 10000, d / 100 % 100, d % 100); + match (y, m, day) { + (0, ..) => String::new(), + (y, 0, _) => y.to_string(), + (y, m, 0) => format!("{m:02}.{y}"), + (y, m, day) => format!("{day:02}.{m:02}.{y}"), + } +} + /// "13:05:09" → seconds of the day. fn seconds(time: &str) -> Option { let mut parts = time.trim().split(':').map(|p| p.parse::().ok()); @@ -185,10 +209,18 @@ fn intern(values: impl Iterator) -> (Vec, Vec) { impl Catalog { pub fn build(entries: &[GameEntry]) -> Catalog { // White and Black share one list: a player is the same whichever colour. + // A text has a title, not players. + let player = |e: &GameEntry, name: Option<&str>| { + if is_text(e) { + String::new() + } else { + known(name) + } + }; let (names, ids) = intern( entries .iter() - .flat_map(|e| [known(e.white()), known(e.black())]), + .flat_map(|e| [player(e, e.white()), player(e, e.black())]), ); let white: Vec = ids.iter().step_by(2).copied().collect(); let black: Vec = ids.iter().skip(1).step_by(2).copied().collect(); @@ -215,6 +247,8 @@ impl Catalog { }, ) .collect(); + let text: Vec = entries.iter().map(is_text).collect(); + let texts = text.iter().filter(|&&t| t).count(); let decided = entries .iter() .filter(|e| outcome(e.result()) != Outcome::Unknown) @@ -241,7 +275,9 @@ impl Catalog { annotator, date, when, - books: decided * 2 < entries.len(), + books: decided * 2 < entries.len() - texts, + text, + texts, white_elo, black_elo, result: entries.iter().map(|e| outcome(e.result())).collect(), @@ -261,6 +297,11 @@ impl Catalog { self.filled } + /// Guiding texts among the records. + pub fn texts(&self) -> usize { + self.texts + } + /// Most games have no result (a book of chapters). pub fn books(&self) -> bool { self.books @@ -284,6 +325,17 @@ impl Catalog { /// The table row of game `index`. pub fn row(entry: &GameEntry, moves: u32) -> Row { + if is_text(entry) { + return Row { + kind: Some(RecordKind::Text), + title: known(entry.white()), + event: known(entry.event()), + round: known(entry.tag("Round")), + annotator: entry.tag("Annotator").unwrap_or("").trim().to_owned(), + result: "*", + ..Row::default() + }; + } Row { kind: Some(RecordKind::Game), white: known(entry.white()), @@ -352,6 +404,18 @@ impl Catalog { }; let names = player_ids.is_some() || opponent_ids.is_some(); let relative = filter.player_relative && player_ids.is_some(); + // A guiding text has only a tournament and an annotator: any other condition + // leaves it out, as in a ChessBase database. + let game_only = names + || filter.year_from.is_some() + || filter.year_to.is_some() + || filter.elo_min.is_some() + || !filter.eco.is_empty() + || !filter.outcomes.is_empty() + || filter.moves_from.is_some() + || filter.moves_to.is_some() + || filter.with_comments + || filter.with_variations; let mut out = Vec::new(); for i in 0..self.white.len() { if i % 65536 == 0 { @@ -360,6 +424,22 @@ impl Catalog { } scanned.store(i, Ordering::Relaxed); } + if self.text[i] { + let at = |t: &Vec, id: u32| t.get(id as usize).copied().unwrap_or(false); + if !filter.games_only + && !game_only + && has(&event_ids, self.event[i]) + && annotator + .iter() + .all(|w| self.annotator[i].contains(w.as_str())) + && quick.iter().all(|(w, _, events)| { + at(events, self.event[i]) || self.annotator[i].contains(w.as_str()) + }) + { + out.push(i as u32); + } + continue; + } let (white, black) = (self.white[i], self.black[i]); let mut player_white = true; let mut player_black = true; @@ -447,6 +527,7 @@ impl Catalog { || at(players, black) || at(events, self.event[i]) || self.annotator[i].contains(w.as_str()) + || shown_date(self.date[i]).contains(w.as_str()) || e.is_some_and(|e| { ["Site", "ECO", "Date", "UTCDate"] .iter() @@ -487,11 +568,14 @@ impl Catalog { } fn sort_key(&self, i: usize, sort: SortKey) -> u64 { + if self.text[i] && !matches!(sort, SortKey::Number | SortKey::Event) { + return u64::MAX; + } let none_if_zero = |v: u32| if v == 0 { u64::MAX } else { u64::from(v) }; match sort { SortKey::Number => i as u64, - SortKey::White => self.players.rank(self.white[i]).into(), - SortKey::Black => self.players.rank(self.black[i]).into(), + SortKey::White => self.players.rank(self.white[i]), + SortKey::Black => self.players.rank(self.black[i]), SortKey::WhiteElo => none_if_zero(self.white_elo[i].into()), SortKey::BlackElo => none_if_zero(self.black_elo[i].into()), SortKey::Result => match self.result[i] { @@ -502,7 +586,7 @@ impl Catalog { }, SortKey::Moves => none_if_zero(self.moves[i].into()), SortKey::Eco => self.eco[i].map_or(u64::MAX, u64::from), - SortKey::Event => self.events.rank(self.event[i]).into(), + SortKey::Event => self.events.rank(self.event[i]), SortKey::Date => { if self.when[i] == 0 { u64::MAX @@ -816,6 +900,77 @@ mod tests { assert_eq!(v, vec![2, 1, 3, 0]); } + #[test] + fn guiding_texts_of_a_converted_chessbase_database() { + let (e, c) = catalog(concat!( + "[Event \"Course\"][White \"Strong and weak squares\"][Black \"\"][CbText \"1\"][Result \"*\"]\n{ A chapter. } *\n", + "[Event \"Course\"][White \"Karpov\"][Black \"Kasparov\"][Date \"1985.??.??\"][Result \"1-0\"]\n1. e4 1-0\n", + )); + assert_eq!(c.texts(), 1); + let r = Catalog::row(&e[0], 0); + assert_eq!( + (r.kind, r.title.as_str(), r.white.as_str()), + (Some(RecordKind::Text), "Strong and weak squares", "") + ); + // A text's title is no player. + assert_eq!(c.suggest_players("squares", 5).1, 0); + // Only the tournament and the annotator apply to a text. + assert_eq!( + find( + &e, + &c, + &Filter { + event: name("course"), + ..Filter::default() + } + ), + vec![0, 1] + ); + assert_eq!( + find( + &e, + &c, + &Filter { + year_from: Some(1900), + ..Filter::default() + } + ), + vec![1] + ); + assert_eq!( + find( + &e, + &c, + &Filter { + games_only: true, + ..Filter::default() + } + ), + vec![1] + ); + } + + #[test] + fn empty_names_come_last_both_ways() { + let (_, c) = catalog("[White \"B\"][Black \"?\"] *\n[White \"A\"][Black \"Z\"] *\n"); + let mut v = vec![0, 1]; + c.sort_records(&mut v, SortKey::Black, false); + assert_eq!(v, vec![1, 0]); + c.sort_records(&mut v, SortKey::Black, true); + assert_eq!(v, vec![1, 0]); + } + + #[test] + fn the_quick_search_finds_a_date_as_the_table_shows_it() { + let (e, c) = catalog(GAMES); + let text = |t: &str| Filter { + text: t.into(), + ..Filter::default() + }; + assert_eq!(find(&e, &c, &text("18.10.2026")), vec![0]); + assert_eq!(find(&e, &c, &text("2026-10")), vec![0]); + } + #[test] fn dates() { assert_eq!(date_number("2026-10-18"), 20261018);