Skip to content

Commit 1aee4d8

Browse files
committed
finish /copy all functions
1 parent 3cfd466 commit 1aee4d8

File tree

6 files changed

+133
-13
lines changed

6 files changed

+133
-13
lines changed

.github/workflows/cmake.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ jobs:
2525
- name: Install libcurl
2626
run: sudo apt install libcurl4-openssl-dev -y
2727

28-
# - name: Install libncursesw
29-
# run: sudo apt install libncursesw5-dev -y
30-
3128
- name: Install libargtable
3229
run: sudo apt install libargtable2-dev -y
3330

@@ -37,18 +34,21 @@ jobs:
3734
- name: Install GNU libreadline
3835
run: sudo apt install libreadline-dev -y
3936

37+
- name: Install libx11
38+
run: sudo apt install libx11-dev -y
39+
4040
- name: Configure CMake
4141
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
4242
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
43-
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
43+
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCLIP_EXAMPLES=OFF -DCLIP_TESTS=OFF
4444

4545
- name: Build
4646
# Build your program with the given configuration
4747
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
4848

49-
- name: Test
50-
working-directory: ${{github.workspace}}/build
51-
# Execute tests defined by the CMake configuration.
52-
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
53-
run: ctest -C ${{env.BUILD_TYPE}}
49+
# - name: Test
50+
# working-directory: ${{github.workspace}}/build
51+
# # Execute tests defined by the CMake configuration.
52+
# # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
53+
# run: ctest -C ${{env.BUILD_TYPE}}
5454

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
cmake_minimum_required(VERSION 3.10)
22
project(cGPTerm)
33

4+
add_compile_options(-fPIC)
45
set(CMAKE_C_FLAGS "-Wall -g")
56
add_definitions(-D_DEFAULT_SOURCE)
67
add_definitions(-D_GNU_SOURCE)

