summaryrefslogtreecommitdiffstats
path: root/contrib/replxx/src/prompt.cxx
blob: c13ea808bf43dc81b4a726d8bfa1af84da2ff67e (plain)
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
#ifdef _WIN32

#include <conio.h>
#include <windows.h>
#include <io.h>
#if _MSC_VER < 1900 && defined (_MSC_VER)
#define snprintf _snprintf	// Microsoft headers use underscores in some names
#endif
#define strcasecmp _stricmp
#define strdup _strdup
#define write _write
#define STDIN_FILENO 0

#else /* _WIN32 */

#include <unistd.h>

#endif /* _WIN32 */

#include "prompt.hxx"
#include "util.hxx"

namespace replxx {

Prompt::Prompt( Terminal& terminal_ )
	: _extraLines( 0 )
	, _lastLinePosition( 0 )
	, _cursorRowOffset( 0 )
	, _screenColumns( 0 )
	, _terminal( terminal_ ) {
}

void Prompt::write() {
	_terminal.write32( _text.get(), _text.length() );
}

void Prompt::update_screen_columns( void ) {
	_screenColumns = _terminal.get_screen_columns();
}

void Prompt::set_text( UnicodeString const& text_ ) {
	_text = text_;
	update_state();
}

void Prompt::update_state() {
	_cursorRowOffset -= _extraLines;
	_extraLines = 0;
	_lastLinePosition = 0;
	_screenColumns = 0;
	update_screen_columns();
	// strip control characters from the prompt -- we do allow newline
	UnicodeString::const_iterator in( _text.begin() );
	UnicodeString::iterator out( _text.begin() );

	int visibleCount = 0;
	int x = 0;

	bool const strip = !tty::out;

	while (in != _text.end()) {
		char32_t c = *in;
		if ('\n' == c || !is_control_code(c)) {
			*out = c;
			++out;
			++in;
			++visibleCount;
			if ('\n' == c || ++x >= _screenColumns) {
				x = 0;
				++_extraLines;
				_lastLinePosition = visibleCount;
			}
		} else if (c == '\x1b') {
			if ( strip ) {
				// jump over control chars
				++in;
				if (*in == '[') {
					++in;
					while ( ( in != _text.end() ) && ( ( *in == ';' ) || ( ( ( *in >= '0' ) && ( *in <= '9' ) ) ) ) ) {
						++in;
					}
					if (*in == 'm') {
						++in;
					}
				}
			} else {
				// copy control chars
				*out = *in;
				++out;
				++in;
				if (*in == '[') {
					*out = *in;
					++out;
					++in;
					while ( ( in != _text.end() ) && ( ( *in == ';' ) || ( ( ( *in >= '0' ) && ( *in <= '9' ) ) ) ) ) {
						*out = *in;
						++out;
						++in;
					}
					if (*in == 'm') {
						*out = *in;
						++out;
						++in;
					}
				}
			}
		} else {
			++in;
		}
	}
	_characterCount = visibleCount;
	int charCount( static_cast<int>( out - _text.begin() ) );
	_text.erase( charCount, _text.length() - charCount );

	_cursorRowOffset += _extraLines;
}

int Prompt::indentation() const {
	return _characterCount - _lastLinePosition;
}

// Used with DynamicPrompt (history search)
//
const UnicodeString forwardSearchBasePrompt("(i-search)`");
const UnicodeString reverseSearchBasePrompt("(reverse-i-search)`");
const UnicodeString endSearchBasePrompt("': ");

DynamicPrompt::DynamicPrompt( Terminal& terminal_, int initialDirection )
	: Prompt( terminal_ )
	, _searchText()
	, _direction( initialDirection ) {
	updateSearchPrompt();
}

void DynamicPrompt::updateSearchPrompt(void) {
	update_screen_columns();
	const UnicodeString* basePrompt =
			(_direction > 0) ? &forwardSearchBasePrompt : &reverseSearchBasePrompt;
	_text.assign( *basePrompt ).append( _searchText ).append( endSearchBasePrompt );
	update_state();
}

}