donniebreve / touchcursor-linux

TouchCursor style keyboard remapping for Linux.
GNU General Public License v2.0
133 stars 28 forks source link

Trim comments from the end of configuration lines #29

Closed Adda0 closed 3 years ago

Adda0 commented 3 years ago

Reason for this feature

I have multiple devices using the same customized config file, so I need to set various keyboard names at once to let TouchCursor choose the currently plugged keyboard (which is supposed to work according to #12 issue). Because of that, I have separate Name= lines in touchcursor.conf config file and to remember which keyboard belongs to which device, I have added short comments at the end of their respective lines.

Name="BTC USB Multimedia Keyboard" # Main desktop keyboard.
Name="AT Translated Set 2 keyboard" # Notebook keyboard.

I have been surprised when TouchCursor stopped working after I added multiple Name= lines. By looking at the code, I realized the problem does not lie with the duplicated Name= lines, but with parsing of the comments at the end of any configuration lines (non-empty lines which are not just comment lines).

I believe the option to add comments at the end of configuration lines should be present, as it provides a useful way to better describe parts of custom TouchCursor configurations and explain specific configuration lines without the need to create a new line for every single comment.

Solution

Look for one-line comments started by # at the end of any line when parsing the configuration lines, and trim such comments.

Example

This PR allows correct parsing of configuration lines with comments at the end and keeps TouchCursor running without errors due to comments on the configuration lines. Comments can be appended at the end of any configuration line started by #, following at least a single whitespace after the corresponding configuration:

[^#]*\s+#.*

For example, the following will be parsed correctly.

[Device]
Name="BTC USB Multimedia Keyboard" # Main desktop keyboard.
Name="AT Translated Set 2 keyboard" # Notebook keyboard.

[Bindings] # This is the Bindings section, where you can remap keys to different one.
KEY_H=KEY_LEFT # You can use Vim-like keybindings for movement.
KEY_J=KEY_DOWN
KEY_K=KEY_UP
KEY_L=KEY_RIGHT

Testing

I am using my version of TouchCursor with this PR included and everything has worked flawlessly so far.

donniebreve commented 3 years ago

Looks good to me 👍