gctl/lib/utility/progress_bar.h

129 lines
4.6 KiB
C
Raw 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
#ifdef _WINDOWS
2025-02-06 21:17:24 +08:00
#include <windows.h>
2024-09-10 15:45:07 +08:00
#else
2025-02-06 21:17:24 +08:00
#include <sys/ioctl.h>
2024-09-10 15:45:07 +08:00
#endif
2025-02-06 21:17:24 +08:00
#include <cstring>
#include <iostream>
#include <iomanip>
#include <thread>
#include <chrono>
2025-04-23 12:39:44 +08:00
#include "../core/exceptions.h"
#include "../io/term_io.h"
2024-09-10 15:45:07 +08:00
//进度条宏定义
#define TOTAL_PERCENTAGE 100.0 ///< 设置总的百分比为100%。
#define CHARACTER_WIDTH_PERCENTAGE 5 ///< 设置每显示一个字符所占的百分比为5%。
namespace gctl
{
2025-03-05 09:19:16 +08:00
enum bar_color_e
{
Green,
Red,
Blue,
Yellow,
};
2024-09-10 15:45:07 +08:00
/**
* @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_
2025-03-05 09:19:16 +08:00
* @param[in] c Green Red Yellow Blue
2024-09-10 15:45:07 +08:00
*/
2025-03-05 09:19:16 +08:00
void set_style(const char* unit_bar_, const char* unit_space_, bar_color_e c = Green);
2024-09-10 15:45:07 +08:00
/**
* @brief
*
* @param[in] idx_
*/
void progressed(unsigned long idx_);
/**
* @brief
*
* @param[in] idx_
*/
void progressed_simple(unsigned long idx_);
protected:
2025-03-05 09:19:16 +08:00
bar_color_e color;
2024-09-10 15:45:07 +08:00
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();
};
2025-03-05 09:19:16 +08:00
};
2024-09-10 15:45:07 +08:00
#endif // _GCTL_PROGRESS_BAR_H