cpp-terminal 1.0.0
Small C++ library for writing multiplatform terminal applications
Loading...
Searching...
No Matches
prompt.hpp
Go to the documentation of this file.
1/*
2* cpp-terminal
3* C++ library for writing multi-platform terminal applications.
4*
5* SPDX-FileCopyrightText: 2019-2024 cpp-terminal
6*
7* SPDX-License-Identifier: MIT
8*/
9
10#pragma once
11
14
15#include <functional>
16
17namespace Term
18{
19
20// indicates the results of prompt_blocking() and prompt_non_blocking
21enum class Result
22{
23 Yes,
24 No,
25 Error,
26 None,
27 Abort,
28 Invalid
29};
30
41Result prompt(const std::string& message, const std::string& first_option, const std::string& second_option, const std::string& prompt_indicator, bool);
42
43// indicates the results of prompt_simple()
44enum class Result_simple
45{
46
47 Yes,
48 No,
49 Abort
50};
51
59Result_simple prompt_simple(const std::string& message);
60
61/* Multiline prompt */
62
63// This model contains all the information about the state of the prompt in an
64// abstract way, irrespective of where or how it is rendered.
65class Model
66{
67public:
68 std::string prompt_string; // The string to show as the prompt
69 std::vector<std::string> lines{""}; // The current input string in the prompt as a vector of lines, without '\n' at the end.
70 // The current cursor position in the "input" string, starting from (1,1)
71 std::size_t cursor_col{1};
72 std::size_t cursor_row{1};
73};
74
75std::string concat(const std::vector<std::string>&);
76
77std::vector<std::string> split(const std::string&);
78
79void print_left_curly_bracket(Term::Window&, const std::size_t&, const std::size_t&, const std::size_t&);
80
81void render(Term::Window&, const Model&, const std::size_t&);
82
83std::string prompt_multiline(const std::string&, std::vector<std::string>&, std::function<bool(std::string)>&);
84} // namespace Term
std::vector< std::string > lines
Definition prompt.hpp:69
std::size_t cursor_row
Definition prompt.hpp:72
std::size_t cursor_col
Definition prompt.hpp:71
std::string prompt_string
Definition prompt.hpp:68
Represents a rectangular window, as a 2D array of characters and their attributes.
Definition window.hpp:34
Definition args.cpp:13
std::string prompt_multiline(const std::string &, std::vector< std::string > &, std::function< bool(std::string)> &)
Definition prompt.cpp:209
std::vector< std::string > split(const std::string &)
Definition prompt.cpp:150
void print_left_curly_bracket(Term::Window &, const std::size_t &, const std::size_t &, const std::size_t &)
Definition prompt.cpp:175
Result prompt(const std::string &message, const std::string &first_option, const std::string &second_option, const std::string &prompt_indicator, bool)
A simple yes/no prompt, requires the user to press the ENTER key to continue.
Definition prompt.cpp:26
std::string concat(const std::vector< std::string > &)
Definition prompt.cpp:143
Result_simple prompt_simple(const std::string &message)
The most simple prompt possible, requires the user to press enter to continue.
Definition prompt.cpp:129
void render(Term::Window &, const Model &, const std::size_t &)
Definition prompt.cpp:187
Result
Definition prompt.hpp:22
@ Invalid
Returned if the given input did not match the case yes of no.
@ None
Returned if the enter key was pressed without additional input.
@ Abort
Returned if CTRL+C was pressed.
@ Error
Returned if no terminal is attached to the program.
@ Yes
Returned if the user chose yes.
@ No
Returned if the user chose no.
Result_simple
Definition prompt.hpp:45