Fixed interface: show download/upload progress

This commit is contained in:
vysheng 2013-10-29 02:33:13 +04:00
parent e858db0964
commit 3be908e7da
4 changed files with 56 additions and 2 deletions

View File

@ -40,10 +40,32 @@ char *default_prompt = "> ";
int unread_messages; int unread_messages;
int msg_num_mode; int msg_num_mode;
long long cur_uploading_bytes;
long long cur_uploaded_bytes;
long long cur_downloading_bytes;
long long cur_downloaded_bytes;
char *get_default_prompt (void) { char *get_default_prompt (void) {
static char buf[100]; static char buf[100];
if (unread_messages || cur_uploading_bytes || cur_downloading_bytes) {
int l = sprintf (buf, COLOR_RED "[");
int ok = 0;
if (unread_messages) { if (unread_messages) {
sprintf (buf, COLOR_RED "[%d unread]" COLOR_NORMAL "%s", unread_messages, default_prompt); l += sprintf (buf + l, "%d unread", unread_messages);
ok = 1;
}
if (cur_uploading_bytes) {
if (ok) { *(buf + l) = ' '; l ++; }
ok = 1;
l += sprintf (buf + l, "%lld%%Up", 100 * cur_uploaded_bytes / cur_uploading_bytes);
}
if (cur_downloading_bytes) {
if (ok) { *(buf + l) = ' '; l ++; }
ok = 1;
l += sprintf (buf + l, "%lld%%Down", 100 * cur_downloaded_bytes / cur_downloading_bytes);
}
sprintf (buf + l, "]" COLOR_NORMAL "%s", default_prompt);
return buf; return buf;
} else { } else {
return default_prompt; return default_prompt;
@ -54,6 +76,13 @@ char *complete_none (const char *text UU, int state UU) {
return 0; return 0;
} }
void update_prompt (void) {
print_start ();
rl_set_prompt (get_default_prompt ());
rl_redisplay ();
print_end ();
}
char *commands[] = { char *commands[] = {
"help", "help",
"msg", "msg",

View File

@ -50,4 +50,6 @@ void push_color (const char *color);
void print_start (void); void print_start (void);
void print_end (void); void print_end (void);
void print_date_full (long t); void print_date_full (long t);
void update_prompt (void);
#endif #endif

View File

@ -644,6 +644,7 @@ void work_update (struct connection *c UU, long long msg_id UU) {
fetch_int (); //pts fetch_int (); //pts
unread_messages ++; unread_messages ++;
print_message (M); print_message (M);
update_prompt ();
break; break;
}; };
case CODE_update_message_i_d: case CODE_update_message_i_d:
@ -939,6 +940,7 @@ void work_update_short_message (struct connection *c UU, long long msg_id UU) {
struct message *M = fetch_alloc_message_short (); struct message *M = fetch_alloc_message_short ();
unread_messages ++; unread_messages ++;
print_message (M); print_message (M);
update_prompt ();
} }
void work_update_short_chat_message (struct connection *c UU, long long msg_id UU) { void work_update_short_chat_message (struct connection *c UU, long long msg_id UU) {
@ -946,6 +948,7 @@ void work_update_short_chat_message (struct connection *c UU, long long msg_id U
struct message *M = fetch_alloc_message_short_chat (); struct message *M = fetch_alloc_message_short_chat ();
unread_messages ++; unread_messages ++;
print_message (M); print_message (M);
update_prompt ();
} }
void work_container (struct connection *c, long long msg_id UU) { void work_container (struct connection *c, long long msg_id UU) {

View File

@ -39,6 +39,11 @@
char *get_downloads_directory (void); char *get_downloads_directory (void);
int verbosity; int verbosity;
long long cur_uploading_bytes;
long long cur_uploaded_bytes;
long long cur_downloading_bytes;
long long cur_downloaded_bytes;
#define QUERY_TIMEOUT 0.3 #define QUERY_TIMEOUT 0.3
#define memcmp8(a,b) memcmp ((a), (b), 8) #define memcmp8(a,b) memcmp ((a), (b), 8)
@ -914,6 +919,9 @@ struct query_methods send_file_methods = {
void send_part (struct send_file *f) { void send_part (struct send_file *f) {
if (f->fd >= 0) { if (f->fd >= 0) {
if (!f->part_num) {
cur_uploading_bytes += f->size;
}
clear_packet (); clear_packet ();
out_int (CODE_upload_save_file_part); out_int (CODE_upload_save_file_part);
out_long (f->id); out_long (f->id);
@ -923,6 +931,7 @@ void send_part (struct send_file *f) {
assert (x > 0); assert (x > 0);
out_cstring (buf, x); out_cstring (buf, x);
f->offset += x; f->offset += x;
cur_uploaded_bytes += x;
if (verbosity >= 2) { if (verbosity >= 2) {
logprintf ("offset=%lld size=%lld\n", f->offset, f->size); logprintf ("offset=%lld size=%lld\n", f->offset, f->size);
} }
@ -932,6 +941,8 @@ void send_part (struct send_file *f) {
} }
send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_part_methods, f); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_part_methods, f);
} else { } else {
cur_uploaded_bytes -= f->size;
cur_uploading_bytes -= f->size;
clear_packet (); clear_packet ();
out_int (CODE_messages_send_media); out_int (CODE_messages_send_media);
out_peer_id (f->to_id); out_peer_id (f->to_id);
@ -1175,6 +1186,9 @@ struct download {
void end_load (struct download *D) { void end_load (struct download *D) {
cur_downloading_bytes -= D->size;
cur_downloaded_bytes -= D->size;
update_prompt ();
close (D->fd); close (D->fd);
if (D->next == 1) { if (D->next == 1) {
logprintf ("Done: %s\n", D->name); logprintf ("Done: %s\n", D->name);
@ -1234,6 +1248,8 @@ int download_on_answer (struct query *q) {
fetch_int (); // mtime fetch_int (); // mtime
int len = prefetch_strlen (); int len = prefetch_strlen ();
assert (len >= 0); assert (len >= 0);
cur_downloaded_bytes += len;
update_prompt ();
assert (write (D->fd, fetch_str (len), len) == len); assert (write (D->fd, fetch_str (len), len) == len);
D->offset += len; D->offset += len;
if (D->offset < D->size) { if (D->offset < D->size) {
@ -1250,6 +1266,10 @@ struct query_methods download_methods = {
}; };
void load_next_part (struct download *D) { void load_next_part (struct download *D) {
if (!D->offset) {
cur_downloading_bytes += D->size;
update_prompt ();
}
clear_packet (); clear_packet ();
out_int (CODE_upload_get_file); out_int (CODE_upload_get_file);
if (!D->id) { if (!D->id) {