ClickHouse/src/Common/UnicodeBar.cpp

63 lines
1.3 KiB
C++
Raw Normal View History

2020-09-19 19:15:16 +00:00
#include <cstring>
#include <cmath>
#include <string>
#include <common/types.h>
#include <common/arithmeticOverflow.h>
#include <Common/Exception.h>
#include <Common/UnicodeBar.h>
2021-01-02 20:40:15 +00:00
#include <Common/NaNUtils.h>
2020-09-19 19:15:16 +00:00
2021-01-02 20:40:15 +00:00
#include <iostream>
2020-09-19 19:15:16 +00:00
namespace UnicodeBar
{
2021-01-02 20:40:15 +00:00
double getWidth(double x, double min, double max, double max_width)
2020-09-19 19:15:16 +00:00
{
2021-01-02 20:40:15 +00:00
if (isNaN(x))
return 0;
2020-09-19 19:15:16 +00:00
if (x <= min)
return 0;
if (x >= max)
return max_width;
2021-01-02 20:40:15 +00:00
return (x - min) / (max - min) * max_width;
2020-09-19 19:15:16 +00:00
}
size_t getWidthInBytes(double width)
{
return ceil(width - 1.0 / 8) * UNICODE_BAR_CHAR_SIZE;
}
void render(double width, char * dst)
{
size_t floor_width = floor(width);
for (size_t i = 0; i < floor_width; ++i)
{
memcpy(dst, "", UNICODE_BAR_CHAR_SIZE);
dst += UNICODE_BAR_CHAR_SIZE;
}
size_t remainder = floor((width - floor_width) * 8);
if (remainder)
{
memcpy(dst, &"▏▎▍▌▋▋▊▉"[(remainder - 1) * UNICODE_BAR_CHAR_SIZE], UNICODE_BAR_CHAR_SIZE);
dst += UNICODE_BAR_CHAR_SIZE;
}
*dst = 0;
}
std::string render(double width)
{
std::string res(getWidthInBytes(width), '\0');
render(width, res.data());
return res;
}
}