Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commandline option to set initial input #55

Open
0ion9 opened this issue Sep 1, 2015 · 3 comments
Open

Commandline option to set initial input #55

0ion9 opened this issue Sep 1, 2015 · 3 comments

Comments

@0ion9
Copy link
Collaborator

0ion9 commented Sep 1, 2015

When lighthouse is triggered from another program (for example, sxiv), it would be useful to be able to provide an initial input string.

For example if the the user triggers TMSU tagging via pressing 'h', then lighthouse (the interactive part of this process) could ideally start with the character 'h' already input.

I suppose '-i' would be the logical name for this option.

I am looking at how to implement this but may not have time to do so in the next few days.

@0ion9
Copy link
Collaborator Author

0ion9 commented Sep 2, 2015

The following diff mostly implements this:
The missing functionality is that the results list is not updated until the user starts typing. I haven't figured out how to safely trigger that at startup yet.

diff --git a/README.md b/README.md
index 4ecd9b5..3203802 100644
--- a/README.md
+++ b/README.md
@@ -111,6 +111,8 @@ Options
 The `-c` command line flag will allow you to set a custom location for the configurations file.
 An example would be `lighthouse -c ~/lighthouserc2`.

+The `-i` command line flag allows you to set the initial value of the input line, rather than beginning with a completely empty line.
+
 Configuration file
 ---
 Check out the sample `lighthouserc` in `config/lighthouse`.  Copy it to your directory by
diff --git a/src/lighthouse.c b/src/lighthouse.c
index b0ef08e..c706a21 100644
--- a/src/lighthouse.c
+++ b/src/lighthouse.c
@@ -455,16 +455,20 @@ int main(int argc, char **argv) {
   /* Determine if there is an XDG_CONFIG_HOME to put the config in, otherwise use ~/.config */
   char *config_file_dir = (getenv("XDG_CONFIG_HOME")) ? getenv("XDG_CONFIG_HOME") : "~/.config";
   char *config_file = malloc(strlen(config_file_dir) + strlen(CONFIG_FILE) + 1);
+  char *initial_input = NULL;
   if (!config_file) {
     return 1;
   }
   sprintf(config_file, "%s%s", config_file_dir, CONFIG_FILE);
   int c;
-  while ((c = getopt(argc, argv, "c:")) != -1) {
+  while ((c = getopt(argc, argv, "c:i:")) != -1) {
     switch (c) {
       case 'c':
         config_file = optarg;
         break;
+      case 'i':
+        initial_input = optarg;
+        break;
       default:
         break;
     }
@@ -677,10 +681,15 @@ int main(int argc, char **argv) {

   /* Query string. */
   char query_string[MAX_QUERY];
-  memset(query_string, 0, sizeof(query_string));
   uint32_t query_index = 0;
   uint32_t query_cursor_index = 0;

+  memset(query_string, 0, sizeof(query_string));
+  if (initial_input) {
+    strncpy(query_string, initial_input, MAX_QUERY - 1);
+    query_cursor_index = strlen(query_string);
+    query_index = query_cursor_index;
+  }
   /* Now draw everything. */
   redraw_all(connection, window, cairo_context, cairo_surface, query_string, query_cursor_index);

@emgram769
Copy link
Owner

don't subtract 1 from MAX_QUERY, the null terminator isn't implicitly copied with strncpy

@0ion9
Copy link
Collaborator Author

0ion9 commented Sep 2, 2015

Okay, thanks, I missed that subtlety.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants