Remove units from numbers
This is a first step to decouple formatting from information because of two reasons: 1. The components should only gather and return the values by design 2. Fine grained user control should be a focus Scaling will be implemented in a different way in a later commit.
This commit is contained in:
		@@ -43,7 +43,7 @@
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return bprintf("%d%%", perc);
 | 
			
		||||
		return bprintf("%d", perc);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const char *
 | 
			
		||||
@@ -144,7 +144,7 @@
 | 
			
		||||
		struct apm_power_info apm_info;
 | 
			
		||||
 | 
			
		||||
		if (load_apm_power_info(&apm_info)) {
 | 
			
		||||
			return bprintf("%d%%", apm_info.battery_life);
 | 
			
		||||
			return bprintf("%d", apm_info.battery_life);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return NULL;
 | 
			
		||||
 
 | 
			
		||||
@@ -20,7 +20,7 @@
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return fmt_human_10(freq * 1000, "Hz");
 | 
			
		||||
		return fmt_human_10(freq * 1000);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const char *
 | 
			
		||||
@@ -39,7 +39,7 @@
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return bprintf("%d%%", (int)(100 *
 | 
			
		||||
		return bprintf("%d", (int)(100 *
 | 
			
		||||
		               ((b[0] + b[1] + b[2] + b[5] + b[6]) -
 | 
			
		||||
		                (a[0] + a[1] + a[2] + a[5] + a[6])) /
 | 
			
		||||
		               ((b[0] + b[1] + b[2] + b[3] + b[4] + b[5] + b[6]) -
 | 
			
		||||
@@ -67,7 +67,7 @@
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return fmt_human_10((size_t)freq * 1000 * 1000, "Hz");
 | 
			
		||||
		return fmt_human_10((size_t)freq * 1000 * 1000);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const char *
 | 
			
		||||
@@ -92,7 +92,7 @@
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return bprintf("%d%%", 100 *
 | 
			
		||||
		return bprintf("%d", 100 *
 | 
			
		||||
		               ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR]) -
 | 
			
		||||
		                (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR])) /
 | 
			
		||||
		               ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR] +
 | 
			
		||||
 
 | 
			
		||||
@@ -16,7 +16,7 @@ disk_free(const char *mnt)
 | 
			
		||||
		return NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return fmt_human_2(fs.f_frsize * fs.f_bavail, "B");
 | 
			
		||||
	return fmt_human_2(fs.f_frsize * fs.f_bavail);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const char *
 | 
			
		||||
@@ -29,7 +29,7 @@ disk_perc(const char *mnt)
 | 
			
		||||
		return NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return bprintf("%d%%", (int)(100 *
 | 
			
		||||
	return bprintf("%d", (int)(100 *
 | 
			
		||||
	               (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks))));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -43,7 +43,7 @@ disk_total(const char *mnt)
 | 
			
		||||
		return NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return fmt_human_2(fs.f_frsize * fs.f_blocks, "B");
 | 
			
		||||
	return fmt_human_2(fs.f_frsize * fs.f_blocks);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const char *
 | 
			
		||||
@@ -56,5 +56,5 @@ disk_used(const char *mnt)
 | 
			
		||||
		return NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return fmt_human_2(fs.f_frsize * (fs.f_blocks - fs.f_bfree), "B");
 | 
			
		||||
	return fmt_human_2(fs.f_frsize * (fs.f_blocks - fs.f_bfree));
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -29,8 +29,7 @@
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return fmt_human_2((rxbytes - oldrxbytes) *
 | 
			
		||||
		                   1000 / interval, "B/s");
 | 
			
		||||
		return fmt_human_2((rxbytes - oldrxbytes) * 1000 / interval);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const char *
 | 
			
		||||
@@ -55,8 +54,7 @@
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return fmt_human_2((txbytes - oldtxbytes) *
 | 
			
		||||
		                   1000 / interval, "B/s");
 | 
			
		||||
		return fmt_human_2((txbytes - oldtxbytes) * 1000 / interval);
 | 
			
		||||
	}
 | 
			
		||||
#elif defined(__OpenBSD__)
 | 
			
		||||
	#include <string.h>
 | 
			
		||||
@@ -97,8 +95,7 @@
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return fmt_human_2((rxbytes - oldrxbytes) *
 | 
			
		||||
		                   1000 / interval, "B/s");
 | 
			
		||||
		return fmt_human_2((rxbytes - oldrxbytes) * 1000 / interval);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const char *
 | 
			
		||||
@@ -133,7 +130,6 @@
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return fmt_human_2((txbytes - oldtxbytes) *
 | 
			
		||||
		                   1000 / interval, "B/s");
 | 
			
		||||
		return fmt_human_2((txbytes - oldtxbytes) * 1000 / interval);
 | 
			
		||||
	}
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,7 @@
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return fmt_human_2(free * 1024, "B");
 | 
			
		||||
		return fmt_human_2(free * 1024);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const char *
 | 
			
		||||
@@ -34,8 +34,8 @@
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return bprintf("%d%%", 100 * ((total - free) -
 | 
			
		||||
		                              (buffers + cached)) / total);
 | 
			
		||||
		return bprintf("%d", 100 * ((total - free) -
 | 
			
		||||
		                            (buffers + cached)) / total);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const char *
 | 
			
		||||
@@ -48,7 +48,7 @@
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return fmt_human_2(total * 1024, "B");
 | 
			
		||||
		return fmt_human_2(total * 1024);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const char *
 | 
			
		||||
@@ -65,8 +65,7 @@
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return fmt_human_2((total - free - buffers - cached) * 1024,
 | 
			
		||||
		                   "B");
 | 
			
		||||
		return fmt_human_2((total - free - buffers - cached) * 1024);
 | 
			
		||||
	}
 | 
			
		||||
#elif defined(__OpenBSD__)
 | 
			
		||||
	#include <stdlib.h>
 | 
			
		||||
@@ -96,7 +95,8 @@
 | 
			
		||||
 | 
			
		||||
		if (load_uvmexp(&uvmexp)) {
 | 
			
		||||
			free_pages = uvmexp.npages - uvmexp.active;
 | 
			
		||||
			return fmt_human_2(pagetok(free_pages, uvmexp.pageshift) * 1024, "B");
 | 
			
		||||
			return fmt_human_2(pagetok(free_pages,
 | 
			
		||||
			                           uvmexp.pageshift) * 1024);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return NULL;
 | 
			
		||||
@@ -110,7 +110,7 @@
 | 
			
		||||
 | 
			
		||||
		if (load_uvmexp(&uvmexp)) {
 | 
			
		||||
			percent = uvmexp.active * 100 / uvmexp.npages;
 | 
			
		||||
			return bprintf("%d%%", percent);
 | 
			
		||||
			return bprintf("%d", percent);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return NULL;
 | 
			
		||||
@@ -122,7 +122,8 @@
 | 
			
		||||
		struct uvmexp uvmexp;
 | 
			
		||||
 | 
			
		||||
		if (load_uvmexp(&uvmexp)) {
 | 
			
		||||
			return fmt_human_2(pagetok(uvmexp.npages, uvmexp.pageshift) * 1024, "B");
 | 
			
		||||
			return fmt_human_2(pagetok(uvmexp.npages,
 | 
			
		||||
			                           uvmexp.pageshift) * 1024);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return NULL;
 | 
			
		||||
@@ -134,7 +135,8 @@
 | 
			
		||||
		struct uvmexp uvmexp;
 | 
			
		||||
 | 
			
		||||
		if (load_uvmexp(&uvmexp)) {
 | 
			
		||||
			return fmt_human_2(pagetok(uvmexp.active, uvmexp.pageshift) * 1024, "B");
 | 
			
		||||
			return fmt_human_2(pagetok(uvmexp.active,
 | 
			
		||||
			                           uvmexp.pageshift) * 1024);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return NULL;
 | 
			
		||||
 
 | 
			
		||||
@@ -48,7 +48,7 @@
 | 
			
		||||
		}
 | 
			
		||||
		sscanf(match, "SwapFree: %ld kB\n", &free);
 | 
			
		||||
 | 
			
		||||
		return fmt_human_2(free * 1024, "B");
 | 
			
		||||
		return fmt_human_2(free * 1024);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const char *
 | 
			
		||||
@@ -80,7 +80,7 @@
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return bprintf("%d%%", 100 * (total - free - cached) / total);
 | 
			
		||||
		return bprintf("%d", 100 * (total - free - cached) / total);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const char *
 | 
			
		||||
@@ -98,7 +98,7 @@
 | 
			
		||||
		}
 | 
			
		||||
		sscanf(match, "SwapTotal: %ld kB\n", &total);
 | 
			
		||||
 | 
			
		||||
		return fmt_human_2(total * 1024, "B");
 | 
			
		||||
		return fmt_human_2(total * 1024);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const char *
 | 
			
		||||
@@ -126,7 +126,7 @@
 | 
			
		||||
		}
 | 
			
		||||
		sscanf(match, "SwapFree: %ld kB\n", &free);
 | 
			
		||||
 | 
			
		||||
		return fmt_human_2((total - free - cached) * 1024, "B");
 | 
			
		||||
		return fmt_human_2((total - free - cached) * 1024);
 | 
			
		||||
	}
 | 
			
		||||
#elif defined(__OpenBSD__)
 | 
			
		||||
	#include <stdlib.h>
 | 
			
		||||
@@ -178,7 +178,7 @@
 | 
			
		||||
 | 
			
		||||
		getstats(&total, &used);
 | 
			
		||||
 | 
			
		||||
		return fmt_human_2((total - used) * 1024, "B");
 | 
			
		||||
		return fmt_human_2((total - used) * 1024);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const char *
 | 
			
		||||
@@ -192,7 +192,7 @@
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return bprintf("%d%%", 100 * used / total);
 | 
			
		||||
		return bprintf("%d", 100 * used / total);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const char *
 | 
			
		||||
@@ -202,7 +202,7 @@
 | 
			
		||||
 | 
			
		||||
		getstats(&total, &used);
 | 
			
		||||
 | 
			
		||||
		return fmt_human_2(total * 1024, "B");
 | 
			
		||||
		return fmt_human_2(total * 1024);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const char *
 | 
			
		||||
@@ -212,6 +212,6 @@
 | 
			
		||||
 | 
			
		||||
		getstats(&total, &used);
 | 
			
		||||
 | 
			
		||||
		return fmt_human_2(used * 1024, "B");
 | 
			
		||||
		return fmt_human_2(used * 1024);
 | 
			
		||||
	}
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -13,7 +13,7 @@
 | 
			
		||||
			return NULL;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return bprintf("%d°C", temp / 1000);
 | 
			
		||||
		return bprintf("%d", temp / 1000);
 | 
			
		||||
	}
 | 
			
		||||
