diff --git a/interface.c b/interface.c index 8a40a59..d9f7eed 100644 --- a/interface.c +++ b/interface.c @@ -40,10 +40,32 @@ char *default_prompt = "> "; int unread_messages; 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) { static char buf[100]; - if (unread_messages) { - sprintf (buf, COLOR_RED "[%d unread]" COLOR_NORMAL "%s", unread_messages, default_prompt); + if (unread_messages || cur_uploading_bytes || cur_downloading_bytes) { + int l = sprintf (buf, COLOR_RED "["); + int ok = 0; + if (unread_messages) { + 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; } else { return default_prompt; @@ -54,6 +76,13 @@ char *complete_none (const char *text UU, int state UU) { return 0; } +void update_prompt (void) { + print_start (); + rl_set_prompt (get_default_prompt ()); + rl_redisplay (); + print_end (); +} + char *commands[] = { "help", "msg", diff --git a/interface.h b/interface.h index 4f14037..51463a8 100644 --- a/interface.h +++ b/interface.h @@ -50,4 +50,6 @@ void push_color (const char *color); void print_start (void); void print_end (void); void print_date_full (long t); + +void update_prompt (void); #endif diff --git a/mtproto-client.c b/mtproto-client.c index 2a877f6..0b5be0b 100644 --- a/mtproto-client.c +++ b/mtproto-client.c @@ -644,6 +644,7 @@ void work_update (struct connection *c UU, long long msg_id UU) { fetch_int (); //pts unread_messages ++; print_message (M); + update_prompt (); break; }; 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 (); unread_messages ++; print_message (M); + update_prompt (); } 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 (); unread_messages ++; print_message (M); + update_prompt (); } void work_container (struct connection *c, long long msg_id UU) { diff --git a/queries.c b/queries.c index 80c97be..f3c78cc 100644 --- a/queries.c +++ b/queries.c @@ -39,6 +39,11 @@ char *get_downloads_directory (void); 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 memcmp8(a,b) memcmp ((a), (b), 8) @@ -914,6 +919,9 @@ struct query_methods send_file_methods = { void send_part (struct send_file *f) { if (f->fd >= 0) { + if (!f->part_num) { + cur_uploading_bytes += f->size; + } clear_packet (); out_int (CODE_upload_save_file_part); out_long (f->id); @@ -923,6 +931,7 @@ void send_part (struct send_file *f) { assert (x > 0); out_cstring (buf, x); f->offset += x; + cur_uploaded_bytes += x; if (verbosity >= 2) { 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); } else { + cur_uploaded_bytes -= f->size; + cur_uploading_bytes -= f->size; clear_packet (); out_int (CODE_messages_send_media); out_peer_id (f->to_id); @@ -1175,6 +1186,9 @@ struct download { void end_load (struct download *D) { + cur_downloading_bytes -= D->size; + cur_downloaded_bytes -= D->size; + update_prompt (); close (D->fd); if (D->next == 1) { logprintf ("Done: %s\n", D->name); @@ -1234,6 +1248,8 @@ int download_on_answer (struct query *q) { fetch_int (); // mtime int len = prefetch_strlen (); assert (len >= 0); + cur_downloaded_bytes += len; + update_prompt (); assert (write (D->fd, fetch_str (len), len) == len); D->offset += len; if (D->offset < D->size) { @@ -1250,6 +1266,10 @@ struct query_methods download_methods = { }; void load_next_part (struct download *D) { + if (!D->offset) { + cur_downloading_bytes += D->size; + update_prompt (); + } clear_packet (); out_int (CODE_upload_get_file); if (!D->id) {