2 * Copyright (C) 2012,2013 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of quvi <http://quvi.sourceforge.net/>.
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
23 #include <glib/gi18n.h>
36 static const gchar
*_units
[] =
45 static const gchar
*_to_unit(gdouble
*n
)
65 lpbar_t p
= g_new0(struct lpbar_s
, 1);
66 p
->counters
.timer
= g_timer_new();
71 void lpbar_free(lpbar_t p
)
76 if (p
->content_bytes
>0
77 && (p
->counters
.count
+ p
->initial_bytes
>p
->content_bytes
))
79 p
->content_bytes
= p
->initial_bytes
+ p
->counters
.count
;
82 if (p
->flags
.failed
== FALSE
)
88 g_timer_destroy(p
->counters
.timer
);
89 g_free(p
->content_type
);
93 memset(p
, 0, sizeof(struct lpbar_s
));
96 static const gchar
*frames
[] =
105 static const gchar
*_next_frame(lpbar_t p
)
107 if (frames
[p
->counters
.curr_frame
] == NULL
)
108 p
->counters
.curr_frame
= 0;
109 return (frames
[p
->counters
.curr_frame
++]);
112 static gchar
*_eta(const glong s
)
114 if (s
>=86400) /* 24h */
115 return (g_strdup_printf(_("%ld hours"), (s
/3600%60)));
116 return (g_strdup_printf("%02ld:%02ld:%02ld", (s
/3600)%60, (s
/60)%60, s
%60));
119 static const gdouble update_interval
= .5;
121 gint
lpbar_update(lpbar_t p
, gdouble dlnow
)
123 gdouble rate
, size
, elapsed
, percent
;
124 const gchar
*rate_unit
, *frame
;
128 if (dlnow
== 0 || p
->flags
.failed
== TRUE
)
131 elapsed
= g_timer_elapsed(p
->counters
.timer
, NULL
);
133 if (p
->flags
.done
== TRUE
)
134 dlnow
= p
->content_bytes
;
137 if ((elapsed
- p
->counters
.last_update
) < update_interval
)
142 if (p
->flags
.done
== FALSE
)
143 size
+= p
->initial_bytes
;
145 inactive
= (dlnow
== 0) ? TRUE
:FALSE
;
146 rate
= (elapsed
>0) ? (dlnow
/elapsed
):0;
148 if (inactive
== FALSE
)
150 if (p
->flags
.done
== FALSE
)
153 (p
->content_bytes
- (dlnow
+ p
->initial_bytes
)) / rate
;
159 rate
= (p
->content_bytes
- p
->initial_bytes
) / elapsed
;
162 rate_unit
= _to_unit(&rate
);
163 frame
= _next_frame(p
);
167 frame
= frames
[p
->counters
.curr_frame
];
168 eta
= g_strdup("--:--");
173 if (p
->content_bytes
>0)
175 percent
= (100.0 * size
/ p
->content_bytes
);
180 g_print(_("copy: %s %3.0f%% %6.1f%s/s %4s%s"),
181 frame
, percent
, rate
, rate_unit
, eta
,
182 (p
->flags
.done
== TRUE
) ? "\n":"\r");
184 p
->counters
.last_update
= elapsed
;
185 p
->counters
.count
= dlnow
;
191 void lpbar_print(const lpbar_t p
)
196 b
= p
->content_bytes
;
199 g_print(_("file: %s [media]\n"), p
->fname
);
200 g_print(_(" content length: %.1f%s"), b
, u
);
202 if (p
->content_type
!= NULL
)
203 g_print(_(" content type: %s"), p
->content_type
);
205 g_print(C_("To indicate transfer mode (resumed, ...) ", " mode: "));
208 case retrieved_already
:
209 g_print(C_("Transfer mode with a reason", "skip <retrieved already>"));
212 g_print(C_("Transfer mode with a reason", "skip <forced>"));
217 g_print("%s", (p
->initial_bytes
==0)
218 ? C_("Transfer mode (begin at offset 0)", "write")
219 : C_("Transfer mode", "resume"));
225 /* vim: set ts=2 sw=2 tw=72 expandtab: */