[thin_ls, base] Factor out grid_layout
This commit is contained in:
parent
5aaa26fe34
commit
e15b11edb1
@ -31,6 +31,7 @@ SOURCE=\
|
||||
base/endian_utils.cc \
|
||||
base/error_state.cc \
|
||||
base/error_string.cc \
|
||||
base/grid_layout.cc \
|
||||
base/progress_monitor.cc \
|
||||
base/xml_utils.cc \
|
||||
block-cache/block_cache.cc \
|
||||
|
82
base/grid_layout.cc
Normal file
82
base/grid_layout.cc
Normal file
@ -0,0 +1,82 @@
|
||||
#include "base/grid_layout.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace base;
|
||||
using namespace std;
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
grid_layout::grid_layout()
|
||||
: nr_fields_(0)
|
||||
{
|
||||
new_row();
|
||||
}
|
||||
|
||||
void
|
||||
grid_layout::render(ostream &out) const
|
||||
{
|
||||
vector<unsigned> widths;
|
||||
calc_field_widths(widths);
|
||||
|
||||
grid::const_iterator row;
|
||||
for (row = grid_.begin(); row != grid_.end(); ++row) {
|
||||
row::const_iterator col;
|
||||
unsigned i;
|
||||
for (col = row->begin(), i = 0; col != row->end(); ++col, ++i)
|
||||
out << justify(widths[i], *col) << " ";
|
||||
out << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
grid_layout::new_row()
|
||||
{
|
||||
grid_.push_back(row());
|
||||
}
|
||||
|
||||
grid_layout::row const &
|
||||
grid_layout::current_row() const
|
||||
{
|
||||
return grid_.back();
|
||||
}
|
||||
|
||||
grid_layout::row &
|
||||
grid_layout::current_row()
|
||||
{
|
||||
return grid_.back();
|
||||
}
|
||||
|
||||
void
|
||||
grid_layout::push_field(string const &s)
|
||||
{
|
||||
current_row().push_back(s);
|
||||
nr_fields_ = max<unsigned>(nr_fields_, current_row().size());
|
||||
}
|
||||
|
||||
void
|
||||
grid_layout::calc_field_widths(vector<unsigned> &widths) const
|
||||
{
|
||||
widths.resize(nr_fields_, 0);
|
||||
|
||||
grid::const_iterator row;
|
||||
for (row = grid_.begin(); row != grid_.end(); ++row) {
|
||||
row::const_iterator col;
|
||||
unsigned i;
|
||||
for (col = row->begin(), i = 0; col != row->end(); ++col, ++i)
|
||||
widths[i] = max<unsigned>(widths[i], col->length());
|
||||
}
|
||||
}
|
||||
|
||||
string
|
||||
grid_layout::justify(unsigned width, string const &txt) const
|
||||
{
|
||||
if (txt.length() > width)
|
||||
throw runtime_error("string field too long, internal error");
|
||||
|
||||
string result(width - txt.length(), ' ');
|
||||
result += txt;
|
||||
return result;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
41
base/grid_layout.h
Normal file
41
base/grid_layout.h
Normal file
@ -0,0 +1,41 @@
|
||||
#ifndef BASE_GRID_LAYOUT_H
|
||||
#define BASE_GRID_LAYOUT_H
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <iosfwd>
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace base {
|
||||
class grid_layout {
|
||||
public:
|
||||
typedef std::list<std::string> row;
|
||||
typedef std::list<row> grid;
|
||||
|
||||
grid_layout();
|
||||
void render(std::ostream &out) const;
|
||||
void new_row();
|
||||
|
||||
template <typename T>
|
||||
void field(T const &t) {
|
||||
push_field(boost::lexical_cast<std::string>(t));
|
||||
}
|
||||
|
||||
private:
|
||||
row ¤t_row();
|
||||
row const ¤t_row() const;
|
||||
void push_field(std::string const &s);
|
||||
void calc_field_widths(std::vector<unsigned> &widths) const;
|
||||
std::string justify(unsigned width, std::string const &txt) const;
|
||||
|
||||
grid grid_;
|
||||
unsigned nr_fields_;
|
||||
};
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
#endif
|
@ -22,6 +22,7 @@
|
||||
#include <libgen.h>
|
||||
|
||||
#include "base/disk_units.h"
|
||||
#include "base/grid_layout.h"
|
||||
#include "boost/lexical_cast.hpp"
|
||||
#include "boost/optional.hpp"
|
||||
#include "boost/range.hpp"
|
||||
@ -43,77 +44,6 @@ using namespace thin_provisioning;
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
// FIXME: move to own file
|
||||
class grid_layout {
|
||||
public:
|
||||
typedef list<string> row;
|
||||
typedef list<row> grid;
|
||||
|
||||
grid_layout()
|
||||
: nr_fields_(0) {
|
||||
new_row();
|
||||
}
|
||||
|
||||
void render(ostream &out) {
|
||||
vector<unsigned> widths;
|
||||
calc_field_widths(widths);
|
||||
|
||||
grid::const_iterator row;
|
||||
for (row = grid_.begin(); row != grid_.end(); ++row) {
|
||||
row::const_iterator col;
|
||||
unsigned i;
|
||||
for (col = row->begin(), i = 0; col != row->end(); ++col, ++i)
|
||||
out << justify(widths[i], *col) << " ";
|
||||
out << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void new_row() {
|
||||
grid_.push_back(row());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void field(T const &t) {
|
||||
push_field(lexical_cast<string>(t));
|
||||
}
|
||||
|
||||
private:
|
||||
row ¤t_row() {
|
||||
return grid_.back();
|
||||
}
|
||||
|
||||
void push_field(string const &s) {
|
||||
current_row().push_back(s);
|
||||
nr_fields_ = max<unsigned>(nr_fields_, current_row().size());
|
||||
}
|
||||
|
||||
void calc_field_widths(vector<unsigned> &widths) const {
|
||||
widths.resize(nr_fields_, 0);
|
||||
|
||||
grid::const_iterator row;
|
||||
for (row = grid_.begin(); row != grid_.end(); ++row) {
|
||||
row::const_iterator col;
|
||||
unsigned i;
|
||||
for (col = row->begin(), i = 0; col != row->end(); ++col, ++i)
|
||||
widths[i] = max<unsigned>(widths[i], col->length());
|
||||
}
|
||||
}
|
||||
|
||||
string justify(unsigned width, string const &txt) const {
|
||||
if (txt.length() > width)
|
||||
throw runtime_error("string field too long, internal error");
|
||||
|
||||
string result(width - txt.length(), ' ');
|
||||
result += txt;
|
||||
return result;
|
||||
}
|
||||
|
||||
grid grid_;
|
||||
unsigned nr_fields_;
|
||||
};
|
||||
|
||||
//------------------------------------------------
|
||||
|
||||
class mapping_set {
|
||||
public:
|
||||
mapping_set(block_address nr_blocks)
|
||||
|
Loading…
Reference in New Issue
Block a user