introduction of new output functions to use instead of directly streaming to cout

This commit is contained in:
Nicholas O'Connor
2017-03-23 18:51:11 -07:00
parent 8b2cb4ceba
commit 5a7353abc1
8 changed files with 77 additions and 31 deletions

View File

@@ -1,11 +1,11 @@
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include "bottles.hpp"
#include "internal/bottles.hpp"
#include "output.hpp"
using namespace std;
using namespace cellar::bottles;
@@ -13,37 +13,43 @@ using namespace cellar::bottles;
void cellar::bottles::print_active_bottle(int argc, vector<string> argv) {
map<string, Bottle> bottlemap = get_bottles();
if (bottlemap.find(".wine") == bottlemap.end()) { // not found
cout << "no active wine bottle" << endl;
output::error("no active wine bottle");
return;
}
Bottle active_bottle = bottlemap[".wine"];
string bottlepath = active_bottle.canonical_path;
stringstream outstr;
bool cellar_managed = true;
if (active_bottle.type == bottle_symlink) {
cout << "symlink to ";
outstr << "symlink to ";
string homedir = getenv("HOME");
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"
active_bottle = bottlemap[bottlepath];
} else {
cout << active_bottle.canonical_path << endl;
outstr << active_bottle.canonical_path;
cellar_managed = false;
return;
}
}
switch (active_bottle.type) {
case bottle_anonymous:
cout << "anonymous wine bottle at " << active_bottle.canonical_path << endl;
return;
case bottle_labelled:
cout << active_bottle.config["name"] << " (~/" << bottlepath << ")";
if (active_bottle.config.find("desc") != active_bottle.config.end()) {
cout << " - " << active_bottle.config["desc"];
}
cout << endl;
return;
default:
cout << "broken or unsupported wine bottle" << endl;
return;
if (cellar_managed) {
switch (active_bottle.type) {
case bottle_anonymous:
outstr << "anonymous wine bottle at " << active_bottle.canonical_path;
break;
case bottle_labelled:
outstr << active_bottle.config["name"] << " (~/" << bottlepath << ")";
if (active_bottle.config.find("desc") != active_bottle.config.end()) {
outstr << " - " << active_bottle.config["desc"];
}
break;
default:
outstr << "broken or unsupported wine bottle";
break;
}
}
output::statement(outstr.str());
}

View File

@@ -13,8 +13,10 @@
#include "internal/bottles.hpp"
#include "dll.hpp"
#include "fs.hpp"
#include "output.hpp"
using namespace std;
using namespace cellar;
using namespace cellar::bottles;
using CommandFunction = cellar::commands::CommandFunction;
@@ -31,7 +33,7 @@ DLL_PUBLIC map<string, Bottle> cellar::bottles::get_bottles() {
map<string, Bottle> result;
string homepath = getenv("HOME");
vector<string> homedir = cellar::fs::listdir(homepath);
vector<string> homedir = fs::listdir(homepath);
for (string item : homedir) {
if (item.substr(0,5) == ".wine") {
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) {
map<string, Bottle> bottles = get_bottles();
stringstream outstr;
for (auto item : bottles) {
if (item.first == ".wine" || item.first == ".wine.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;
}
Bottle bottle = item.second;
cout << item.first << " - ";
outstr << item.first << " - ";
switch (bottle.type) {
case bottle_anonymous:
cout << "anonymous wine bottle";
outstr << "anonymous wine bottle";
break;
case bottle_symlink:
cout << "symlink to " << bottle.canonical_path;
outstr << "symlink to " << bottle.canonical_path;
break;
case bottle_labelled:
cout << bottle.config["name"];
outstr << bottle.config["name"];
break;
default:
cout << "broken or unsupported wine bottle";
outstr << "broken or unsupported wine bottle";
}
cout << endl;
output::statement(outstr.str());
outstr.str("");
}
}