introduction of new output functions to use instead of directly streaming to cout
This commit is contained in:
parent
8b2cb4ceba
commit
5a7353abc1
@ -57,7 +57,7 @@ add_dependencies(bottles cog)
|
|||||||
add_custom_target(cog ALL DEPENDS ${coggedfiles})
|
add_custom_target(cog ALL DEPENDS ${coggedfiles})
|
||||||
|
|
||||||
add_executable(cellar ${src}/cellar.cpp ${src}/commands.cpp ${src}/fs.cpp
|
add_executable(cellar ${src}/cellar.cpp ${src}/commands.cpp ${src}/fs.cpp
|
||||||
${src}/version.cpp)
|
${src}/version.cpp ${src}/output.cpp)
|
||||||
target_link_libraries(cellar ${Boost_LIBRARIES} bottles)
|
target_link_libraries(cellar ${Boost_LIBRARIES} bottles)
|
||||||
|
|
||||||
install(TARGETS cellar bottles
|
install(TARGETS cellar bottles
|
||||||
|
14
include/output.hpp
Normal file
14
include/output.hpp
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#ifndef __OUTPUT_HPP
|
||||||
|
#define __OUTPUT_HPP
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace cellar {
|
||||||
|
namespace output {
|
||||||
|
extern void statement(std::string parm);
|
||||||
|
extern void warning(std::string parm);
|
||||||
|
extern void error(std::string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // __OUTPUT_HPP
|
@ -1,11 +1,11 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "bottles.hpp"
|
#include "bottles.hpp"
|
||||||
#include "internal/bottles.hpp"
|
#include "internal/bottles.hpp"
|
||||||
|
#include "output.hpp"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace cellar::bottles;
|
using namespace cellar::bottles;
|
||||||
@ -13,37 +13,43 @@ using namespace cellar::bottles;
|
|||||||
void cellar::bottles::print_active_bottle(int argc, vector<string> argv) {
|
void cellar::bottles::print_active_bottle(int argc, vector<string> argv) {
|
||||||
map<string, Bottle> bottlemap = get_bottles();
|
map<string, Bottle> bottlemap = get_bottles();
|
||||||
if (bottlemap.find(".wine") == bottlemap.end()) { // not found
|
if (bottlemap.find(".wine") == bottlemap.end()) { // not found
|
||||||
cout << "no active wine bottle" << endl;
|
output::error("no active wine bottle");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Bottle active_bottle = bottlemap[".wine"];
|
Bottle active_bottle = bottlemap[".wine"];
|
||||||
string bottlepath = active_bottle.canonical_path;
|
string bottlepath = active_bottle.canonical_path;
|
||||||
|
stringstream outstr;
|
||||||
|
bool cellar_managed = true;
|
||||||
if (active_bottle.type == bottle_symlink) {
|
if (active_bottle.type == bottle_symlink) {
|
||||||
cout << "symlink to ";
|
outstr << "symlink to ";
|
||||||
string homedir = getenv("HOME");
|
string homedir = getenv("HOME");
|
||||||
if (active_bottle.canonical_path.substr(0, homedir.length()) == homedir) {
|
if (active_bottle.canonical_path.substr(0, homedir.length()) == homedir) {
|
||||||
bottlepath.replace(0, homedir.length() + 1, ""); // should convert "/home/someone/.wine.example" to ".wine.example"
|
bottlepath.replace(0, homedir.length() + 1, ""); // should convert "/home/someone/.wine.example" to ".wine.example"
|
||||||
active_bottle = bottlemap[bottlepath];
|
active_bottle = bottlemap[bottlepath];
|
||||||
} else {
|
} else {
|
||||||
cout << active_bottle.canonical_path << endl;
|
outstr << active_bottle.canonical_path;
|
||||||
|
cellar_managed = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (cellar_managed) {
|
||||||
switch (active_bottle.type) {
|
switch (active_bottle.type) {
|
||||||
case bottle_anonymous:
|
case bottle_anonymous:
|
||||||
cout << "anonymous wine bottle at " << active_bottle.canonical_path << endl;
|
outstr << "anonymous wine bottle at " << active_bottle.canonical_path;
|
||||||
return;
|
break;
|
||||||
case bottle_labelled:
|
case bottle_labelled:
|
||||||
cout << active_bottle.config["name"] << " (~/" << bottlepath << ")";
|
outstr << active_bottle.config["name"] << " (~/" << bottlepath << ")";
|
||||||
if (active_bottle.config.find("desc") != active_bottle.config.end()) {
|
if (active_bottle.config.find("desc") != active_bottle.config.end()) {
|
||||||
cout << " - " << active_bottle.config["desc"];
|
outstr << " - " << active_bottle.config["desc"];
|
||||||
}
|
}
|
||||||
cout << endl;
|
break;
|
||||||
return;
|
|
||||||
default:
|
default:
|
||||||
cout << "broken or unsupported wine bottle" << endl;
|
outstr << "broken or unsupported wine bottle";
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output::statement(outstr.str());
|
||||||
|
}
|
||||||
|
@ -13,8 +13,10 @@
|
|||||||
#include "internal/bottles.hpp"
|
#include "internal/bottles.hpp"
|
||||||
#include "dll.hpp"
|
#include "dll.hpp"
|
||||||
#include "fs.hpp"
|
#include "fs.hpp"
|
||||||
|
#include "output.hpp"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
using namespace cellar;
|
||||||
using namespace cellar::bottles;
|
using namespace cellar::bottles;
|
||||||
|
|
||||||
using CommandFunction = cellar::commands::CommandFunction;
|
using CommandFunction = cellar::commands::CommandFunction;
|
||||||
@ -31,7 +33,7 @@ DLL_PUBLIC map<string, Bottle> cellar::bottles::get_bottles() {
|
|||||||
map<string, Bottle> result;
|
map<string, Bottle> result;
|
||||||
|
|
||||||
string homepath = getenv("HOME");
|
string homepath = getenv("HOME");
|
||||||
vector<string> homedir = cellar::fs::listdir(homepath);
|
vector<string> homedir = fs::listdir(homepath);
|
||||||
for (string item : homedir) {
|
for (string item : homedir) {
|
||||||
if (item.substr(0,5) == ".wine") {
|
if (item.substr(0,5) == ".wine") {
|
||||||
Bottle output;
|
Bottle output;
|
||||||
@ -78,6 +80,8 @@ DLL_PUBLIC map<string, Bottle> cellar::bottles::get_bottles() {
|
|||||||
void cellar::bottles::print_bottles(int argc, vector<string> argv) {
|
void cellar::bottles::print_bottles(int argc, vector<string> argv) {
|
||||||
map<string, Bottle> bottles = get_bottles();
|
map<string, Bottle> bottles = get_bottles();
|
||||||
|
|
||||||
|
stringstream outstr;
|
||||||
|
|
||||||
for (auto item : bottles) {
|
for (auto item : bottles) {
|
||||||
if (item.first == ".wine" || item.first == ".wine.template") {
|
if (item.first == ".wine" || item.first == ".wine.template") {
|
||||||
// .wine is considered to be "active", and .wine.template is used as a template
|
// .wine is considered to be "active", and .wine.template is used as a template
|
||||||
@ -85,20 +89,21 @@ void cellar::bottles::print_bottles(int argc, vector<string> argv) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Bottle bottle = item.second;
|
Bottle bottle = item.second;
|
||||||
cout << item.first << " - ";
|
outstr << item.first << " - ";
|
||||||
switch (bottle.type) {
|
switch (bottle.type) {
|
||||||
case bottle_anonymous:
|
case bottle_anonymous:
|
||||||
cout << "anonymous wine bottle";
|
outstr << "anonymous wine bottle";
|
||||||
break;
|
break;
|
||||||
case bottle_symlink:
|
case bottle_symlink:
|
||||||
cout << "symlink to " << bottle.canonical_path;
|
outstr << "symlink to " << bottle.canonical_path;
|
||||||
break;
|
break;
|
||||||
case bottle_labelled:
|
case bottle_labelled:
|
||||||
cout << bottle.config["name"];
|
outstr << bottle.config["name"];
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
cout << "broken or unsupported wine bottle";
|
outstr << "broken or unsupported wine bottle";
|
||||||
}
|
}
|
||||||
cout << endl;
|
output::statement(outstr.str());
|
||||||
|
outstr.str("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
#include "bottles.hpp"
|
#include "bottles.hpp"
|
||||||
#include "cellar.hpp"
|
#include "cellar.hpp"
|
||||||
#include "commands.hpp"
|
#include "commands.hpp"
|
||||||
|
#include "output.hpp"
|
||||||
#include "version.hpp"
|
#include "version.hpp"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@ -18,8 +19,8 @@ using namespace cellar;
|
|||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
void cellar::print_header() {
|
void cellar::print_header() {
|
||||||
cout << "cellar - bottle management tool for WINE connoisseurs" << std::endl;
|
output::statement("cellar - bottle management tool for WINE connoisseurs");
|
||||||
cout << version::short_version() << std::endl;
|
output::statement(version::short_version());
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
@ -57,7 +58,10 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
commands::command_map[usercmd](subargv.size(), subargv);
|
commands::command_map[usercmd](subargv.size(), subargv);
|
||||||
} else {
|
} else {
|
||||||
cerr << "invalid command: " << usercmd << endl;
|
stringstream errstr;
|
||||||
|
|
||||||
|
errstr << "invalid command: " << usercmd;
|
||||||
|
output::error(errstr.str());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include <boost/filesystem/path.hpp>
|
#include <boost/filesystem/path.hpp>
|
||||||
|
|
||||||
#include "fs.hpp"
|
#include "fs.hpp"
|
||||||
|
#include "output.hpp"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@ -21,7 +22,8 @@ vector<string> cellar::fs::listdir(string path) {
|
|||||||
result.push_back(item);
|
result.push_back(item);
|
||||||
}
|
}
|
||||||
catch (const exception& exc) {
|
catch (const exception& exc) {
|
||||||
cout << "[1;31mfuck[0m" << endl;
|
// TODO: better error handling
|
||||||
|
cellar::output::error("[1;31mfuck[0m");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
14
src/output.cpp
Normal file
14
src/output.cpp
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "output.hpp"
|
||||||
|
|
||||||
|
void cellar::output::statement(std::string str_message) {
|
||||||
|
std::cout << "* " << str_message << std::endl;
|
||||||
|
}
|
||||||
|
void cellar::output::warning(std::string str_message) {
|
||||||
|
std::cerr << "* " << str_message << std::endl;
|
||||||
|
}
|
||||||
|
void cellar::output::error(std::string str_message) {
|
||||||
|
std::cerr << "* " << str_message << std::endl;
|
||||||
|
}
|
@ -4,6 +4,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "commands.hpp"
|
#include "commands.hpp"
|
||||||
|
#include "output.hpp"
|
||||||
#include "version.hpp"
|
#include "version.hpp"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@ -41,6 +42,6 @@ string cellar::version::short_version() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void print_version(int argc, vector<string> argv) {
|
void print_version(int argc, vector<string> argv) {
|
||||||
cout << short_version() << endl;
|
cellar::output::statement(short_version());
|
||||||
}
|
}
|
||||||
cellar::commands::CommandFunction versioncmd = cellar::commands::command_map["version"] = &print_version;
|
cellar::commands::CommandFunction versioncmd = cellar::commands::command_map["version"] = &print_version;
|
||||||
|
Loading…
Reference in New Issue
Block a user