Skip to content

Commit f66a4f6

Browse files
committed
Add a --frac option to output fractional values
1 parent 97e0c4c commit f66a4f6

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

brightnessctl.1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ Specify device class.
9696
Print version and exit.
9797
.RE
9898

99+
.sp
100+
\fB\-V, \-\-frac\fP
101+
.RS 4
102+
Output percentages as fractional values.
103+
.RE
104+
99105

100106
.SH "OPERATIONS"
101107

brightnessctl.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ struct params {
104104
bool percentage;
105105
bool save;
106106
bool restore;
107+
bool frac;
107108
float exponent;
108109
};
109110

@@ -123,6 +124,7 @@ static const struct option options[] = {
123124
{"restore", no_argument, NULL, 'r'},
124125
{"save", no_argument, NULL, 's'},
125126
{"version", no_argument, NULL, 'V'},
127+
{"frac", no_argument, NULL, 0},
126128
{NULL,}
127129
};
128130

@@ -192,6 +194,9 @@ int main(int argc, char **argv) {
192194
printf("%s\n", VERSION);
193195
exit(0);
194196
break;
197+
case 0:
198+
p.frac = true;
199+
break;
195200
default:
196201
phelp++;
197202
}
@@ -286,7 +291,7 @@ int apply_operation(struct device *dev, enum operation operation, struct value *
286291
return 0;
287292
case GET:
288293
if (p.percentage)
289-
fprintf(stdout, "%d\n", (int) val_to_percent(dev->curr_brightness, dev, true));
294+
fprintf(stdout, "%.4g\n", val_to_percent(dev->curr_brightness, dev, !p.frac));
290295
else
291296
fprintf(stdout, "%u\n", dev->curr_brightness);
292297
return 0;
@@ -381,12 +386,12 @@ unsigned long percent_to_val(float percent, struct device *d) {
381386
}
382387

383388
void print_device(struct device *dev) {
384-
char *format = p.mach ? "%s,%s,%d,%d%%,%d\n" :
385-
"Device '%s' of class '%s':\n\tCurrent brightness: %d (%d%%)\n\tMax brightness: %d\n\n";
389+
char *format = p.mach ? "%s,%s,%d,%.4g%%,%d\n" :
390+
"Device '%s' of class '%s':\n\tCurrent brightness: %d (%.4g%%)\n\tMax brightness: %d\n\n";
386391
fprintf(stdout, format,
387392
dev->id, dev->class,
388393
dev->curr_brightness,
389-
(int) val_to_percent(dev->curr_brightness, dev, true),
394+
val_to_percent(dev->curr_brightness, dev, !p.frac),
390395
dev->max_brightness);
391396
}
392397

@@ -738,6 +743,7 @@ Options:\n\
738743
-d, --device=DEVICE \tspecify device name (can be a wildcard).\n\
739744
-c, --class=CLASS \tspecify device class.\n\
740745
-V, --version \tprint version and exit.\n\
746+
--frac \tenable fractional percentage output.\n\
741747
\n\
742748
Operations:\n\
743749
i, info \tget device info.\n\

0 commit comments

Comments
 (0)