Skip to contentSkip to conversation

master - display: add display_percent function

PATCH Zdenek Kabelac 1 participant 1 message Jun 24, 2017

lore
Share & tools
lore
#0 lore raw
Gitweb:        https://sourceware.org/git/?p=lvm2.git;a=commitdiff;h=07eec06f5d7f7a9589b8555df7e67d211dcd3257
Commit:        07eec06f5d7f7a9589b8555df7e67d211dcd3257
Parent:        feed61f3fa17a94140ba91ae5df844ee4ad34248
Author:        Zdenek Kabelac <[email protected]>
AuthorDate:    Sat Jun 24 16:03:21 2017 +0200
Committer:     Zdenek Kabelac <[email protected]>
CommitterDate: Sat Jun 24 17:44:42 2017 +0200

display: add display_percent function

Add universal function to print dm_percent_t values via single code path.

TODO: extend with configurale precision of printed values.

---
 WHATS_NEW             |    1 +
 lib/display/display.c |   26 +++++++++++++++++++++++++-
 lib/display/display.h |    4 +++-
 3 files changed, 29 insertions(+), 2 deletions(-)
WHATS_NEW +1 -0
index fa51f67..6a60f0b 100644
@@ -1,5 +1,6 @@
 Version 2.02.172 - 
 ===============================
+  Add display_percent helper function for printing percent values.
   Lvconvert --repair handles failing raid legs (present but marked 'D'ead).
   Do not lvdisplay --maps unset settings of cache pool.
   Fix lvdisplay --maps for cache pool without policy settings.
lib/display/display.c +25 -1
index a8bb5cc..5940cf0 100644
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
- * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2004-2017 Red Hat, Inc. All rights reserved.
  *
  * This file is part of LVM2.
  *
@@ -152,6 +152,30 @@ const char *display_lvname(const struct logical_volume *lv)
 	return name;
 }
 
+/* Display percentage with (TODO) configurable precision */
+const char *display_percent(struct cmd_context *cmd, dm_percent_t percent)
+{
+	char *buf;
+	int r;
+
+        /* Reusing same  ring buffer we use for displaying LV names */
+	if ((cmd->display_lvname_idx + NAME_LEN) >= sizeof((cmd->display_buffer)))
+		cmd->display_lvname_idx = 0;
+
+	buf = cmd->display_buffer + cmd->display_lvname_idx;
+	/* TODO: Make configurable hardcoded 2 digits */
+	r = dm_snprintf(buf, NAME_LEN, "%.2f", dm_percent_to_round_float(percent, 2));
+
+	if (r < 0) {
+		log_error("Percentage %d does not fit.", percent);
+		return NULL;
+	}
+
+	cmd->display_lvname_idx += r + 1;
+
+	return buf;
+}
+
 /* Size supplied in sectors */
 static const char *_display_size(const struct cmd_context *cmd,
 				 uint64_t size, dm_size_suffix_t suffix_type)
lib/display/display.h +3 -1
index 1fc9079..9f9afe9 100644
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
- * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2004-2017 Red Hat, Inc. All rights reserved.
  *
  * This file is part of LVM2.
  *
@@ -24,6 +24,8 @@
 
 const char *display_lvname(const struct logical_volume *lv);
 
+const char *display_percent(struct cmd_context *cmd, dm_percent_t percent);
+
 /* Specify size in KB */
 const char *display_size(const struct cmd_context *cmd, uint64_t size);
 const char *display_size_long(const struct cmd_context *cmd, uint64_t size);