replxx.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /*
  2. * Copyright (c) 2017-2018, Marcin Konarski (amok at codestation.org)
  3. * Copyright (c) 2010, Salvatore Sanfilippo <antirez at gmail dot com>
  4. * Copyright (c) 2010, Pieter Noordhuis <pcnoordhuis at gmail dot com>
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * * Neither the name of Redis nor the names of its contributors may be used
  17. * to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * line editing lib needs to be 20,000 lines of C code.
  33. *
  34. * You can find the latest source code at:
  35. *
  36. * http://github.com/antirez/linenoise
  37. *
  38. * Does a number of crazy assumptions that happen to be true in 99.9999% of
  39. * the 2010 UNIX computers around.
  40. *
  41. * References:
  42. * - http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
  43. * - http://www.3waylabs.com/nw/WWW/products/wizcon/vt220.html
  44. *
  45. * Todo list:
  46. * - Switch to gets() if $TERM is something we can't support.
  47. * - Filter bogus Ctrl+<char> combinations.
  48. * - Win32 support
  49. *
  50. * Bloat:
  51. * - Completion?
  52. * - History search like Ctrl+r in readline?
  53. *
  54. * List of escape sequences used by this program, we do everything just
  55. * with three sequences. In order to be so cheap we may have some
  56. * flickering effect with some slow terminal, but the lesser sequences
  57. * the more compatible.
  58. *
  59. * CHA (Cursor Horizontal Absolute)
  60. * Sequence: ESC [ n G
  61. * Effect: moves cursor to column n (1 based)
  62. *
  63. * EL (Erase Line)
  64. * Sequence: ESC [ n K
  65. * Effect: if n is 0 or missing, clear from cursor to end of line
  66. * Effect: if n is 1, clear from beginning of line to cursor
  67. * Effect: if n is 2, clear entire line
  68. *
  69. * CUF (Cursor Forward)
  70. * Sequence: ESC [ n C
  71. * Effect: moves cursor forward of n chars
  72. *
  73. * The following are used to clear the screen: ESC [ H ESC [ 2 J
  74. * This is actually composed of two sequences:
  75. *
  76. * cursorhome
  77. * Sequence: ESC [ H
  78. * Effect: moves the cursor to upper left corner
  79. *
  80. * ED2 (Clear entire screen)
  81. * Sequence: ESC [ 2 J
  82. * Effect: clear the whole screen
  83. *
  84. */
  85. #include <algorithm>
  86. #include <cstdarg>
  87. #ifdef _WIN32
  88. #include <io.h>
  89. #define STDIN_FILENO 0
  90. #else /* _WIN32 */
  91. #include <signal.h>
  92. #include <unistd.h>
  93. #include <sys/stat.h>
  94. #endif /* _WIN32 */
  95. #include "replxx.h"
  96. #include "replxx.hxx"
  97. #include "replxx_impl.hxx"
  98. #include "history.hxx"
  99. static_assert(
  100. static_cast<int>( replxx::Replxx::ACTION::SEND_EOF ) == static_cast<int>( REPLXX_ACTION_SEND_EOF ),
  101. "C and C++ `ACTION` APIs are missaligned!"
  102. );
  103. static_assert(
  104. static_cast<int>( replxx::Replxx::KEY::PASTE_FINISH ) == static_cast<int>( REPLXX_KEY_PASTE_FINISH ),
  105. "C and C++ `KEY` APIs are missaligned!"
  106. );
  107. using namespace std;
  108. using namespace std::placeholders;
  109. using namespace replxx;
  110. namespace replxx {
  111. namespace {
  112. void delete_ReplxxImpl( Replxx::ReplxxImpl* impl_ ) {
  113. delete impl_;
  114. }
  115. }
  116. Replxx::Replxx( void )
  117. : _impl( new Replxx::ReplxxImpl( nullptr, nullptr, nullptr ), delete_ReplxxImpl ) {
  118. }
  119. void Replxx::set_completion_callback( completion_callback_t const& fn ) {
  120. _impl->set_completion_callback( fn );
  121. }
  122. void Replxx::set_modify_callback( modify_callback_t const& fn ) {
  123. _impl->set_modify_callback( fn );
  124. }
  125. void Replxx::set_highlighter_callback( highlighter_callback_t const& fn ) {
  126. _impl->set_highlighter_callback( fn );
  127. }
  128. void Replxx::set_hint_callback( hint_callback_t const& fn ) {
  129. _impl->set_hint_callback( fn );
  130. }
  131. char const* Replxx::input( std::string const& prompt ) {
  132. return ( _impl->input( prompt ) );
  133. }
  134. void Replxx::history_add( std::string const& line ) {
  135. _impl->history_add( line );
  136. }
  137. bool Replxx::history_sync( std::string const& filename ) {
  138. return ( _impl->history_sync( filename ) );
  139. }
  140. bool Replxx::history_save( std::string const& filename ) {
  141. return ( _impl->history_save( filename ) );
  142. }
  143. void Replxx::history_save( std::ostream& out ) {
  144. _impl->history_save( out );
  145. }
  146. bool Replxx::history_load( std::string const& filename ) {
  147. return ( _impl->history_load( filename ) );
  148. }
  149. void Replxx::history_load( std::istream& in ) {
  150. _impl->history_load( in );
  151. }
  152. void Replxx::history_clear( void ) {
  153. _impl->history_clear();
  154. }
  155. int Replxx::history_size( void ) const {
  156. return ( _impl->history_size() );
  157. }
  158. Replxx::HistoryScan Replxx::history_scan( void ) const {
  159. return ( _impl->history_scan() );
  160. }
  161. void Replxx::set_preload_buffer( std::string const& preloadText ) {
  162. _impl->set_preload_buffer( preloadText );
  163. }
  164. void Replxx::set_word_break_characters( char const* wordBreakers ) {
  165. _impl->set_word_break_characters( wordBreakers );
  166. }
  167. void Replxx::set_max_hint_rows( int count ) {
  168. _impl->set_max_hint_rows( count );
  169. }
  170. void Replxx::set_hint_delay( int milliseconds ) {
  171. _impl->set_hint_delay( milliseconds );
  172. }
  173. void Replxx::set_completion_count_cutoff( int count ) {
  174. _impl->set_completion_count_cutoff( count );
  175. }
  176. void Replxx::set_double_tab_completion( bool val ) {
  177. _impl->set_double_tab_completion( val );
  178. }
  179. void Replxx::set_complete_on_empty( bool val ) {
  180. _impl->set_complete_on_empty( val );
  181. }
  182. void Replxx::set_beep_on_ambiguous_completion( bool val ) {
  183. _impl->set_beep_on_ambiguous_completion( val );
  184. }
  185. void Replxx::set_immediate_completion( bool val ) {
  186. _impl->set_immediate_completion( val );
  187. }
  188. void Replxx::set_unique_history( bool val ) {
  189. _impl->set_unique_history( val );
  190. }
  191. void Replxx::set_no_color( bool val ) {
  192. _impl->set_no_color( val );
  193. }
  194. void Replxx::set_indent_multiline( bool val ) {
  195. _impl->set_indent_multiline( val );
  196. }
  197. void Replxx::set_max_history_size( int len ) {
  198. _impl->set_max_history_size( len );
  199. }
  200. void Replxx::clear_screen( void ) {
  201. _impl->clear_screen( 0 );
  202. }
  203. void Replxx::emulate_key_press( char32_t keyPress_ ) {
  204. _impl->emulate_key_press( keyPress_ );
  205. }
  206. Replxx::ACTION_RESULT Replxx::invoke( ACTION action_, char32_t keyPress_ ) {
  207. return ( _impl->invoke( action_, keyPress_ ) );
  208. }
  209. void Replxx::bind_key( char32_t keyPress_, key_press_handler_t handler_ ) {
  210. _impl->bind_key( keyPress_, handler_ );
  211. }
  212. void Replxx::bind_key_internal( char32_t keyPress_, char const* actionName_ ) {
  213. _impl->bind_key_internal( keyPress_, actionName_ );
  214. }
  215. Replxx::State Replxx::get_state( void ) const {
  216. return ( _impl->get_state() );
  217. }
  218. void Replxx::set_state( Replxx::State const& state_ ) {
  219. _impl->set_state( state_ );
  220. }
  221. void Replxx::set_ignore_case( bool val ) {
  222. _impl->set_ignore_case( val );
  223. }
  224. int Replxx::install_window_change_handler( void ) {
  225. return ( _impl->install_window_change_handler() );
  226. }
  227. void Replxx::enable_bracketed_paste( void ) {
  228. _impl->enable_bracketed_paste();
  229. }
  230. void Replxx::disable_bracketed_paste( void ) {
  231. _impl->disable_bracketed_paste();
  232. }
  233. void Replxx::print( char const* format_, ... ) {
  234. ::std::va_list ap;
  235. va_start( ap, format_ );
  236. int size = static_cast<int>( vsnprintf( nullptr, 0, format_, ap ) );
  237. va_end( ap );
  238. va_start( ap, format_ );
  239. unique_ptr<char[]> buf( new char[size + 1] );
  240. vsnprintf( buf.get(), static_cast<size_t>( size + 1 ), format_, ap );
  241. va_end( ap );
  242. return ( _impl->print( buf.get(), size ) );
  243. }
  244. void Replxx::write( char const* str, int length ) {
  245. return ( _impl->print( str, length ) );
  246. }
  247. void Replxx::set_prompt( std::string prompt ) {
  248. return ( _impl->set_prompt( std::move( prompt ) ) );
  249. }
  250. namespace color {
  251. Replxx::Color operator | ( Replxx::Color color1_, Replxx::Color color2_ ) {
  252. return static_cast<Replxx::Color>( static_cast<int unsigned>( color1_ ) | static_cast<int unsigned>( color2_ ) );
  253. }
  254. Replxx::Color bg( Replxx::Color color_ ) {
  255. return static_cast<Replxx::Color>( ( ( static_cast<int unsigned>( color_ ) & 0xFFu ) << 8 ) | color::BACKGROUND_COLOR_SET );
  256. }
  257. Replxx::Color bold( Replxx::Color color_ ) {
  258. return static_cast<Replxx::Color>( static_cast<int unsigned>( color_ ) | color::BOLD );
  259. }
  260. Replxx::Color underline( Replxx::Color color_ ) {
  261. return static_cast<Replxx::Color>( static_cast<int unsigned>( color_ ) | color::UNDERLINE );
  262. }
  263. Replxx::Color grayscale( int level_ ) {
  264. assert( ( level_ >= 0 ) && ( level_ < 24 ) );
  265. return static_cast<Replxx::Color>( abs( level_ ) % 24 + static_cast<int unsigned>( color::GRAYSCALE ) );
  266. }
  267. Replxx::Color rgb666( int red_, int green_, int blue_ ) {
  268. assert( ( red_ >= 0 ) && ( red_ < 6 ) && ( green_ >= 0 ) && ( green_ < 6 ) && ( blue_ >= 0 ) && ( blue_ < 6 ) );
  269. return static_cast<Replxx::Color>(
  270. ( abs( red_ ) % 6 ) * 36
  271. + ( abs( green_ ) % 6 ) * 6
  272. + ( abs( blue_ ) % 6 )
  273. + static_cast<int unsigned>( color::RGB666 )
  274. );
  275. }
  276. }
  277. }
  278. ::Replxx* replxx_init() {
  279. typedef ::Replxx* replxx_data_t;
  280. return ( reinterpret_cast<replxx_data_t>( new replxx::Replxx::ReplxxImpl( nullptr, nullptr, nullptr ) ) );
  281. }
  282. void replxx_end( ::Replxx* replxx_ ) {
  283. delete reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ );
  284. }
  285. void replxx_clear_screen( ::Replxx* replxx_ ) {
  286. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  287. replxx->clear_screen( 0 );
  288. }
  289. void replxx_emulate_key_press( ::Replxx* replxx_, int unsigned keyPress_ ) {
  290. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  291. replxx->emulate_key_press( keyPress_ );
  292. }
  293. ReplxxActionResult replxx_invoke( ::Replxx* replxx_, ReplxxAction action_, int unsigned keyPress_ ) {
  294. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  295. return ( static_cast<ReplxxActionResult>( replxx->invoke( static_cast<replxx::Replxx::ACTION>( action_ ), keyPress_ ) ) );
  296. }
  297. replxx::Replxx::ACTION_RESULT key_press_handler_forwarder( key_press_handler_t handler_, char32_t code_, void* userData_ ) {
  298. return ( static_cast<replxx::Replxx::ACTION_RESULT>( handler_( code_, userData_ ) ) );
  299. }
  300. void replxx_bind_key( ::Replxx* replxx_, int code_, key_press_handler_t handler_, void* userData_ ) {
  301. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  302. replxx->bind_key( code_, std::bind( key_press_handler_forwarder, handler_, _1, userData_ ) );
  303. }
  304. int replxx_bind_key_internal( ::Replxx* replxx_, int code_, char const* actionName_ ) {
  305. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  306. try {
  307. replxx->bind_key_internal( code_, actionName_ );
  308. } catch ( ... ) {
  309. return ( -1 );
  310. }
  311. return ( 0 );
  312. }
  313. void replxx_get_state( ::Replxx* replxx_, ReplxxState* state ) {
  314. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  315. replxx::Replxx::State s( replxx->get_state() );
  316. state->text = s.text();
  317. state->cursorPosition = s.cursor_position();
  318. }
  319. void replxx_set_state( ::Replxx* replxx_, ReplxxState* state ) {
  320. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  321. replxx->set_state( replxx::Replxx::State( state->text, state->cursorPosition ) );
  322. }
  323. void replxx_set_ignore_case( ::Replxx* replxx_, int val ) {
  324. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  325. replxx->set_ignore_case( val );
  326. }
  327. /**
  328. * replxx_set_preload_buffer provides text to be inserted into the command buffer
  329. *
  330. * the provided text will be processed to be usable and will be used to preload
  331. * the input buffer on the next call to replxx_input()
  332. *
  333. * @param preloadText text to begin with on the next call to replxx_input()
  334. */
  335. void replxx_set_preload_buffer(::Replxx* replxx_, const char* preloadText) {
  336. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  337. replxx->set_preload_buffer( preloadText ? preloadText : "" );
  338. }
  339. /**
  340. * replxx_input is a readline replacement.
  341. *
  342. * call it with a prompt to display and it will return a line of input from the
  343. * user
  344. *
  345. * @param prompt text of prompt to display to the user
  346. * @return the returned string is managed by replxx library
  347. * and it must NOT be freed in the client.
  348. */
  349. char const* replxx_input( ::Replxx* replxx_, const char* prompt ) {
  350. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  351. return ( replxx->input( prompt ) );
  352. }
  353. int replxx_print( ::Replxx* replxx_, char const* format_, ... ) {
  354. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  355. ::std::va_list ap;
  356. va_start( ap, format_ );
  357. int size = static_cast<int>( vsnprintf( nullptr, 0, format_, ap ) );
  358. va_end( ap );
  359. va_start( ap, format_ );
  360. unique_ptr<char[]> buf( new char[size + 1] );
  361. vsnprintf( buf.get(), static_cast<size_t>( size + 1 ), format_, ap );
  362. va_end( ap );
  363. try {
  364. replxx->print( buf.get(), size );
  365. } catch ( ... ) {
  366. return ( -1 );
  367. }
  368. return ( size );
  369. }
  370. int replxx_write( ::Replxx* replxx_, char const* str, int length ) {
  371. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  372. try {
  373. replxx->print( str, length );
  374. } catch ( ... ) {
  375. return ( -1 );
  376. }
  377. return static_cast<int>( length );
  378. }
  379. void replxx_set_prompt( ::Replxx* replxx_, const char* prompt ) {
  380. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  381. replxx->set_prompt( prompt );
  382. }
  383. struct replxx_completions {
  384. replxx::Replxx::completions_t data;
  385. };
  386. struct replxx_hints {
  387. replxx::Replxx::hints_t data;
  388. };
  389. void modify_fwd( replxx_modify_callback_t fn, std::string& line_, int& cursorPosition_, void* userData_ ) {
  390. #ifdef _WIN32
  391. #define strdup _strdup
  392. #endif
  393. char* s( strdup( line_.c_str() ) );
  394. #undef strdup
  395. fn( &s, &cursorPosition_, userData_ );
  396. line_ = s;
  397. free( s );
  398. return;
  399. }
  400. void replxx_set_modify_callback(::Replxx* replxx_, replxx_modify_callback_t* fn, void* userData) {
  401. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  402. replxx->set_modify_callback( std::bind( &modify_fwd, fn, _1, _2, userData ) );
  403. }
  404. replxx::Replxx::completions_t completions_fwd( replxx_completion_callback_t fn, std::string const& input_, int& contextLen_, void* userData ) {
  405. replxx_completions completions;
  406. fn( input_.c_str(), &completions, &contextLen_, userData );
  407. return ( completions.data );
  408. }
  409. /* Register a callback function to be called for tab-completion. */
  410. void replxx_set_completion_callback(::Replxx* replxx_, replxx_completion_callback_t* fn, void* userData) {
  411. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  412. replxx->set_completion_callback( std::bind( &completions_fwd, fn, _1, _2, userData ) );
  413. }
  414. void highlighter_fwd( replxx_highlighter_callback_t fn, std::string const& input, replxx::Replxx::colors_t& colors, void* userData ) {
  415. std::vector<ReplxxColor> colorsTmp( colors.size() );
  416. std::transform(
  417. colors.begin(),
  418. colors.end(),
  419. colorsTmp.begin(),
  420. []( replxx::Replxx::Color c ) {
  421. return ( static_cast<ReplxxColor>( c ) );
  422. }
  423. );
  424. fn( input.c_str(), colorsTmp.data(), static_cast<int>( colors.size() ), userData );
  425. std::transform(
  426. colorsTmp.begin(),
  427. colorsTmp.end(),
  428. colors.begin(),
  429. []( ReplxxColor c ) {
  430. return ( static_cast<replxx::Replxx::Color>( c ) );
  431. }
  432. );
  433. }
  434. void replxx_set_highlighter_callback( ::Replxx* replxx_, replxx_highlighter_callback_t* fn, void* userData ) {
  435. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  436. replxx->set_highlighter_callback( std::bind( &highlighter_fwd, fn, _1, _2, userData ) );
  437. }
  438. replxx::Replxx::hints_t hints_fwd( replxx_hint_callback_t fn, std::string const& input_, int& contextLen_, replxx::Replxx::Color& color_, void* userData ) {
  439. replxx_hints hints;
  440. ReplxxColor c( static_cast<ReplxxColor>( color_ ) );
  441. fn( input_.c_str(), &hints, &contextLen_, &c, userData );
  442. return ( hints.data );
  443. }
  444. void replxx_set_hint_callback( ::Replxx* replxx_, replxx_hint_callback_t* fn, void* userData ) {
  445. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  446. replxx->set_hint_callback( std::bind( &hints_fwd, fn, _1, _2, _3, userData ) );
  447. }
  448. void replxx_add_hint(replxx_hints* lh, const char* str) {
  449. lh->data.emplace_back(str);
  450. }
  451. void replxx_add_completion( replxx_completions* lc, const char* str ) {
  452. lc->data.emplace_back( str );
  453. }
  454. void replxx_add_color_completion( replxx_completions* lc, const char* str, ReplxxColor color ) {
  455. lc->data.emplace_back( str, static_cast<replxx::Replxx::Color>( color ) );
  456. }
  457. void replxx_history_add( ::Replxx* replxx_, const char* line ) {
  458. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  459. replxx->history_add( line );
  460. }
  461. void replxx_set_max_history_size( ::Replxx* replxx_, int len ) {
  462. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  463. replxx->set_max_history_size( len );
  464. }
  465. void replxx_set_max_hint_rows( ::Replxx* replxx_, int count ) {
  466. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  467. replxx->set_max_hint_rows( count );
  468. }
  469. void replxx_set_hint_delay( ::Replxx* replxx_, int milliseconds ) {
  470. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  471. replxx->set_hint_delay( milliseconds );
  472. }
  473. void replxx_set_completion_count_cutoff( ::Replxx* replxx_, int count ) {
  474. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  475. replxx->set_completion_count_cutoff( count );
  476. }
  477. void replxx_set_word_break_characters( ::Replxx* replxx_, char const* breakChars_ ) {
  478. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  479. replxx->set_word_break_characters( breakChars_ );
  480. }
  481. void replxx_set_double_tab_completion( ::Replxx* replxx_, int val ) {
  482. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  483. replxx->set_double_tab_completion( val ? true : false );
  484. }
  485. void replxx_set_complete_on_empty( ::Replxx* replxx_, int val ) {
  486. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  487. replxx->set_complete_on_empty( val ? true : false );
  488. }
  489. void replxx_set_no_color( ::Replxx* replxx_, int val ) {
  490. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  491. replxx->set_no_color( val ? true : false );
  492. }
  493. void replxx_set_indent_multiline( ::Replxx* replxx_, int val ) {
  494. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  495. replxx->set_indent_multiline( val ? true : false );
  496. }
  497. void replxx_set_beep_on_ambiguous_completion( ::Replxx* replxx_, int val ) {
  498. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  499. replxx->set_beep_on_ambiguous_completion( val ? true : false );
  500. }
  501. void replxx_set_immediate_completion( ::Replxx* replxx_, int val ) {
  502. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  503. replxx->set_immediate_completion( val ? true : false );
  504. }
  505. void replxx_set_unique_history( ::Replxx* replxx_, int val ) {
  506. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  507. replxx->set_unique_history( val ? true : false );
  508. }
  509. void replxx_enable_bracketed_paste( ::Replxx* replxx_ ) {
  510. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  511. replxx->enable_bracketed_paste();
  512. }
  513. void replxx_disable_bracketed_paste( ::Replxx* replxx_ ) {
  514. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  515. replxx->disable_bracketed_paste();
  516. }
  517. ReplxxHistoryScan* replxx_history_scan_start( ::Replxx* replxx_ ) {
  518. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  519. return ( reinterpret_cast<ReplxxHistoryScan*>( replxx->history_scan().release() ) );
  520. }
  521. void replxx_history_scan_stop( ::Replxx*, ReplxxHistoryScan* historyScan_ ) {
  522. delete reinterpret_cast<replxx::Replxx::HistoryScanImpl*>( historyScan_ );
  523. }
  524. int replxx_history_scan_next( ::Replxx*, ReplxxHistoryScan* historyScan_, ReplxxHistoryEntry* historyEntry_ ) {
  525. replxx::Replxx::HistoryScanImpl* historyScan( reinterpret_cast<replxx::Replxx::HistoryScanImpl*>( historyScan_ ) );
  526. bool hasNext( historyScan->next() );
  527. if ( hasNext ) {
  528. replxx::Replxx::HistoryEntry const& historyEntry( historyScan->get() );
  529. historyEntry_->timestamp = historyEntry.timestamp().c_str();
  530. historyEntry_->text = historyEntry.text().c_str();
  531. }
  532. return ( hasNext ? 0 : -1 );
  533. }
  534. /* Save the history in the specified file. On success 0 is returned
  535. * otherwise -1 is returned. */
  536. int replxx_history_sync( ::Replxx* replxx_, const char* filename ) {
  537. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  538. return ( replxx->history_sync( filename ) ? 0 : -1 );
  539. }
  540. /* Save the history in the specified file. On success 0 is returned
  541. * otherwise -1 is returned. */
  542. int replxx_history_save( ::Replxx* replxx_, const char* filename ) {
  543. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  544. return ( replxx->history_save( filename ) ? 0 : -1 );
  545. }
  546. /* Load the history from the specified file. If the file does not exist
  547. * zero is returned and no operation is performed.
  548. *
  549. * If the file exists and the operation succeeded 0 is returned, otherwise
  550. * on error -1 is returned. */
  551. int replxx_history_load( ::Replxx* replxx_, const char* filename ) {
  552. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  553. return ( replxx->history_load( filename ) ? 0 : -1 );
  554. }
  555. void replxx_history_clear( ::Replxx* replxx_ ) {
  556. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  557. replxx->history_clear();
  558. }
  559. int replxx_history_size( ::Replxx* replxx_ ) {
  560. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  561. return ( replxx->history_size() );
  562. }
  563. /* This special mode is used by replxx in order to print scan codes
  564. * on screen for debugging / development purposes. It is implemented
  565. * by the replxx-c-api-example program using the --keycodes option. */
  566. #ifdef __REPLXX_DEBUG__
  567. void replxx_debug_dump_print_codes(void) {
  568. char quit[4];
  569. printf(
  570. "replxx key codes debugging mode.\n"
  571. "Press keys to see scan codes. Type 'quit' at any time to exit.\n");
  572. if (enableRawMode() == -1) return;
  573. memset(quit, ' ', 4);
  574. while (1) {
  575. char c;
  576. int nread;
  577. #if _WIN32
  578. nread = _read(STDIN_FILENO, &c, 1);
  579. #else
  580. nread = read(STDIN_FILENO, &c, 1);
  581. #endif
  582. if (nread <= 0) continue;
  583. memmove(quit, quit + 1, sizeof(quit) - 1); /* shift string to left. */
  584. quit[sizeof(quit) - 1] = c; /* Insert current char on the right. */
  585. if (memcmp(quit, "quit", sizeof(quit)) == 0) break;
  586. printf("'%c' %02x (%d) (type quit to exit)\n", isprint(c) ? c : '?', (int)c,
  587. (int)c);
  588. printf("\r"); /* Go left edge manually, we are in raw mode. */
  589. fflush(stdout);
  590. }
  591. disableRawMode();
  592. }
  593. #endif // __REPLXX_DEBUG__
  594. int replxx_install_window_change_handler( ::Replxx* replxx_ ) {
  595. replxx::Replxx::ReplxxImpl* replxx( reinterpret_cast<replxx::Replxx::ReplxxImpl*>( replxx_ ) );
  596. return ( replxx->install_window_change_handler() );
  597. }
  598. using namespace replxx::color;
  599. ReplxxColor replxx_color_combine( ReplxxColor color1_, ReplxxColor color2_ ) {
  600. return static_cast<ReplxxColor>( static_cast<replxx::Replxx::Color>( color1_ ) | static_cast<replxx::Replxx::Color>( color2_ ) );
  601. }
  602. ReplxxColor replxx_color_bg( ReplxxColor color_ ) {
  603. return static_cast<ReplxxColor>( color::bg( static_cast<replxx::Replxx::Color>( color_ ) ) );
  604. }
  605. ReplxxColor replxx_color_bold( ReplxxColor color_ ) {
  606. return static_cast<ReplxxColor>( color::bold( static_cast<replxx::Replxx::Color>( color_ ) ) );
  607. }
  608. ReplxxColor replxx_color_underline( ReplxxColor color_ ) {
  609. return static_cast<ReplxxColor>( color::underline( static_cast<replxx::Replxx::Color>( color_ ) ) );
  610. }
  611. ReplxxColor replxx_color_grayscale( int level_ ) {
  612. return static_cast<ReplxxColor>( color::grayscale( level_ ) );
  613. }
  614. ReplxxColor replxx_color_rgb666( int r_, int g_, int b_ ) {
  615. return static_cast<ReplxxColor>( color::rgb666( r_, g_, b_ ) );
  616. }