CommandScreen.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. htop - CommandScreen.c
  3. (C) 2017,2020 ryenus
  4. (C) 2020,2021 htop dev team
  5. Released under the GNU GPLv2+, see the COPYING file
  6. in the source distribution for its full text.
  7. */
  8. #include "config.h" // IWYU pragma: keep
  9. #include "CommandScreen.h"
  10. #include <assert.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "Macros.h"
  14. #include "Panel.h"
  15. #include "ProvideCurses.h"
  16. static void CommandScreen_scan(InfoScreen* this) {
  17. Panel* panel = this->display;
  18. int idx = MAXIMUM(Panel_getSelectedIndex(panel), 0);
  19. Panel_prune(panel);
  20. const char* p = Process_getCommand(this->process);
  21. char line[COLS + 1];
  22. int line_offset = 0, last_spc = -1, len;
  23. for (; *p != '\0'; p++, line_offset++) {
  24. assert(line_offset >= 0 && (size_t)line_offset < sizeof(line));
  25. line[line_offset] = *p;
  26. if (*p == ' ') {
  27. last_spc = line_offset;
  28. }
  29. if (line_offset == COLS) {
  30. len = (last_spc == -1) ? line_offset : last_spc;
  31. line[len] = '\0';
  32. InfoScreen_addLine(this, line);
  33. line_offset -= len;
  34. last_spc = -1;
  35. memcpy(line, p - line_offset, line_offset + 1);
  36. }
  37. }
  38. if (line_offset > 0) {
  39. line[line_offset] = '\0';
  40. InfoScreen_addLine(this, line);
  41. }
  42. Panel_setSelected(panel, idx);
  43. }
  44. static void CommandScreen_draw(InfoScreen* this) {
  45. InfoScreen_drawTitled(this, "Command of process %d - %s", Process_getPid(this->process), Process_getCommand(this->process));
  46. }
  47. const InfoScreenClass CommandScreen_class = {
  48. .super = {
  49. .extends = Class(Object),
  50. .delete = CommandScreen_delete
  51. },
  52. .scan = CommandScreen_scan,
  53. .draw = CommandScreen_draw
  54. };
  55. CommandScreen* CommandScreen_new(Process* process) {
  56. CommandScreen* this = AllocThis(CommandScreen);
  57. return (CommandScreen*) InfoScreen_init(&this->super, process, NULL, LINES - 2, " ");
  58. }
  59. void CommandScreen_delete(Object* this) {
  60. free(InfoScreen_done((InfoScreen*)this));
  61. }