include/cli/slashcmd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern "C" {
2323
#include"utils.h"
2424
#include"crich.h"
2525
#include"cclip.hpp"
26+
#include"cvector.h"
2627

2728
extern char* chat_history_save_file_generated_path;
2829

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ add_subdirectory(argparse)
99
add_subdirectory(cli)
1010
# build cpp cli
1111

12-
add_library(cclip STATIC
12+
add_library(cclip SHARED
1313
${CMAKE_CURRENT_SOURCE_DIR}/cclip.cpp
1414
)
1515
target_link_libraries(cclip

src/cli/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ target_link_libraries(cli
99
openaiapi
1010
ezylog
1111
cclip
12+
cvector
1213
utils
1314
pthread
1415
readline

src/cli/slashcmd.c

Lines changed: 120 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ void enable_history_search( void ){
1212

1313
char* chat_history_save_file_generated_path = NULL;
1414

15+
void search_codes( c_vector* __codelist , const char* __text ){
16+
size_t index = 0;
17+
char* start;
18+
char* end;
19+
start = strstr( __text , "```" );
20+
while ( start != NULL )
21+
{
22+
end = strstr( start + 3 , "```" );
23+
if ( end == NULL )
24+
break;
25+
char* this_code = ( char* ) malloc( ( end - start + 8 ) * sizeof( char ) );
26+
strncpy( this_code , start , end - start + 3 );
27+
this_code[end-start+3] = '\0';
28+
cv_push_back( __codelist , ( void* ) this_code );
29+
start = strstr( end + 3 , "```" );
30+
}
31+
return;
32+
}
33+
1534
/**
1635
* @brief this startup hook will automaticlly generate a file name for chat history save file
1736
*/
@@ -469,9 +488,102 @@ int handle_slash_command( const char* __slashcmd ){
469488
crprint( "[dim]Nothing to copy\n" );
470489
return 0;
471490
} // /copy command should not copy system prompt
472-
clipboard_copy( last_response );
491+
492+
if ( strlen( __slashcmd ) == 5 )
493+
{
494+
clipboard_copy( last_response );
495+
crprint( "[dim]Last reply copied to Clipboard.\n");
496+
return 0;
497+
} // no other args given
498+
499+
char* temp = ( char* ) malloc( strlen( __slashcmd ) + 1 );
500+
char* second_arg;
501+
strcpy( temp , __slashcmd );
502+
second_arg = strtok( temp , " " );
503+
second_arg = strtok( NULL , " " );
504+
505+
if ( strcmp( second_arg , "all" ) == 0 )
506+
{
507+
clipboard_copy( last_response );
508+
crprint( "[dim]Last reply copied to Clipboard.\n");
509+
free( temp );
510+
} // /copy all
511+
else if ( strcmp( second_arg , "code" ) == 0 )
512+
{
513+
c_vector codelist;
514+
cv_init( &codelist , 10 );
515+
char* selected_code = NULL;
516+
search_codes( &codelist , last_response );
517+
if ( cv_len( &codelist ) == 0 )
518+
{
519+
crprint( "[dim]No code found.\n" );
520+
cv_clean( &codelist );
521+
free( temp );
522+
return 0;
523+
} // no code found
524+
if ( cv_len( &codelist ) == 1 )
525+
{
526+
selected_code = ( char* ) codelist.items[0];
527+
goto copy_code;
528+
} // only one code block exists
529+
530+
// more than one code block exist
531+
532+
crprint( "[dim]There are more than one code in ChatGPT's last reply.\n" );
533+
for ( int i = 0 ; i < cv_len( &codelist ) ; i++ )
534+
{
535+
crprint( "[yellow]Code [bright yellow]%d[/]:\n" , i + 1 );
536+
md_set( ( char* ) codelist.items[i] );
537+
md_parse();
538+
md_print();
539+
printf( "\n" );
540+
}
541+
542+
ask_code_index:
543+
disable_history_search();
544+
char* code_index_str = readline( "Please select which code to copy: " );
545+
enable_history_search();
546+
int code_index = atoi( code_index_str );
547+
if ( code_index > 0 && code_index <= cv_len( &codelist ) )
548+
{
549+
selected_code = ( char* ) codelist.items[code_index-1];
550+
goto copy_code;
551+
} // legal index input
552+
553+
printf( "\r\033[2K\r" );
554+
if ( code_index <= 0 )
555+
crprint( "[red]Code index must be an integer greater than 0\n" );
556+
if ( code_index > cv_len( &codelist ) )
557+
crprint( "[red]Code index out of range: You should input an Integer in range [bright red]1[/] ~ [bright red]%d[/]\n" , cv_len( &codelist ) );
558+
// illegal index input
559+
printf( "\033[2A\r\033[2K\r" );
560+
fflush( stdout );
561+
goto ask_code_index;
562+
563+
copy_code:
564+
printf( "\r\033[2K\r" );
565+
char* code_raw = ( char* ) malloc( strlen( selected_code ) );
566+
char* bpos = strstr( selected_code , "\n" );
567+
bpos++;
568+
char* epos = strstr( bpos , "```" );
569+
// build code begin and end index
570+
strncpy( code_raw , bpos , epos - bpos );
571+
code_raw[epos-bpos] = '\0';
572+
// copy code raw
573+
clipboard_copy( code_raw );
574+
crprint( "[dim]Code copied to Clipboard.\n" );
575+
576+
cv_clean( &codelist );
577+
free( temp );
578+
free( code_index_str );
579+
free( code_raw );
580+
} // /copy code
581+
else
582+
crprint( "[dim]Nothing to do. Available copy command: `[bright magenta]/copy code[/]` or `[bright magenta]/copy all[/]`" );
583+
// /copy ?
584+
473585
return 0;
474-
}
586+
} // /copy
475587

476588
// ====================================================================================
477589
// ===================================== /version =====================================
@@ -514,9 +626,14 @@ void print_slash_command_help(){
514626
crprint( " [bright magenta]/raw[/]\t\t\t- Toggle raw mode (showing raw text of ChatGPT's reply)\n" );
515627
crprint( " [bright magenta]/tokens[/]\t\t\t- Show the total tokens spent and the tokens for the current conversation\n" );
516628
crprint( " [bright magenta]/usage[/]\t\t\t- Show total credits and current credits used\n" );
517-
crprint( " [bright magenta]/save[/] [bold]\\[filename_or_path][/]\t- Save the chat history to a file, suggest title if filename_or_path not provided\n" );
518629
crprint( " [bright magenta]/timeout[/] [bold]\\[new_timeout][/]\t- Modify the api timeout\n" );
519630
crprint( " [bright magenta]/model[/] [bold]\\[model_name][/]\t\t- Change AI model\n" );
631+
crprint( " [bright magenta]/rand[/] [bold]\\[randomness][/]\t\t- Set Model sampling temperature (0~2)\n" );
632+
crprint( " [bright magenta]/save[/] [bold]\\[filename_or_path][/]\t- Save the chat history to a file, suggest title if filename_or_path not provided\n" );
633+
crprint( " [bright magenta]/undo[/]\t\t\t- Undo the last question and remove its answer\n" );
634+
crprint( " [bright magenta]/last[/]\t\t\t- Display last ChatGPT's reply\n" );
635+
crprint( " [bright magenta]/copy[/] [bold](all)[/]\t\t\t- Copy the full ChatGPT's last reply (raw) to Clipboard\n" );
636+
crprint( " [bright magenta]/copy[/] [bold]code[/]\t\t\t- Copy the code in ChatGPT's last reply to Clipboard\n" );
520637
crprint( " [bright magenta]/version[/]\t\t\t- Show cGPTerm local and remote version\n" );
521638
crprint( " [bright magenta]/help[/]\t\t\t- Show this help message\n" );
522639
crprint( " [bright magenta]/exit[/]\t\t\t- Exit the application\n" );

0 commit comments

Comments
 (0)