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();
70 void lpbar_free(lpbar_t p
)
75 if (p
->content_bytes
>0
76 && (p
->counters
.count
+ p
->initial_bytes
>p
->content_bytes
))
78 p
->content_bytes
= p
->initial_bytes
+ p
->counters
.count
;
81 if (p
->flags
.failed
== FALSE
)
87 g_timer_destroy(p
->counters
.timer
);
88 g_free(p
->content_type
);
92 memset(p
, 0, sizeof(struct lpbar_s
));
95 static const gchar
*frames
[] =
104 static const gchar
*_next_frame(lpbar_t p
)
106 if (frames
[p
->counters
.curr_frame
] == NULL
)
107 p
->counters
.curr_frame
= 0;
108 return (frames
[p
->counters
.curr_frame
++]);
111 static gchar
*_eta(const glong s
)
113 if (s
>=86400) /* 24h */
114 return (g_strdup_printf(_("%ld hours"), (s
/3600%60)));
115 return (g_strdup_printf("%02ld:%02ld:%02ld", (s
/3600)%60, (s
/60)%60, s
%60));
118 static const gdouble update_interval
= .5;
120 gint
lpbar_update(lpbar_t p
, gdouble dlnow
)
122 gdouble rate
, size
, elapsed
, percent
;
123 const gchar
*rate_unit
, *frame
;
127 if (dlnow
== 0 || p
->flags
.failed
== TRUE
)
130 elapsed
= g_timer_elapsed(p
->counters
.timer
, NULL
);
132 if (p
->flags
.done
== TRUE
)
133 dlnow
= p
->content_bytes
;
136 if ((elapsed
- p
->counters
.last_update
) < update_interval
)
141 if (p
->flags
.done
== FALSE
)
142 size
+= p
->initial_bytes
;
144 inactive
= (dlnow
== 0) ? TRUE
:FALSE
;
145 rate
= (elapsed
>0) ? (dlnow
/elapsed
):0;
147 if (inactive
== FALSE
)
149 if (p
->flags
.done
== FALSE
)
152 (p
->content_bytes
- (dlnow
+ p
->initial_bytes
)) / rate
;
158 rate
= (p
->content_bytes
- p
->initial_bytes
) / elapsed
;
161 rate_unit
= _to_unit(&rate
);
162 frame
= _next_frame(p
);
166 frame
= frames
[p
->counters
.curr_frame
];
167 eta
= g_strdup("--:--");
172 if (p
->content_bytes
>0)
174 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":"");
184 p
->counters
.last_update
= elapsed
;
185 p
->counters
.count
= dlnow
;
191 void lpbar_print(const lpbar_t p
, const gboolean skip_transfer
)
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 if (skip_transfer
== FALSE
)
207 g_print(_(" mode: %s"),
208 (p
->initial_bytes
>0) ? _("resume"):_("write"));
213 /* vim: set ts=2 sw=2 tw=72 expandtab: */