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

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

Open
Swivelgames opened this issue Apr 27, 2023 · 4 comments

Comments

@Swivelgames
Copy link

Swivelgames commented Apr 27, 2023

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
Copy link
Owner

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
Copy link
Author

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

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:

rofi-calc/src/calc.c

Lines 442 to 456 in 3be0c3a

} else if (menu_entry & MENU_CUSTOM_INPUT) {
if (!is_error_string(pd->last_result) && strlen(pd->last_result) > 0) {
if (find_arg(NO_HISTORY_OPTION) == -1 && find_arg(CALC_COMMAND_USES_HISTORY) != -1) {
char* history_entry = g_strdup_printf("%s", pd->last_result);
g_ptr_array_add(pd->history, (gpointer) history_entry);
if (find_arg(NO_PERSIST_HISTORY_OPTION) == -1) {
append_str_to_history(history_entry);
}
}
execsh(pd->cmd, pd->last_result);
retv = MODE_EXIT;
} else {
retv = RELOAD_DIALOG;
}

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 😂

@Swivelgames
Copy link
Author

Swivelgames commented Apr 28, 2023

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
Copy link
Author

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);

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

No branches or pull requests

2 participants