From 0b12d72476c8c60b934cc8aa635c3b21e9bd4704 Mon Sep 17 00:00:00 2001 From: login Date: Tue, 26 Nov 2019 00:40:47 +0000 Subject: Improving messaging around messages The title is confusing, sorry. "No messages found" is shown in tcoin and pcoin when no messages are found (and this no-message message has one newline above and one newline below it instead of two newlines above it as before). --- pcoin.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 6 deletions(-) (limited to 'pcoin.cpp') diff --git a/pcoin.cpp b/pcoin.cpp index fb85cdd..337fc08 100644 --- a/pcoin.cpp +++ b/pcoin.cpp @@ -392,8 +392,48 @@ void show_messages(const char* username) { std::string messages_path = std::string(TCOIN_MSG_PATH) + std::string(username) + std::string("_messages.txt"); std::ifstream fin(messages_path.c_str()); - std::cout << fin.rdbuf(); - std::cout << "\n"; + char ch; + bool first_char_is_newline = false; + bool reached_eof = false; + for(int i=0; i < 2; ++i) + { + if(ch = fin.get()) + { + if(ch == std::istream::traits_type::eof()) //https://stackoverflow.com/questions/4533063/how-does-ifstreams-eof-work + { + if(first_char_is_newline && i==1) + { + reached_eof = true; + std::cout << "No messages found.\n"; + } + break; + } + else + { + if(i==0 && ch=='\n') + { + first_char_is_newline = true; + } + std::cout << ch; + } + } + } + if(!reached_eof) + { + std::cout << fin.rdbuf(); + } + + //removing eofbit + fin.clear(); + //moving back two places from the end to read the last two characters + fin.seekg(-2, std::ios::end); + char chs[2]; //chs = characters + chs[0] = fin.get(); + chs[1] = fin.get(); + fin.get(); //to set eofbit again because I like it to be just the way it was before removing the eofbit + if(chs[0]!='\n' && chs[1]=='\n') //if only one newline at the end of the file + std::cout << "\n"; //print another one + fin.close(); } @@ -436,11 +476,18 @@ void show_messages_tail(const char* username, int lineCount) continue; //we're counting cutting off a '\n ' (and '\n\n') as zero (and one) newline cut off because "\n \_message>\n\n" is one message --newlineCount; } - std::cout << "Last " << lineCount << " Messages:\n\n"; + std::cout << "Last " << lineCount << " Messages:\n"; std::vector::iterator end = remove(start, buffer.end(), '\r'); - std::cout << std::string(start, end); - if(*(end-2) != '\n') //if it ends with two newlines, don't put another one - std::cout << "\n"; + if((start == (end-1)) && (*(start) == '\n')) //there is only one character, and it is a newline (i.e.. no messages) + std::cout << "\nNo messages found.\n\n"; + else + { + if(*(start) != '\n') //if it starts with a newline, don't put another one + std::cout << "\n"; + std::cout << std::string(start, end); + if(*(end-2) != '\n' && *(end-1) == '\n') //if it ends with two newlines, don't put another one + std::cout << "\n"; + } } void show_tsv_messages_tail(const char* username, int lineCount) -- cgit 1.4.1-2-gfad0