[powerdebug,06/17] Factor out display code

Message ID 1308610705-23281-6-git-send-email-daniel.lezcano@linaro.org
State Accepted
Headers show

Commit Message

Daniel Lezcano June 20, 2011, 10:58 p.m.
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
---
 clocks.c    |   20 ++++++++++++++++++--
 display.c   |   40 +++-------------------------------------
 display.h   |    6 +-----
 regulator.c |   20 +++++++++++++++++++-
 sensor.c    |   17 ++++++++++++++++-
 5 files changed, 57 insertions(+), 46 deletions(-)

Patch

diff --git a/clocks.c b/clocks.c
index 684bf88..411fcb2 100644
--- a/clocks.c
+++ b/clocks.c
@@ -286,14 +286,30 @@  static int clock_print_info_cb(struct tree *t, void *data)
 	return 0;
 }
 
+static int clock_print_header(void)
+{
+	char *buf;
+	int ret;
+
+	if (asprintf(&buf, "%-55s %-16s %-12s %-9s %-8s",
+		     "Name", "Flags", "Rate", "Usecount", "Children") < 0)
+		return -1;
+
+	ret = display_header_footer(CLOCK, buf);
+
+	free(buf);
+
+	return ret;
+}
+
 static int clock_print_info(void)
 {
 	int ret, line = 0;
 
-	print_clock_header();
-
 	display_reset_cursor(CLOCK);
 
+	clock_print_header();
+
 	ret = tree_for_each(clock_tree, clock_print_info_cb, &line);
 
 	display_refresh_pad(CLOCK);
diff --git a/display.c b/display.c
index bd5971a..d32783f 100644
--- a/display.c
+++ b/display.c
@@ -375,49 +375,15 @@  int display_init(int wdefault)
 	return display_refresh(wdefault);
 }
 
-void print_regulator_header(void)
+int display_header_footer(int win, const char *line)
 {
 	werase(main_win);
 	wattron(main_win, A_BOLD);
-	mvwprintw(main_win, 0, 0, "Name");
-	mvwprintw(main_win, 0, 12, "Status");
-	mvwprintw(main_win, 0, 24, "State");
-	mvwprintw(main_win, 0, 36, "Type");
-	mvwprintw(main_win, 0, 48, "Users");
-	mvwprintw(main_win, 0, 60, "Microvolts");
-	mvwprintw(main_win, 0, 72, "Min u-volts");
-	mvwprintw(main_win, 0, 84, "Max u-volts");
+	mvwprintw(main_win, 0, 0, "%s", line);
 	wattroff(main_win, A_BOLD);
 	wrefresh(main_win);
 
-	show_header_footer(REGULATOR);
-}
-
-void print_clock_header(void)
-{
-	werase(main_win);
-	wattron(main_win, A_BOLD);
-	mvwprintw(main_win, 0, 0,  "Name");
-	mvwprintw(main_win, 0, 56, "Flags");
-	mvwprintw(main_win, 0, 75, "Rate");
-	mvwprintw(main_win, 0, 88, "Usecount");
-	mvwprintw(main_win, 0, 98, "Children");
-	wattroff(main_win, A_BOLD);
-	wrefresh(main_win);
-
-	show_header_footer(CLOCK);
-}
-
-void print_sensor_header(void)
-{
-	werase(main_win);
-	wattron(main_win, A_BOLD);
-	mvwprintw(main_win, 0, 0, "Name");
-	mvwprintw(main_win, 0, 36, "Value");
-	wattroff(main_win, A_BOLD);
-	wrefresh(main_win);
-
-	show_header_footer(SENSOR);
+	return show_header_footer(win);
 }
 
 int display_register(int win, struct display_ops *ops)
diff --git a/display.h b/display.h
index 749d8a3..8586f5e 100644
--- a/display.h
+++ b/display.h
@@ -29,9 +29,5 @@  extern void *display_get_row_data(int window);
 
 extern int display_init(int wdefault);
 extern int display_register(int win, struct display_ops *ops);
+extern int display_header_footer(int win, const char *line);
 extern int display_refresh(int win);
-
-/* FIXME */
-extern void print_sensor_header(void);
-extern void print_clock_header(void);
-extern void print_regulator_header(void);
diff --git a/regulator.c b/regulator.c
index 696ed10..4151fdb 100644
--- a/regulator.c
+++ b/regulator.c
@@ -138,13 +138,31 @@  static int regulator_display_cb(struct tree *t, void *data)
 	return 0;
 }
 
+static int regulator_print_header(void)
+{
+	char *buf;
+	int ret;
+
+	if (asprintf(&buf, "%-11s %-11s %-11s %-11s %-11s %-11s %-11s %-12s",
+		     "Name", "Status", "State", "Type", "Users", "Microvolts",
+		     "Min u-volts", "Max u-volts") < 0)
+		return -1;
+
+	ret = display_header_footer(REGULATOR, buf);
+
+	free(buf);
+
+	return ret;
+
+}
+
 int regulator_display(void)
 {
 	int ret, line = 0;
 
 	display_reset_cursor(REGULATOR);
 
-	print_regulator_header();
+	regulator_print_header();
 
 	ret = tree_for_each(reg_tree, regulator_display_cb, &line);
 
diff --git a/sensor.c b/sensor.c
index db58137..20602c0 100644
--- a/sensor.c
+++ b/sensor.c
@@ -235,13 +235,28 @@  static int sensor_display_cb(struct tree *t, void *data)
 	return 0;
 }
 
+static int sensor_print_header(void)
+{
+	char *buf;
+	int ret;
+
+	if (asprintf(&buf, "%-36s%s", "Name", "Value") < 0)
+		return -1;
+
+	ret = display_header_footer(SENSOR, buf);
+
+	free(buf);
+
+	return ret;
+}
+
 int sensor_display(void)
 {
 	int ret, line = 0;
 
 	display_reset_cursor(SENSOR);
 
-	print_sensor_header();
+	sensor_print_header();
 
 	ret = tree_for_each(sensor_tree, sensor_display_cb, &line);