/src/neomutt/progress/progress.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * @file |
3 | | * Progress bar |
4 | | * |
5 | | * @authors |
6 | | * Copyright (C) 2018-2022 Richard Russon <rich@flatcap.org> |
7 | | * Copyright (C) 2019 Pietro Cerutti <gahr@gahr.ch> |
8 | | * |
9 | | * @copyright |
10 | | * This program is free software: you can redistribute it and/or modify it under |
11 | | * the terms of the GNU General Public License as published by the Free Software |
12 | | * Foundation, either version 2 of the License, or (at your option) any later |
13 | | * version. |
14 | | * |
15 | | * This program is distributed in the hope that it will be useful, but WITHOUT |
16 | | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
17 | | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
18 | | * details. |
19 | | * |
20 | | * You should have received a copy of the GNU General Public License along with |
21 | | * this program. If not, see <http://www.gnu.org/licenses/>. |
22 | | */ |
23 | | |
24 | | /** |
25 | | * @page progress_progress Progress Bar |
26 | | * |
27 | | * This is a wrapper around the Progress Bar Window. |
28 | | * After creating the window, it pushes it into the Message Window Container. |
29 | | */ |
30 | | |
31 | | #include "config.h" |
32 | | #include <stdbool.h> |
33 | | #include <stdio.h> |
34 | | #include "mutt/lib.h" |
35 | | #include "config/lib.h" |
36 | | #include "core/lib.h" |
37 | | #include "gui/lib.h" |
38 | | #include "lib.h" |
39 | | #include "globals.h" |
40 | | #include "mutt_logging.h" |
41 | | #include "window.h" |
42 | | |
43 | | struct Progress; |
44 | | |
45 | | /** |
46 | | * choose_increment - Choose the right increment given a ProgressType |
47 | | * @param type ProgressType |
48 | | * @retval num Increment value |
49 | | */ |
50 | | static size_t choose_increment(enum ProgressType type) |
51 | 0 | { |
52 | 0 | const short c_read_inc = cs_subset_number(NeoMutt->sub, "read_inc"); |
53 | 0 | const short c_write_inc = cs_subset_number(NeoMutt->sub, "write_inc"); |
54 | 0 | const short c_net_inc = cs_subset_number(NeoMutt->sub, "net_inc"); |
55 | 0 | const short *incs[] = { &c_read_inc, &c_write_inc, &c_net_inc }; |
56 | 0 | return (type >= mutt_array_size(incs)) ? 0 : *incs[type]; |
57 | 0 | } |
58 | | |
59 | | /** |
60 | | * progress_update - Update the state of the progress bar |
61 | | * @param progress Progress bar |
62 | | * @param pos Position, or count |
63 | | * @param percent Percentage complete |
64 | | * @retval true Screen update is needed |
65 | | * |
66 | | * If percent is -1, then the percentage will be calculated using pos and the |
67 | | * size in progress. |
68 | | * |
69 | | * If percent is positive, it is displayed as percentage, otherwise |
70 | | * percentage is calculated from size and pos if progress |
71 | | * was initialized with positive size, otherwise no percentage is shown |
72 | | */ |
73 | | bool progress_update(struct Progress *progress, size_t pos, int percent) |
74 | 0 | { |
75 | | // Decloak an opaque pointer |
76 | 0 | struct MuttWindow *win = (struct MuttWindow *) progress; |
77 | 0 | const bool updated = progress_window_update(win, pos, percent); |
78 | 0 | if (updated) |
79 | 0 | { |
80 | 0 | window_redraw(win); |
81 | 0 | } |
82 | 0 | return updated; |
83 | 0 | } |
84 | | |
85 | | /** |
86 | | * progress_free - Free a Progress Bar |
87 | | * @param ptr Progress Bar to free |
88 | | */ |
89 | | void progress_free(struct Progress **ptr) |
90 | 0 | { |
91 | 0 | if (!ptr) |
92 | 0 | return; |
93 | | |
94 | 0 | if (!*ptr) |
95 | 0 | { |
96 | 0 | mutt_clear_error(); |
97 | 0 | return; |
98 | 0 | } |
99 | | |
100 | | // Decloak an opaque pointer |
101 | 0 | struct MuttWindow **wptr = (struct MuttWindow **) ptr; |
102 | 0 | struct MuttWindow *win_pop = msgcont_pop_window(); |
103 | 0 | if (win_pop != *wptr) |
104 | 0 | return; |
105 | | |
106 | 0 | mutt_window_free(wptr); |
107 | 0 | } |
108 | | |
109 | | /** |
110 | | * progress_new - Create a new Progress Bar |
111 | | * @param msg Message to display |
112 | | * @param type Type, e.g. #MUTT_PROGRESS_READ |
113 | | * @param size Total size of expected file / traffic |
114 | | * @retval ptr New Progress Bar |
115 | | * |
116 | | * If the user has disabled the progress bar, e.g. `set read_inc = 0` then a |
117 | | * simple message will be displayed instead. |
118 | | * |
119 | | * @note msg will be copied |
120 | | */ |
121 | | struct Progress *progress_new(const char *msg, enum ProgressType type, size_t size) |
122 | 0 | { |
123 | 0 | if (OptNoCurses) |
124 | 0 | return NULL; |
125 | | |
126 | 0 | const size_t size_inc = choose_increment(type); |
127 | 0 | if (size_inc == 0) // The user has disabled the progress bar |
128 | 0 | { |
129 | 0 | mutt_message(msg); |
130 | 0 | return NULL; |
131 | 0 | } |
132 | | |
133 | 0 | const short c_time_inc = cs_subset_number(NeoMutt->sub, "time_inc"); |
134 | 0 | const bool is_bytes = (type == MUTT_PROGRESS_NET); |
135 | |
|
136 | 0 | struct MuttWindow *win = progress_window_new(msg, size, size_inc, c_time_inc, is_bytes); |
137 | |
|
138 | 0 | msgcont_push_window(win); |
139 | | |
140 | | // Return an opaque pointer |
141 | 0 | return (struct Progress *) win; |
142 | 0 | } |