Chops Net IP
Loading...
Searching...
No Matches
simple_chat_screen.hpp
Go to the documentation of this file.
1
14#ifndef SIMPLE_CHAT_SCREEN_HPP_INCLUDED
15#define SIMPLE_CHAT_SCREEN_HPP_INCLUDED
16
17#include <iostream>
18#include <string>
19#include <vector>
20#include <cstdlib> // std::system
21
22const int NUN_SCROLL_LINES = 10;
23
24// shared globals
25const std::string PARAM_CONNECT = "-connect";
26const std::string PARAM_ACCEPT = "-accept";
27const std::string REMOTE = "[remote] ";
28
29// handle all methods to print output to stdout
31private:
32 const std::string m_ip_addr; // IP address or host name
33 const std::string m_port; // connection port
34 const std::string m_connect_type; // @c '-connect' or @c '-accept'
35 std::string m_upper_screen; // fixed upper region of screen output
36 std::string m_scroll_text; // history scroll text region
37 const int m_num_scroll_lines; // number of 'scroll lines'
38
39public:
40 simple_chat_screen(const std::string& ip, const std::string& port, const std::string& type,
41 bool print_errors, int num_lines = NUN_SCROLL_LINES) :
42 m_ip_addr(ip), m_port(port), m_connect_type(type), m_num_scroll_lines(num_lines) {
43 create_upper_screen(print_errors);
44 create_scroll_text();
45 };
46
47 // print the output to stdout
48 // called after @c insert_scroll_line
49 void draw_screen() {
50 clear_screen();
51 std::cout << (m_upper_screen + m_scroll_text + BOTTOM + PROMPT);
52 }
53
54 // the scroll region has a fixed numbmer of 'scroll lines'.
55 // calculate complete new scroll line; delete old line at top
56 // of text scroll region; add new scroll line (append)
57 void insert_scroll_line(const std::string& text, const std::string& prefix) {
58
59 // create the new scroll line
60 // remove DELIM at end of text
61 std::string new_scroll_line = "| " + prefix +
62 text.substr(0, text.size() - 1);
63 new_scroll_line +=
64 BLANK_LINE.substr(new_scroll_line.length(), std::string::npos);
65
66 // remove old scroll line in scroll text (using substr), add new scroll line
67 m_scroll_text =
68 m_scroll_text.substr(m_scroll_text.find_first_of("\n") + 1, std::string::npos) +
69 new_scroll_line;
70 }
71
72private:
73 using S = const std::string;
74 // string constants for output
75 S TOP =
76 "\n_____________________________________________________________________________\n";
77 S BLANK_LINE =
78 "| |\n";
79 S DIVIDOR =
80 "|___________________________________________________________________________|\n";
81 S HDR_1 =
82 "| chops-net-ip chat network demo |\n";
83 S HDR_IP =
84 "| IP address: ";
85 S HDR_PORT =
86 " port: ";
87 S HDR_TYPE =
88 "| connection type: ";
89 S CONNECT_T =
90 "connector |\n";
91 S ACCEPT_T =
92 "acceptor |\n";
93 S ERR_LOG_ON =
94 "| errors printed to console: ON |\n";
95 S ERR_LOG_OFF =
96 "| errors printed to console: OFF |\n";
97 S HDR_INSTR =
98 "| Enter text to send at prompt. Enter 'quit' to exit. |\n";
99 S SCROLL_LINE =
100 "| ";
101 S BOTTOM =
102 "|---------------------------------------------------------------------------|\n";
103 S HDR_START =
104 "| ";
105 S PROMPT = "| > ";
106
107 // create the string that represent the (unchanging) upper screen so only calculate once
108 void create_upper_screen(bool print_err) {
109 std::string hdr_info = HDR_IP + (m_ip_addr == "" ? "\"\"" : m_ip_addr) + HDR_PORT +
110 m_port ;
111 hdr_info += BLANK_LINE.substr(hdr_info.size(), std::string::npos);
112 m_upper_screen =
113 TOP + BLANK_LINE + HDR_1 + DIVIDOR + BLANK_LINE + hdr_info + HDR_TYPE +
114 (m_connect_type == PARAM_ACCEPT ? ACCEPT_T : CONNECT_T) +
115 (print_err ? ERR_LOG_ON : ERR_LOG_OFF) +
116 DIVIDOR + BLANK_LINE + HDR_INSTR + DIVIDOR;
117 }
118
119 // seed the scroll text region with m_scoll_lines number of blank lines
120 void create_scroll_text() {
121 int count = m_num_scroll_lines;
122 while (count-- > 0) {
123 m_scroll_text += BLANK_LINE;
124 }
125 }
126
127 // not recommended, but adequate for this demo
128 // for problems with system("clear") see
129 // http://www.cplusplus.com/articles/4z18T05o/
130 void clear_screen() {
131 #ifdef _WIN32
132 system("cls");
133 #else
134 system("clear");
135 #endif
136 }
137};
138
139#endif
Definition simple_chat_screen.hpp:30