tui: use whole keyboard mapping in select view as value

Thus all its values can easily be used later on. Involves making
`KeyboardMapping` partial-orderable, just use the human-readable name
here (as that's what displayed to the user).

Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
This commit is contained in:
Christoph Heiss 2023-06-15 11:49:55 +02:00
parent cae155237b
commit 86dac562e8
2 changed files with 20 additions and 11 deletions

View File

@ -1,4 +1,4 @@
use std::{collections::HashMap, fs::File, io::BufReader, path::Path};
use std::{cmp, collections::HashMap, fs::File, io::BufReader, path::Path};
use serde::{Deserialize, Deserializer};
@ -41,15 +41,21 @@ pub struct CountryInfo {
pub kmap: String,
}
#[derive(Clone, Deserialize)]
#[derive(Clone, Deserialize, Eq, PartialEq, Ord)]
pub struct KeyboardMapping {
pub name: String,
#[serde(rename = "kvm")]
pub id: String,
#[serde(rename = "x11")]
pub layout: String,
pub xkb_layout: String,
#[serde(rename = "x11var")]
pub variant: String,
pub xkb_variant: String,
}
impl cmp::PartialOrd for KeyboardMapping {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
self.name.partial_cmp(&other.name)
}
}
#[derive(Clone, Deserialize)]

View File

@ -1,5 +1,8 @@
use super::FormView;
use crate::{options::TimezoneOptions, setup::LocaleInfo};
use crate::{
options::TimezoneOptions,
setup::{KeyboardMapping, LocaleInfo},
};
use cursive::{
view::{Nameable, ViewWrapper},
views::{NamedView, SelectView},
@ -48,13 +51,13 @@ impl TimezoneOptionsView {
.kmap
.clone()
.into_values()
.map(|l| (l.name, l.id))
.collect::<Vec<(String, String)>>();
.map(|l| (l.name.clone(), l))
.collect::<Vec<(String, KeyboardMapping)>>();
kb_layouts.sort();
let kb_layout_selected_pos = kb_layouts
.iter()
.position(|l| l.1 == options.kb_layout)
.position(|l| l.1.id == options.kb_layout)
.unwrap_or_default();
let view = FormView::new()
@ -86,15 +89,15 @@ impl TimezoneOptionsView {
.get_value::<NamedView<SelectView>, _>(1)
.ok_or("failed to retrieve timezone")?;
let kb_layout = self
let kmap = self
.view
.get_value::<SelectView, _>(2)
.get_value::<SelectView<KeyboardMapping>, _>(2)
.ok_or("failed to retrieve keyboard layout")?;
Ok(TimezoneOptions {
country,
timezone,
kb_layout,
kb_layout: kmap.id,
})
}