File indexing completed on 2024-03-24 17:00:28
0001 /* fork. doesn't include os_string and draws all blocks white. 0002 * 0003 * SPDX-FileCopyrightText: 2008 Red Hat Inc. 0004 * SPDX-FileContributor: Adam Jackson <ajax@redhat.com> 0005 * SPDX-FileContributor: Bill Nottingham <notting@redhat.com> 0006 * SPDX-FileContributor: Ray Strode <rstrode@redhat.com> 0007 * SPDX-FileContributor: Soeren Sandmann <sandmann@redhat.com> 0008 * 0009 * SPDX-License-Identifier: GPL-2.0-or-later 0010 */ 0011 0012 #include "ply-text-progress-bar.h" 0013 0014 #include <assert.h> 0015 #include <dirent.h> 0016 #include <errno.h> 0017 #include <fcntl.h> 0018 #include <math.h> 0019 #include <signal.h> 0020 #include <stdbool.h> 0021 #include <stdint.h> 0022 #include <stdio.h> 0023 #include <stdlib.h> 0024 #include <string.h> 0025 #include <sys/ioctl.h> 0026 #include <sys/stat.h> 0027 #include <sys/time.h> 0028 #include <sys/types.h> 0029 #include <unistd.h> 0030 #include <values.h> 0031 #include <wchar.h> 0032 0033 #include <ply-array.h> 0034 #include <ply-logger.h> 0035 #include <ply-text-display.h> 0036 #include <ply-utils.h> 0037 0038 #include <linux/kd.h> 0039 0040 #ifndef FRAMES_PER_SECOND 0041 #define FRAMES_PER_SECOND 5 0042 #endif 0043 0044 #define NUMBER_OF_INDICATOR_COLUMNS 6 0045 0046 static char *os_string; 0047 0048 struct _breeze_text_progress_bar { 0049 ply_text_display_t *display; 0050 0051 int column, row; 0052 int number_of_rows; 0053 int number_of_columns; 0054 0055 double percent_done; 0056 uint32_t is_hidden : 1; 0057 }; 0058 0059 breeze_text_progress_bar_t *breeze_text_progress_bar_new(void) 0060 { 0061 breeze_text_progress_bar_t *progress_bar; 0062 0063 progress_bar = calloc(1, sizeof(breeze_text_progress_bar_t)); 0064 0065 progress_bar->row = 0; 0066 progress_bar->column = 0; 0067 progress_bar->number_of_columns = 0; 0068 progress_bar->number_of_rows = 0; 0069 0070 return progress_bar; 0071 } 0072 0073 void breeze_text_progress_bar_free(breeze_text_progress_bar_t *progress_bar) 0074 { 0075 if (progress_bar == NULL) 0076 return; 0077 0078 free(progress_bar); 0079 } 0080 0081 static void get_os_string(void) 0082 { 0083 os_string = ""; 0084 } 0085 0086 void breeze_text_progress_bar_draw(breeze_text_progress_bar_t *progress_bar) 0087 { 0088 int i, width; 0089 double brown_fraction, blue_fraction, white_fraction; 0090 0091 if (progress_bar->is_hidden) 0092 return; 0093 0094 width = progress_bar->number_of_columns - 2 - strlen(os_string); 0095 0096 ply_text_display_set_cursor_position(progress_bar->display, progress_bar->column, progress_bar->row); 0097 0098 brown_fraction = -(progress_bar->percent_done * progress_bar->percent_done) + 2 * progress_bar->percent_done; 0099 blue_fraction = progress_bar->percent_done; 0100 white_fraction = progress_bar->percent_done * progress_bar->percent_done; 0101 0102 for (i = 0; i < width; i++) { 0103 double f; 0104 0105 f = (double)i / (double)width; 0106 if (f < white_fraction) 0107 ply_text_display_set_background_color(progress_bar->display, PLY_TERMINAL_COLOR_WHITE); 0108 else if (f < blue_fraction) 0109 ply_text_display_set_background_color(progress_bar->display, PLY_TERMINAL_COLOR_WHITE); 0110 else if (f < brown_fraction) 0111 ply_text_display_set_background_color(progress_bar->display, PLY_TERMINAL_COLOR_WHITE); 0112 else 0113 break; 0114 0115 ply_text_display_write(progress_bar->display, "%c", ' '); 0116 } 0117 0118 ply_text_display_set_background_color(progress_bar->display, PLY_TERMINAL_COLOR_BLACK); 0119 0120 if (brown_fraction > 0.5) { 0121 if (white_fraction > 0.875) 0122 ply_text_display_set_foreground_color(progress_bar->display, PLY_TERMINAL_COLOR_WHITE); 0123 else if (blue_fraction > 0.66) 0124 ply_text_display_set_foreground_color(progress_bar->display, PLY_TERMINAL_COLOR_WHITE); 0125 else 0126 ply_text_display_set_foreground_color(progress_bar->display, PLY_TERMINAL_COLOR_WHITE); 0127 0128 ply_text_display_set_cursor_position(progress_bar->display, progress_bar->column + width, progress_bar->row); 0129 0130 ply_text_display_write(progress_bar->display, "%s", os_string); 0131 0132 ply_text_display_set_foreground_color(progress_bar->display, PLY_TERMINAL_COLOR_DEFAULT); 0133 } 0134 } 0135 0136 void breeze_text_progress_bar_show(breeze_text_progress_bar_t *progress_bar, ply_text_display_t *display) 0137 { 0138 assert(progress_bar != NULL); 0139 0140 progress_bar->display = display; 0141 0142 progress_bar->number_of_rows = ply_text_display_get_number_of_rows(display); 0143 progress_bar->row = progress_bar->number_of_rows - 1; 0144 progress_bar->number_of_columns = ply_text_display_get_number_of_columns(display); 0145 progress_bar->column = 2; 0146 0147 get_os_string(); 0148 0149 progress_bar->is_hidden = false; 0150 0151 breeze_text_progress_bar_draw(progress_bar); 0152 } 0153 0154 void breeze_text_progress_bar_hide(breeze_text_progress_bar_t *progress_bar) 0155 { 0156 progress_bar->display = NULL; 0157 progress_bar->is_hidden = true; 0158 } 0159 0160 void breeze_text_progress_bar_set_percent_done(breeze_text_progress_bar_t *progress_bar, double percent_done) 0161 { 0162 progress_bar->percent_done = percent_done; 0163 } 0164 0165 double breeze_text_progress_bar_get_percent_done(breeze_text_progress_bar_t *progress_bar) 0166 { 0167 return progress_bar->percent_done; 0168 } 0169 0170 int breeze_text_progress_bar_get_number_of_columns(breeze_text_progress_bar_t *progress_bar) 0171 { 0172 return progress_bar->number_of_columns; 0173 } 0174 0175 int breeze_text_progress_bar_get_number_of_rows(breeze_text_progress_bar_t *progress_bar) 0176 { 0177 return progress_bar->number_of_rows; 0178 } 0179 0180 /* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */