File: percent_progress.cpp

package info (click to toggle)
kmc 3.2.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,716 kB
  • sloc: cpp: 38,308; python: 664; makefile: 216; perl: 179; sh: 34
file content (103 lines) | stat: -rw-r--r-- 3,624 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
  This file is a part of KMC software distributed under GNU GPL 3 licence.
  The homepage of the KMC project is http://sun.aei.polsl.pl/kmc
  
  Authors: Marek Kokot
  
  Version: 3.2.4
  Date   : 2024-02-09
*/

#include "percent_progress.h"
#include <iostream>
#include <string>
using namespace std;


/*****************************************************************************************************************************/
/********************************************************** PUBLIC ***********************************************************/
/*****************************************************************************************************************************/

/*****************************************************************************************************************************/
uint32 CPercentProgress::RegisterItem(const std::string& name, uint64 max_value)
{
	items.emplace_back(name, max_value);
	return static_cast<uint32>(items.size() - 1);
}

/*****************************************************************************************************************************/
uint32 CPercentProgress::RegisterItem(uint64 max_value)
{
	items.emplace_back("in" + std::to_string(items.size() + 1), max_value);
	display();
	return static_cast<uint32>(items.size() - 1);
}

/*****************************************************************************************************************************/
void CPercentProgress::UpdateItem(uint32 id)
{
	--items[id].to_next_update;
	if (!items[id].to_next_update)
	{
		items[id].to_next_update = items[id].to_next_update_pattern;
		UpdateItem(id, items[id].to_next_update_pattern);
	}
}

/*****************************************************************************************************************************/
void CPercentProgress::UpdateItem(uint32 id, uint32 offset)
{
	items[id].cur_val += offset;
	uint32 prev = items[id].cur_percent;
	if (items[id].max_val)
		items[id].cur_percent = static_cast<uint32>((items[id].cur_val * 100) / items[id].max_val);
	else
		items[id].cur_percent = 100;
	if (prev != items[id].cur_percent)
		display();
}

/*****************************************************************************************************************************/
void CPercentProgress::Complete(uint32 id)
{
	if (items[id].cur_percent != 100)
	{
		items[id].cur_percent = 100;
		display();		
	}
}

/*****************************************************************************************************************************/
/********************************************************** PRIVATE **********************************************************/
/*****************************************************************************************************************************/

/*****************************************************************************************************************************/
void CPercentProgress::display()
{
	if (hide_progress || ignore_rest)
		return;
	std::cerr << "\r";
	bool finished = true;
	for (auto& item : items)
	{
		std::cerr << item.name << ": " << item.cur_percent << "% ";
		if (item.cur_percent != 100)
			finished = false;
	}

	if (finished)
	{
		cerr << "\n";
		ignore_rest = true;
	}
	std::cerr.flush();
}

/*****************************************************************************************************************************/
CPercentProgress::CDisplayItem::CDisplayItem(const std::string name, uint64 max_val) : name(name), max_val(max_val)
{
	to_next_update_pattern = (uint32)MAX(1, max_val / 100);
	to_next_update = to_next_update_pattern;
}

// ***** EOF