move formatting logic to output modules
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 04 May 2015 00:12:33 +0200
changeset 11870b868b6b57
parent 10 145b45ef7751
child 12 fde958908539
move formatting logic to output modules
c++/parameter-lister/CLI.cpp
c++/parameter-lister/info/globalcode/parameterLister/OutputModule.cpp
c++/parameter-lister/info/globalcode/parameterLister/OutputModule.h
c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.cpp
c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.h
c++/parameter-lister/info/globalcode/parameterLister/terminalCodes/TerminalCodes.h
     1.1 --- a/c++/parameter-lister/CLI.cpp	Sun May 03 23:41:43 2015 +0200
     1.2 +++ b/c++/parameter-lister/CLI.cpp	Mon May 04 00:12:33 2015 +0200
     1.3 @@ -25,10 +25,6 @@
     1.4  	}
     1.5  }
     1.6  
     1.7 -void process(shared_ptr<OutputModule> om) {
     1.8 -	om->process(cout);
     1.9 -}
    1.10 -
    1.11  }
    1.12  }
    1.13  }
    1.14 @@ -46,16 +42,7 @@
    1.15  		args.push_back(argv[i]);
    1.16  	}
    1.17  
    1.18 -	for_each(args.begin(), args.end(), [command, fgGreen, fgReset](string s) {
    1.19 -		cout << fgGreen << command << fgReset << ": " << s << endl;
    1.20 -	});
    1.21 -
    1.22 -
    1.23 -	for (int i = 0; i < args.size(); i++) {
    1.24 -		string s = args[i];
    1.25 -		cout << i + 1 << ":" << s.length() << " = \"" << s << "\"" << endl;
    1.26 -	}
    1.27 -
    1.28 +	/** Load environment variable */
    1.29  	{
    1.30  		string envName = args[0];
    1.31  		boost::optional<string> outputModule = getenv(envName);
    1.32 @@ -67,14 +54,15 @@
    1.33  		}
    1.34  	}
    1.35  
    1.36 +	/** Do formatting */
    1.37  	{
    1.38  		shared_ptr<OutputModule> om(new OutputModule());
    1.39  		shared_ptr<TerminalOutputModule> tom(new TerminalOutputModule());
    1.40  		shared_ptr<OutputModule> o(new TerminalOutputModule());
    1.41  
    1.42 -		process(om);
    1.43 -		process(tom);
    1.44 -		process(o);
    1.45 +		om->process(cout, command, args);
    1.46 +		tom->process(cout, command, args);
    1.47 +		o->process(cout, command, args);
    1.48  	}
    1.49  
    1.50  	return 0;
     2.1 --- a/c++/parameter-lister/info/globalcode/parameterLister/OutputModule.cpp	Sun May 03 23:41:43 2015 +0200
     2.2 +++ b/c++/parameter-lister/info/globalcode/parameterLister/OutputModule.cpp	Mon May 04 00:12:33 2015 +0200
     2.3 @@ -1,4 +1,7 @@
     2.4 +#include <algorithm>
     2.5 +
     2.6  #include "OutputModule.h"
     2.7 +#include "terminalCodes/TerminalCodes.h"
     2.8  
     2.9  namespace info {
    2.10  namespace globalcode {
    2.11 @@ -13,8 +16,19 @@
    2.12  OutputModule::~OutputModule() {
    2.13  }
    2.14  
    2.15 -void OutputModule::process(std::ostream &output) {
    2.16 -	output << "OutputModule!" << std::endl;
    2.17 +void OutputModule::process(std::ostream &output, std::string &command, std::vector<std::string> &args) {
    2.18 +	terminalCodes::Modifier fgGreen(terminalCodes::FG_GREEN);
    2.19 +	terminalCodes::Modifier fgReset(terminalCodes::FG_DEFAULT);
    2.20 +
    2.21 +	using namespace std;
    2.22 +
    2.23 +	output << "<outputModule>" << endl;
    2.24 +
    2.25 +	for_each(args.begin(), args.end(), [command, fgGreen, fgReset](string s) {
    2.26 +		cout << fgGreen << command << fgReset << ": " << s << endl;
    2.27 +	});
    2.28 +	
    2.29 +	output << "</outputModule>" << endl;
    2.30  }
    2.31  
    2.32  
     3.1 --- a/c++/parameter-lister/info/globalcode/parameterLister/OutputModule.h	Sun May 03 23:41:43 2015 +0200
     3.2 +++ b/c++/parameter-lister/info/globalcode/parameterLister/OutputModule.h	Mon May 04 00:12:33 2015 +0200
     3.3 @@ -2,6 +2,7 @@
     3.4  #define	OUTPUTMODULE_H
     3.5  
     3.6  #include <iostream>
     3.7 +#include <vector>
     3.8  
     3.9  namespace info {
    3.10  namespace globalcode {
    3.11 @@ -12,7 +13,7 @@
    3.12  	OutputModule();
    3.13  	OutputModule(const OutputModule& orig);
    3.14  	virtual ~OutputModule();
    3.15 -	virtual void process(std::ostream &output);
    3.16 +	virtual void process(std::ostream &output, std::string &command, std::vector<std::string> &args);
    3.17  private:
    3.18  
    3.19  };
     4.1 --- a/c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.cpp	Sun May 03 23:41:43 2015 +0200
     4.2 +++ b/c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.cpp	Mon May 04 00:12:33 2015 +0200
     4.3 @@ -13,8 +13,16 @@
     4.4  TerminalOutputModule::~TerminalOutputModule() {
     4.5  }
     4.6  
     4.7 -void TerminalOutputModule::process(std::ostream &output) {
     4.8 -	output << "TerminalOutputModule!" << std::endl;
     4.9 +void TerminalOutputModule::process(std::ostream &output, std::string &command, std::vector<std::string> &args) {
    4.10 +	using namespace std;
    4.11 +	output << "<terminalOutputModule>" << endl;
    4.12 +	
    4.13 +	for (int i = 0; i < args.size(); i++) {
    4.14 +		string s = args[i];
    4.15 +		cout << i + 1 << ":" << s.length() << " = \"" << s << "\"" << endl;
    4.16 +	}
    4.17 +	
    4.18 +	output << "</terminalOutputModule>" << endl;
    4.19  }
    4.20  
    4.21  }
     5.1 --- a/c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.h	Sun May 03 23:41:43 2015 +0200
     5.2 +++ b/c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.h	Mon May 04 00:12:33 2015 +0200
     5.3 @@ -14,7 +14,7 @@
     5.4  	TerminalOutputModule();
     5.5  	TerminalOutputModule(const TerminalOutputModule& orig);
     5.6  	virtual ~TerminalOutputModule();
     5.7 -	virtual void process(std::ostream &output);
     5.8 +	virtual void process(std::ostream &output, std::string &command, std::vector<std::string> &args);
     5.9  private:
    5.10  
    5.11  };
     6.1 --- a/c++/parameter-lister/info/globalcode/parameterLister/terminalCodes/TerminalCodes.h	Sun May 03 23:41:43 2015 +0200
     6.2 +++ b/c++/parameter-lister/info/globalcode/parameterLister/terminalCodes/TerminalCodes.h	Mon May 04 00:12:33 2015 +0200
     6.3 @@ -1,5 +1,5 @@
     6.4 -#ifndef COLORZ_H
     6.5 -#define	COLORZ_H
     6.6 +#ifndef TERMINALCODES_H
     6.7 +#define	TERMINALCODES_H
     6.8  
     6.9  #include <ostream>
    6.10  
    6.11 @@ -37,5 +37,5 @@
    6.12  }
    6.13  }
    6.14  
    6.15 -#endif	/* COLORZ_H */
    6.16 +#endif	/* TERMINALCODES_H */
    6.17