Re: [hackers] [slstatus][PATCH] Add flexible formatting to keyboard_indicators.

2018-06-04 Thread Aaron Marcher

Ian,

thanks for your work, merged.

Regards,
Aaron

--
Web: https://drkhsh.at/ or http://drkhsh5rv6pnahas.onion/
Gopher: gopher://drkhsh.at or gopher://drkhsh5rv6pnahas.onion
GPG: 0x7A65E38D55BE96FE
Fingerprint: 4688 907C 8720 3318 0D9F AFDE 7A65 E38D 55BE 96FE



[hackers] [slstatus][PATCH] Add flexible formatting to keyboard_indicators.

2018-06-03 Thread Ian Remmler
Updated for style.

---
 components/keyboard_indicators.c | 38 +++-
 config.def.h |  3 ++-
 2 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/components/keyboard_indicators.c b/components/keyboard_indicators.c
index 73ba32e..b35eba1 100644
--- a/components/keyboard_indicators.c
+++ b/components/keyboard_indicators.c
@@ -1,14 +1,26 @@
 /* See LICENSE file for copyright and license details. */
+#include 
 #include 
+#include 
 #include 
 
 #include "../util.h"
 
+/*
+ * fmt consists of uppercase or lowercase 'c' for caps lock and/or 'n' for num
+ * lock, each optionally followed by '?', in the order of indicators desired.
+ * If followed by '?', the letter with case preserved is included in the output
+ * if the corresponding indicator is on.  Otherwise, the letter is always
+ * included, lowercase when off and uppercase when on.
+ */
 const char *
