sysinfo.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2015-2018 K. Lange
  5. *
  6. * sysinfo - visually based on screenfetch
  7. *
  8. * Displays system information in a visually-pleasing format.
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/utsname.h>
  15. #include <toaru/graphics.h>
  16. #include <toaru/termemu.h>
  17. #include "toaru_logo.h"
  18. #define NUM_DATA_LINES 30
  19. char data_lines[NUM_DATA_LINES][100];
  20. const char * prog_lines[NUM_DATA_LINES] = {NULL};
  21. #define C_A "\033[34;1m"
  22. #define C_O "\033[0m"
  23. void print_thing(int j) {
  24. printf("\033[0m %s", data_lines[j]);
  25. fflush(stdout);
  26. if (prog_lines[j]) {
  27. system(prog_lines[j]);
  28. } else {
  29. printf("\n");
  30. }
  31. }
  32. int main(int argc, char * argv[]) {
  33. /* Prepare data */
  34. char * user = getenv("USER");
  35. char * wm_theme = getenv("WM_THEME");
  36. char * term = getenv("TERM");
  37. int term_is_toaru = term && strstr(term,"toaru");
  38. int i = 0;
  39. prog_lines[i] = "hostname";
  40. sprintf(data_lines[i++], C_A "%s" C_O "@" C_A, user);
  41. /* no command */
  42. sprintf(data_lines[i++], C_A "OS: " C_O "ToaruOS");
  43. prog_lines[i] = "uname -sr";
  44. sprintf(data_lines[i++], C_A "Kernel: " C_O);
  45. prog_lines[i] = "uptime -p";
  46. sprintf(data_lines[i++], C_A "Uptime: " C_O);
  47. prog_lines[i] = "msk count";
  48. sprintf(data_lines[i++], C_A "Packages: " C_O);
  49. prog_lines[i] = "sh -v";
  50. sprintf(data_lines[i++], C_A "Shell: " C_O);
  51. prog_lines[i] = "yutani-query resolution";
  52. sprintf(data_lines[i++], C_A "Resolution: " C_O);
  53. /* no command */
  54. sprintf(data_lines[i++], C_A "WM: " C_O "Yutani");
  55. /* from environment */
  56. sprintf(data_lines[i++], C_A "WM Theme: " C_O "%s", wm_theme);
  57. prog_lines[i] = "free -ut";
  58. sprintf(data_lines[i++], C_A "RAM: " C_O);
  59. int j = 0;
  60. for (unsigned int y = 0; y < gimp_image.height; y += 2) {
  61. for (unsigned int x = 0; x < gimp_image.width; x += 1) {
  62. unsigned char r_t = gimp_image.pixel_data[(x + y * gimp_image.width) * 4];
  63. unsigned char g_t = gimp_image.pixel_data[(x + y * gimp_image.width) * 4 + 1];
  64. unsigned char b_t = gimp_image.pixel_data[(x + y * gimp_image.width) * 4 + 2];
  65. unsigned char a_t = gimp_image.pixel_data[(x + y * gimp_image.width) * 4 + 3];
  66. unsigned char r_b = 0;
  67. unsigned char g_b = 0;
  68. unsigned char b_b = 0;
  69. unsigned char a_b = 0;
  70. if (y != gimp_image.height - 1) {
  71. r_b = gimp_image.pixel_data[(x + (y + 1) * gimp_image.width) * 4];
  72. g_b = gimp_image.pixel_data[(x + (y + 1) * gimp_image.width) * 4 + 1];
  73. b_b = gimp_image.pixel_data[(x + (y + 1) * gimp_image.width) * 4 + 2];
  74. a_b = gimp_image.pixel_data[(x + (y + 1) * gimp_image.width) * 4 + 3];
  75. }
  76. uint32_t out = alpha_blend_rgba(
  77. rgba(0,0,0,TERM_DEFAULT_OPAC),
  78. premultiply(rgba(r_t, g_t, b_t, a_t)));
  79. uint32_t back = alpha_blend_rgba(
  80. rgba(0,0,0,TERM_DEFAULT_OPAC),
  81. premultiply(rgba(r_b, g_b, b_b, a_b)));
  82. if (term_is_toaru) {
  83. /* Print half block */
  84. printf("\033[38;6;%d;%d;%d;%dm\033[48;6;%d;%d;%d;%dm▄",
  85. (int)_RED(back), (int)_GRE(back), (int)_BLU(back), (int)_ALP(back),
  86. (int)_RED(out), (int)_GRE(out), (int)_BLU(out), (int)_ALP(out));
  87. } else {
  88. printf("\033[38;2;%d;%d;%dm\033[48;2;%d;%d;%dm▄",
  89. (int)_RED(back), (int)_GRE(back), (int)_BLU(back),
  90. (int)_RED(out), (int)_GRE(out), (int)_BLU(out));
  91. }
  92. }
  93. if (j < i) {
  94. print_thing(j);
  95. j++;
  96. } else {
  97. printf("\033[0m\n");
  98. }
  99. }
  100. while (j < i) {
  101. for (int x = 0; x < (int)gimp_image.width; x++) {
  102. printf(" ");
  103. }
  104. print_thing(j);
  105. j++;
  106. }
  107. }