From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH installer v2 1/3] tui: NumericEditView: add optional placeholder value
Date: Wed, 7 Feb 2024 15:28:12 +0100 [thread overview]
Message-ID: <20240207142824.2613933-2-c.heiss@proxmox.com> (raw)
In-Reply-To: <20240207142824.2613933-1-c.heiss@proxmox.com>
Enables to add an optional placeholder value to `NumericEditView`, which
will be displayed in a different (darker) color and not returned by
`.get_content*()`.
Can be used for having default values in the TUI, but with different
handling in the back.
Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
---
Changes v1 -> v2:
* new patch
proxmox-tui-installer/src/views/mod.rs | 70 ++++++++++++++++++++++++--
1 file changed, 66 insertions(+), 4 deletions(-)
diff --git a/proxmox-tui-installer/src/views/mod.rs b/proxmox-tui-installer/src/views/mod.rs
index 3244e76..299517e 100644
--- a/proxmox-tui-installer/src/views/mod.rs
+++ b/proxmox-tui-installer/src/views/mod.rs
@@ -2,9 +2,10 @@ use std::{net::IpAddr, rc::Rc, str::FromStr};
use cursive::{
event::{Event, EventResult},
+ theme::BaseColor,
view::{Resizable, ViewWrapper},
views::{EditView, LinearLayout, NamedView, ResizedView, SelectView, TextView},
- Rect, Vec2, View,
+ Printer, Rect, Vec2, View,
};
use proxmox_installer_common::utils::CidrAddress;
@@ -24,6 +25,7 @@ pub use timezone::*;
pub struct NumericEditView<T> {
view: LinearLayout,
max_value: Option<T>,
+ placeholder: Option<T>,
max_content_width: Option<usize>,
allow_empty: bool,
}
@@ -36,6 +38,7 @@ impl<T: Copy + ToString + FromStr + PartialOrd> NumericEditView<T> {
Self {
view,
max_value: None,
+ placeholder: None,
max_content_width: None,
allow_empty: false,
}
@@ -54,6 +57,7 @@ impl<T: Copy + ToString + FromStr + PartialOrd> NumericEditView<T> {
Self {
view,
max_value: None,
+ placeholder: None,
max_content_width: None,
allow_empty: false,
}
@@ -84,15 +88,42 @@ impl<T: Copy + ToString + FromStr + PartialOrd> NumericEditView<T> {
self
}
+ /// Sets a placeholder value for this view. Implies `allow_empty(true)`.
+ /// Implies `allow_empty(true)`.
+ ///
+ /// # Arguments
+ /// `placeholder` - The placeholder value to set for this view.
+ #[allow(unused)]
+ pub fn placeholder(mut self, placeholder: T) -> Self {
+ self.placeholder = Some(placeholder);
+ self.allow_empty(true)
+ }
+
+ /// Returns the current value of the view. If a placeholder is defined and
+ /// no value is currently set, the placeholder value is returned.
+ ///
+ /// **This should only be called when `allow_empty = false` or a placeholder
+ /// is set.**
pub fn get_content(&self) -> Result<T, <T as FromStr>::Err> {
- assert!(!self.allow_empty);
- self.inner().get_content().parse()
+ let content = self.inner().get_content();
+
+ if content.is_empty() {
+ if let Some(placeholder) = self.placeholder {
+ return Ok(placeholder);
+ }
+ }
+
+ assert!(!(self.allow_empty && self.placeholder.is_none()));
+ content.parse()
}
+ /// Returns the current value of the view, or [`None`] if the view is
+ /// currently empty.
pub fn get_content_maybe(&self) -> Option<Result<T, <T as FromStr>::Err>> {
let content = self.inner().get_content();
+
if !content.is_empty() {
- Some(self.inner().get_content().parse())
+ Some(content.parse())
} else {
None
}
@@ -157,6 +188,29 @@ impl<T: Copy + ToString + FromStr + PartialOrd> NumericEditView<T> {
std::mem::swap(self.inner_mut(), &mut inner);
self
}
+
+ /// Generic `wrap_draw()` implementation for [`ViewWrapper`].
+ ///
+ /// # Arguments
+ /// * `printer` - The [`Printer`] to draw to the base view.
+ fn wrap_draw_inner(&self, printer: &Printer) {
+ self.view.draw(printer);
+
+ if self.inner().get_content().is_empty() {
+ if let Some(placeholder) = self.placeholder {
+ let placeholder = placeholder.to_string();
+
+ printer.with_color(
+ (BaseColor::Blue.light(), BaseColor::Blue.dark()).into(),
+ |printer| printer.print((0, 0), &placeholder),
+ );
+
+ if printer.focused {
+ printer.print((placeholder.len(), 0), " ");
+ }
+ }
+ }
+ }
}
pub type FloatEditView = NumericEditView<f64>;
@@ -165,6 +219,10 @@ pub type IntegerEditView = NumericEditView<usize>;
impl ViewWrapper for FloatEditView {
cursive::wrap_impl!(self.view: LinearLayout);
+ fn wrap_draw(&self, printer: &Printer) {
+ self.wrap_draw_inner(printer);
+ }
+
fn wrap_on_event(&mut self, event: Event) -> EventResult {
let original = self.inner_mut().get_content();
@@ -204,6 +262,10 @@ impl FloatEditView {
impl ViewWrapper for IntegerEditView {
cursive::wrap_impl!(self.view: LinearLayout);
+ fn wrap_draw(&self, printer: &Printer) {
+ self.wrap_draw_inner(printer);
+ }
+
fn wrap_on_event(&mut self, event: Event) -> EventResult {
let original = self.inner_mut().get_content();
--
2.43.0
next prev parent reply other threads:[~2024-02-07 14:28 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-07 14:28 [pve-devel] [PATCH installer v2 0/3] expose zfs arc size setting for all products Christoph Heiss
2024-02-07 14:28 ` Christoph Heiss [this message]
2024-02-07 14:28 ` [pve-devel] [PATCH installer v2 2/3] tui: expose arc size setting for zfs bootdisks " Christoph Heiss
2024-02-07 14:28 ` [pve-devel] [PATCH installer v2 3/3] proxinstall: " Christoph Heiss
2024-02-23 15:37 ` Maximiliano Sandoval
2024-04-12 10:26 ` Christoph Heiss
2024-04-15 13:21 ` [pve-devel] [PATCH installer v2 0/3] expose zfs arc size setting " Christoph Heiss
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240207142824.2613933-2-c.heiss@proxmox.com \
--to=c.heiss@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox