-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcstdio_driver.cpp
More file actions
67 lines (54 loc) · 1.6 KB
/
Copy pathcstdio_driver.cpp
File metadata and controls
67 lines (54 loc) · 1.6 KB
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
/* File: /lib/impl/cli/cstdio_driver.cpp
*
* This file is a part of cpplib project which is distributed under MIT License.
* See file LICENSE for full license details.
*
* Copyright (c) 2020-present Nikita Zuev (V.Slavski!) <[email protected]>
*/
#include <cstdio>
#include "../../../lib/utils/string.hpp"
#include "../../../lib/cli/interface.hpp"
#include "./cstdio_driver.hpp"
// IMPLEMENTATION lib::cli::impl::CstdioDriver
namespace lib::cli::impl {
CstdioDriver::~CstdioDriver() {
::std::fputc( '\n', stdout );
::std::fflush( stdout );
}
void CstdioDriver::out( const cstring & line )/* override*/ {
if ( not input_clear )
::std::fputc( '\n', stdout );
const auto & line_str = str( line );
::std::printf( "%s\n", line_str.c_str() );
::std::fflush( stdout );
input_clear = true;
}
bool CstdioDriver::update()/* override*/ {
auto * interface = get_link<Interface>();
if ( interface == nullptr )
return false; ///< @todo Possible it's legal to return "true" here.
redraw_input();
const char chr = (char)::std::getchar();
if ( is_ascii_printable( chr ) ) {
input_clear = false;
input_string += chr;
} else if ( chr == '\n' ) {
input_clear = true;
::std::string command;
::std::swap( command, input_string );
if ( not (*interface)( command ) )
out( COMMAND_NOT_FOUND + command );
}
return true;
}
void CstdioDriver::afterPromptChanged()/* override*/ {
redraw_input();
}
void CstdioDriver::redraw_input() {
if ( input_clear ) {
::std::printf( "%s%s", prompt().c_str(), input_string.c_str() );
::std::fflush( stdout );
}
input_clear = false;
}
} // namespace lib::cli::impl