svenstaro / rofi-calc

🖩 Do live calculations in rofi!
MIT License
964 stars 31 forks source link

Feature Request: Option to replace equation in input with result when added to history #97

Open Swivelgames opened 1 year ago

Swivelgames commented 1 year ago

Often times, I'd like to be able to do rapid-fire sequential equations in order get to the final result. It would be great if there was an option that could be passed that would replace the equation in the input with the result of the question that was just added to history. This would make it easy to do follow-up equations.

Example workflow:

2378.29 + 762.2 Enter 3140.49 / 2 Enter 1570.245 Ctrl+Enter (to copy)

This is especially useful when paired with -no-persist-history.

svenstaro commented 1 year ago

Sounds cool. How do you suggest that usage be triggered? We could try to use another modifier with Enter perhaps? Would you like to take a stab at implementing this?

Swivelgames commented 1 year ago

(rip... I totally wrote out a whole response and then got side-tracked and lost it :joy: )

I was imagining a cli arg like -reuse-result or something like that (I had a much better name for it before lol). I envisaged it augmenting Add to history to also replace the input if the option is passed.

But if you'd rather go with a modifier, Shift+Enter or Ctrl+Shift+Enter might be options. I think -kb-accept-alt and -kb-accept-custom-alt respectively. However, I'm not entirely sure if those are already covered by:

https://github.com/svenstaro/rofi-calc/blob/3be0c3a5c9490a078fc5b77332489f25e257ec08/src/calc.c#L442-L456

Or that maybe one of them trigger MENU_CUSTOM_ACTION.

I'll see what I can do, but the last time I wrote c on a regular basis was about 10 years ago, so I may need my hand slapped in the PR :joy:

Swivelgames commented 1 year ago

EDIT: There's always a way.

Getting a segfault right now at:

textbox_text(state->text, result);

But here's what I've got so far:

diff --git a/src/calc.c b/src/calc.c
index fa29309..07d469d 100644
--- a/src/calc.c
+++ b/src/calc.c
@@ -94,6 +95,14 @@ typedef struct
 #define AUTOMATIC_SAVE_TO_HISTORY "-automatic-save-to-history"
 #define HISTORY_LENGTH 100

+// Stolen from rofi/include
+typedef struct textbox textbox;
+typedef struct {
+  /** #textbox with the user input in the input bar. */
+  textbox *text;
+} RofiViewState;
+RofiViewState *rofi_view_get_active(void);
+void textbox_text(textbox *tb, const char *text);
+
 // Limit `str` to at most `limit` new lines.
 // Returns a new string of either the limited length or the length length.
 // However, in both cases, it's a new string.
@@ -427,6 +436,17 @@ static ModeMode calc_mode_result(Mode* sw, int menu_entry, G_GNUC_UNUSED char**
         retv = PREVIOUS_DIALOG;
     } else if (menu_entry & MENU_QUICK_SWITCH) {
         retv = (menu_entry & MENU_LOWER_MASK);
+    } else if (menu_entry & MENU_CUSTOM_ACTION) {
+        g_debug("MENU_CUSTOM_ACTION: %x", menu_entry & MENU_CUSTOM_ACTION);
+        RofiViewState *state = rofi_view_get_active();
+        if (state != NULL) {
+            char* result = g_strdup_printf("%s", pd->last_result);
+            g_debug("pd->last_result: %s", result);
+            g_debug("RofiViewState NOT NULL");
+            textbox_text(state->text, result);
+        }
+        append_last_result_to_history(pd);
+        retv = RELOAD_DIALOG;
     } else if ((menu_entry & MENU_OK) && (selected_line == 0 && find_arg(NO_HISTORY_OPTION) == -1)) {
         append_last_result_to_history(pd);
         retv = RELOAD_DIALOG;
Swivelgames commented 1 year ago

I tried changing my declarations to static, but still no dice. Any ideas?

static RofiViewState *rofi_view_get_active(void);
static void textbox_text(textbox *tb, const char *text);