#elif defined(__OpenBSD__)
 | 
			
		||||
	#include <errno.h>
 | 
			
		||||
@@ -44,6 +44,6 @@
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		/* kelvin to celsius */
 | 
			
		||||
		return bprintf("%d°C", (temp.value - 273150000) / 1000000);
 | 
			
		||||
		return bprintf("%d", (temp.value - 273150000) / 1000000);
 | 
			
		||||
	}
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -42,5 +42,5 @@ vol_perc(const char *card)
 | 
			
		||||
 | 
			
		||||
	close(afd);
 | 
			
		||||
 | 
			
		||||
	return bprintf("%d%%", v & 0xff);
 | 
			
		||||
	return bprintf("%d", v & 0xff);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -60,7 +60,7 @@
 | 
			
		||||
		sscanf(datastart + 1, " %*d   %d  %*d  %*d\t\t  %*d\t   "
 | 
			
		||||
		       "%*d\t\t%*d\t\t %*d\t  %*d\t\t %*d", &cur);
 | 
			
		||||
 | 
			
		||||
		return bprintf("%d%%", (int)((float)cur / total * 100));
 | 
			
		||||
		return bprintf("%d", (int)((float)cur / total * 100));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const char *
 | 
			
		||||
@@ -147,7 +147,7 @@
 | 
			
		||||
				q = nr.nr_rssi >= -50 ? 100 : (nr.nr_rssi <= -100 ? 0 :
 | 
			
		||||
				(2 * (nr.nr_rssi + 100)));
 | 
			
		||||
			}
 | 
			
		||||
			return bprintf("%d%%", q);
 | 
			
		||||
			return bprintf("%d", q);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return NULL;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										8
									
								
								util.c
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								util.c
									
									
									
									
									
								
							@@ -87,7 +87,7 @@ bprintf(const char *fmt, ...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const char *
 | 
			
		||||
fmt_human_2(size_t num, char *unit)
 | 
			
		||||
fmt_human_2(size_t num)
 | 
			
		||||
{
 | 
			
		||||
	size_t i;
 | 
			
		||||
	double scaled;
 | 
			
		||||
@@ -99,11 +99,11 @@ fmt_human_2(size_t num, char *unit)
 | 
			
		||||
		scaled /= 1024.0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return bprintf("%.1f%s%s", scaled, prefix[i], unit);
 | 
			
		||||
	return bprintf("%.1f%s", scaled, prefix[i]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const char *
 | 
			
		||||
fmt_human_10(size_t num, char *unit)
 | 
			
		||||
fmt_human_10(size_t num)
 | 
			
		||||
{
 | 
			
		||||
	size_t i;
 | 
			
		||||
	double scaled;
 | 
			
		||||
@@ -115,7 +115,7 @@ fmt_human_10(size_t num, char *unit)
 | 
			
		||||
		scaled /= 1000.0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return bprintf("%.1f%s%s", scaled, prefix[i], unit);
 | 
			
		||||
	return bprintf("%.1f%s", scaled, prefix[i]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								util.h
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								util.h
									
									
									
									
									
								
							@@ -10,6 +10,6 @@ void die(const char *, ...);
 | 
			
		||||
 | 
			
		||||
int esnprintf(char *str, size_t size, const char *fmt, ...);
 | 
			
		||||
const char *bprintf(const char *fmt, ...);
 | 
			
		||||
const char *fmt_human_2(size_t num, char *unit);
 | 
			
		||||
const char *fmt_human_10(size_t num, char *unit);
 | 
			
		||||
const char *fmt_human_2(size_t num);
 | 
			
		||||
const char *fmt_human_10(size_t num);
 | 
			
		||||
int pscanf(const char *path, const char *fmt, ...);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user