gctl/lib/utility/progress_bar.h

118 lines
4.4 KiB
C
Raw Permalink Normal View History

2024-09-10 15:45:07 +08:00
/********************************************************
*
*
*
*
*
*
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn)
*
* GCTL is distributed under a dual licensing scheme. You can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 2
* of the License, or (at your option) any later version. You should have
* received a copy of the GNU Lesser General Public License along with this
* program. If not, see <http://www.gnu.org/licenses/>.
*
* If the terms and conditions of the LGPL v.2. would prevent you from using
* the GCTL, please consider the option to obtain a commercial license for a
* fee. These licenses are offered by the GCTL's original author. As a rule,
* licenses are provided "as-is", unlimited in time for a one time fee. Please
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
* to include some description of your company and the realm of its activities.
* Also add information on how to contact you by electronic and paper mail.
******************************************************/
#ifndef _GCTL_PROGRESS_BAR_H
#define _GCTL_PROGRESS_BAR_H
#include "../core/exceptions.h"
#ifdef _WINDOWS
#include "windows.h"
#else
#include "sys/ioctl.h"
#endif
#include "cstring"
#include "iostream"
#include "iomanip"
#include "thread"
#include "chrono"
//进度条宏定义
#define TOTAL_PERCENTAGE 100.0 ///< 设置总的百分比为100%。
#define CHARACTER_WIDTH_PERCENTAGE 5 ///< 设置每显示一个字符所占的百分比为5%。
namespace gctl
{
/**
* @brief
* @warning
*/
class progress_bar
{
public:
progress_bar(); ///< 类默认的构造函数。
/**
* @brief
*
* @param[in] n_
* @param[in] description_
* @param out_
*/
progress_bar(unsigned long n_, const char *description_="Progress", std::ostream& out_=std::cerr);
/**
* @brief
*
* @param[in] n_
* @param[in] description_
* @param out_
*/
void reset(unsigned long n_, const char *description_="Progress", std::ostream& out_=std::cerr);
/**
* @brief
*
* @param[in] frequency_update_
*/
void set_frequency_update(unsigned long frequency_update_);
/**
* @brief
*
* @param[in] unit_bar_
* @param[in] unit_space_
*/
void set_style(const char* unit_bar_, const char* unit_space_);
/**
* @brief
*
* @param[in] idx_
*/
void progressed(unsigned long idx_);
/**
* @brief
*
* @param[in] idx_
*/
void progressed_simple(unsigned long idx_);
protected:
unsigned long n;
unsigned int desc_width;
unsigned long frequency_update;
std::ostream* out;
const char *description;
const char *unit_bar;
const char *unit_space;
void clear_bar_field();
int get_console_width();
int get_bar_length();
};
}
#endif // _GCTL_PROGRESS_BAR_H