Skip to content

Commit

Permalink
feat: Add function to handle tab key press and insert previous result
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Veneckiy authored and Sergey Veneckiy committed Jun 11, 2024
1 parent 009e6ec commit 13892a6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.10)
project(uc VERSION 1.6.0)
project(uc VERSION 1.7.0)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED True)
Expand All @@ -18,7 +18,8 @@ add_executable(uc uc.c conv_str_to_math_res.c handle_arguments.c exponent_mode.c
target_link_libraries(uc m)
target_link_libraries(uc ${READLINE_LIBRARY})
target_include_directories(uc PRIVATE ${READLINE_INCLUDE_DIR})
#configure_file(${CMAKE_CURRENT_SOURCE_DIR}/handle_arguments.c ${CMAKE_CURRENT_SOURCE_DIR}/handle_arguments.c @ONLY)

# Add uc version from header of this file to function that print --version
target_compile_definitions(uc PUBLIC UC_VERSION="${PROJECT_VERSION}")


Expand Down
26 changes: 26 additions & 0 deletions uc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,24 @@
#include "conv_str_to_math_res.h"
#include "exponent_mode.h"

long double previous_result = 0.0;

/* Function to handle tab key press and insert previous result. */
int insert_previous_result(int count, int key)
{
if (key == '\t') { // Check if the pressed key is the tab key
char result_str[32]; // Buffer to store the result string
sprintf(result_str, "%Lf", previous_result); // Convert previous result to string and store in buffer
rl_insert_text(result_str); // Insert the result string at the current cursor position
rl_redisplay(); // Redisplay the input line with the inserted text
return 0; // Return 0 to indicate successful handling of the tab key
}
return rl_insert(count, key); // If not tab key, pass the arguments to the default rl_insert function
}

int main(int argc, char *argv[]) {
handle_arguments(argc, argv);
rl_bind_key('\t', insert_previous_result);

char *exp; // the math expression from the user.

Expand Down Expand Up @@ -39,13 +55,23 @@ int main(int argc, char *argv[]) {
printf("Result: %.0Lf\n", result);
break;
}
/*
default: {
long double result = conv_str_to_math_res(exp);
char *formatted_result = add_spaces_to_triples(result);
printf("Result: %s\n", formatted_result);
free(exp);
break;
}
*/
default: {
long double result = conv_str_to_math_res(exp);
previous_result = result; // Store the result for later use
char *formatted_result = add_spaces_to_triples(result);
printf("Result: %s\n", formatted_result);
free(exp);
break;
}
}
}

Expand Down

0 comments on commit 13892a6

Please sign in to comment.