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