Files
klammertext/mac/error.cpp
T

47 lines
1.4 KiB
C++
Raw Normal View History

#include <sstream>
#include "error.h"
#include "show.h"
#include "util.h"
#include "file.h"
bool display_source(const std::string& filename_arg)
{
if (filename_arg.empty()) {
return false;
} else {
fs::path filename = fs::path(filename_arg).filename();
return sks_commands.count(filename.string()) == 0;
}
}
std::string Error::message(const std::string& epilog) const
{
// The separator is not cosmetic: the description ends with a sentence, and
// appending the advice straight onto it ran the two together into one word
// ("...for an unspecified targetkdiag loads no klammerset..."). m_desc is
// then justified to 80 columns, so the break has to be a blank line rather
// than a single newline, which justify() would fold back into the flow.
std::string desc = m_desc;
if (epilog != "") {
desc += "\n\n" + epilog + "\n";
}
std::stringstream ss {};
ss << command_name << " (" << m_type << " error)";
if (display_source(m_loc.m_filename)) {
ss << ":\n " << m_loc.m_filename;
}
if (m_loc.m_line > -1) {
ss << ", line " << m_loc.m_line;
}
if (m_loc.m_chr > -1) {
ss << ", character " << m_loc.m_chr + 1;
}
ss << ":\n\n" << justify(desc, 80, 0);
return ss.str();
}
void Error::print_message(const std::string& epilog)
{
std::cerr << "\n" << red << message(epilog) << reset << "\n";
}