-keyboard_indicators(void)
+keyboard_indicators(const char *fmt)
 {
Display *dpy;
XKeyboardState state;
+   size_t fmtlen, i, n;
+   int togglecase, isset;
+   char key;
 
if (!(dpy = XOpenDisplay(NULL))) {
warn("XOpenDisplay: Failed to open display");
@@ -17,14 +29,20 @@ keyboard_indicators(void)
XGetKeyboardControl(dpy, );
XCloseDisplay(dpy);
 
-   switch (state.led_mask) {
-   case 1:
-   return "c";
-   case 2:
-   return "n";
-   case 3:
-   return "cn";
-   default:
-   return "";
+   fmtlen = strnlen(fmt, 4);
+   for (i = n = 0; i < fmtlen; i++) {
+   key = tolower(fmt[i]);
+   if (key != 'c' && key != 'n') {
+   continue;
+   }
+   togglecase = (i + 1 >= fmtlen || fmt[i + 1] != '?');
+   isset = (state.led_mask & (1 << (key == 'n')));
+   if (togglecase) {
+   buf[n++] = isset ? toupper(key) : key;
+   } else if (isset) {
+   buf[n++] = fmt[i];
+   }
}
+   buf[n] = 0;
+   return buf;
 }
diff --git a/config.def.h b/config.def.h
index 58e935a..4474508 100644
--- a/config.def.h
+++ b/config.def.h
@@ -31,7 +31,8 @@ static const char unknown_str[] = "n/a";
  * ipv4 IPv4 addressinterface name (eth0)
  * ipv6 IPv6 addressinterface name (eth0)
  * kernel_release   `uname -r`  NULL
- * keyboard_indicators  caps/num lock indicatorsNULL
+ * keyboard_indicators  caps/num lock indicatorsformat string (c?n?)
+ *  see 
keyboard_indicators.c
  * keymap   layout (variant) of current NULL
  *  keymap
  * load_avg load averageNULL

-- 
- Ian.



Re: [hackers] [slstatus][PATCH] Add flexible formatting to keyboard_indicators.

2018-06-01 Thread Ian Remmler
On Fri, Jun 01, 2018 at 05:35:16PM +0200, Aaron Marcher wrote:
> You could also add something like "see keyboard_indicators.c for
> additional documentation".

That's reasonable.

> Additionally, for a minimum, add a comment for the argument and an example
> in config.def.h (like for the other components).

I had done that, but in config.h instead on accident.  I'll add it.

> These can be simplified by creating one-liners for vars of the same type.

That is a deadly sin in our coding style at work, so my brain probably
skipped over that bit. :)

> Additionally, according we decided to add braces for blocks always, also for
> single line statements.

Oh, good!  I prefer that.  I'll update the patch this weekend.

-- 
- Ian.



Re: [hackers] [slstatus][PATCH] Add flexible formatting to keyboard_indicators.

2018-06-01 Thread Aaron Marcher

Ian,

thank you very much for your work! I have a few things to comment before 
we can merge the patch:



+/*
for num
+ * lock, each optionally followed by '?', in the order of indicators desired.
+ * If followed by '?', the letter with case preserved is included in the output
+ * if the corresponding indicator is on.  Otherwise, the letter is always
+ * included, lowercase when off and uppercase when on.
+ */


I am not sure if keyboard_indicators.c is the correct place for the 
comment? Maybe config.def.h is better? However it is pretty long.

You could also add something like "see keyboard_indicators.c for
additional documentation".
Additionally, for a minimum, add a comment for the argument and an 
example in config.def.h (like for the other components).



+   size_t fmtlen;
+   size_t i;
+   size_t n = 0;
+   int togglecase;
+   int isset;
+   char key;


These can be simplified by creating one-liners for vars of the same 
type. Additionally according to the coding style vars should be only 
declared but not assigned at the beginning of a block.



+   if (key != 'c' && key != 'n')
+   continue;
+   togglecase = (i + 1 >= fmtlen || fmt[i + 1] != '?');
+   isset = (state.led_mask & (1 << (key == 'n')));
+   if (togglecase)
+   buf[n++] = isset ? toupper(key) : key;
+   else if (isset)
+   buf[n++] = fmt[i];


Additionally, according we decided to add braces for blocks always, also 
for single line statements.


Cheers!
Aaron

--
Web: https://drkhsh.at/ or http://drkhsh5rv6pnahas.onion/
Gopher: gopher://drkhsh.at or gopher://drkhsh5rv6pnahas.onion
GPG: 0x7A65E38D55BE96FE
Fingerprint: 4688 907C 8720 3318 0D9F AFDE 7A65 E38D 55BE 96FE



[hackers] [slstatus][PATCH] Add flexible formatting to keyboard_indicators.

2018-05-31 Thread Ian Remmler
Add output formatting for keyboard indicators, allowing the user to
specify order and whether to only show an indicator when it is on or
always show it and use case to indicate state.

---
 components/keyboard_indicators.c | 39 
 1 file changed, 29 insertions(+), 10 deletions(-)

diff --git a/components/keyboard_indicators.c b/components/keyboard_indicators.c
index 73ba32e..e6f1b98 100644
--- a/components/keyboard_indicators.c
+++ b/components/keyboard_indicators.c
@@ -1,14 +1,29 @@
 /* See LICENSE file for copyright and license details. */
+#include 
 #include 
+#include 
 #include 
 
 #include "../util.h"
 
+/*
+ * fmt consists of uppercase or lowercase 'c' for caps lock and/or 'n' for num
+ * lock, each optionally followed by '?', in the order of indicators desired.
+ * If followed by '?', the letter with case preserved is included in the output
+ * if the corresponding indicator is on.  Otherwise, the letter is always
+ * included, lowercase when off and uppercase when on.
+ */
 const char *
-keyboard_indicators(void)
+keyboard_indicators(const char *fmt)
 {
Display *dpy;
XKeyboardState state;
+   size_t fmtlen;
+   size_t i;
+   size_t n = 0;
+   int togglecase;
+   int isset;
+   char key;
 
if (!(dpy = XOpenDisplay(NULL))) {
warn("XOpenDisplay: Failed to open display");
@@ -17,14 +32,18 @@ keyboard_indicators(void)
XGetKeyboardControl(dpy, );
XCloseDisplay(dpy);
 
-   switch (state.led_mask) {
-   case 1:
-   return "c";
-   case 2:
-   return "n";
-   case 3:
-   return "cn";
-   default:
-   return "";
+   fmtlen = strnlen(fmt, 4);
+   for (i = 0; i < fmtlen; i++) {
+   key = tolower(fmt[i]);
+   if (key != 'c' && key != 'n')
+   continue;
+   togglecase = (i + 1 >= fmtlen || fmt[i + 1] != '?');
+   isset = (state.led_mask & (1 << (key == 'n')));
+   if (togglecase)
+   buf[n++] = isset ? toupper(key) : key;
+   else if (isset)
+   buf[n++] = fmt[i];
}
+   buf[n] = 0;
+   return buf;
 }

-- 
- Ian.