Refactor AppMode to InputMode

Author Mitja Felicijan <mitja.felicijan@gmail.com> 2026-01-16 17:19:44 +0100
Committer Mitja Felicijan <mitja.felicijan@gmail.com> 2026-01-16 17:19:44 +0100
Commit bcadcc72746ff1ad2dd5d0fac2c8a5579ab53ecd (patch)
-rw-r--r-- tdbg.cpp 42
1 files changed, 21 insertions, 21 deletions
diff --git a/tdbg.cpp b/tdbg.cpp
...
26
const uint32_t SCROLLBAR_THUMB = 0x2593; // Dark shade
26
const uint32_t SCROLLBAR_THUMB = 0x2593; // Dark shade
27
const uint32_t SCROLLBAR_LINE = 0x2502;  // Vertical line
27
const uint32_t SCROLLBAR_LINE = 0x2502;  // Vertical line
28
  
28
  
  
29
enum InputMode {
  
30
	INPUT_MODE_NORMAL,
  
31
	INPUT_MODE_BREAKPOINT,
  
32
	INPUT_MODE_VARIABLE
  
33
};
  
34
  
29
struct LLDBGuard {
35
struct LLDBGuard {
30
	LLDBGuard() { SBDebugger::Initialize(); }
36
	LLDBGuard() { SBDebugger::Initialize(); }
31
	~LLDBGuard() { SBDebugger::Terminate(); }
37
	~LLDBGuard() { SBDebugger::Terminate(); }
...
52
		}
58
		}
53
		return lines;
59
		return lines;
54
	}
60
	}
55
};
  
56
  
  
57
enum AppMode {
  
58
	MODE_NORMAL,
  
59
	MODE_INPUT_BREAKPOINT,
  
60
	MODE_INPUT_VARIABLE
  
61
};
61
};
62
  
62
  
63
struct VarLine {
63
struct VarLine {
...
472
	return target.BreakpointCreateByName(input.c_str());
472
	return target.BreakpointCreateByName(input.c_str());
473
}
473
}
474
  
474
  
475
void draw_log_view(int x, int y, int w, int h, const std::vector<std::string>& log_buffer, AppMode mode, const std::string& input_buffer, int scroll_offset) {
475
void draw_log_view(int x, int y, int w, int h, const std::vector<std::string>& log_buffer, InputMode mode, const std::string& input_buffer, int scroll_offset) {
476
	bool input_mode = (mode == MODE_INPUT_BREAKPOINT || mode == MODE_INPUT_VARIABLE);
476
	bool input_mode = (mode == INPUT_MODE_BREAKPOINT || mode == INPUT_MODE_VARIABLE);
477
	std::string title = input_mode ? "Input (Esc to Cancel)" : "Logs";
477
	std::string title = input_mode ? "Input (Esc to Cancel)" : "Logs";
478
	if (!input_mode && scroll_offset > 0) {
478
	if (!input_mode && scroll_offset > 0) {
479
		title += " (Scrolled up: " + std::to_string(scroll_offset) + ")";
479
		title += " (Scrolled up: " + std::to_string(scroll_offset) + ")";
...
487
  
487
  
488
	if (input_mode) {
488
	if (input_mode) {
489
		std::string prompt;
489
		std::string prompt;
490
		if (mode == MODE_INPUT_BREAKPOINT) prompt = "Add Breakpoint: ";
490
		if (mode == INPUT_MODE_BREAKPOINT) prompt = "Add Breakpoint: ";
491
		else if (mode == MODE_INPUT_VARIABLE) prompt = "Print Variable: ";
491
		else if (mode == INPUT_MODE_VARIABLE) prompt = "Print Variable: ";
492
  
492
  
493
		prompt += input_buffer;
493
		prompt += input_buffer;
494
		if ((int)prompt.length() > cw) prompt = prompt.substr(prompt.length() - cw);
494
		if ((int)prompt.length() > cw) prompt = prompt.substr(prompt.length() - cw);
...
528
	}
528
	}
529
}
529
}
530
  
530
  
531
void draw_status_bar(SBProcess &process, AppMode mode, int width, int height) {
531
void draw_status_bar(SBProcess &process, InputMode mode, int width, int height) {
532
	std::string state_str = "Status: ";
532
	std::string state_str = "Status: ";
533
	if (!process.IsValid()) {
533
	if (!process.IsValid()) {
534
		state_str += "Not Running";
534
		state_str += "Not Running";
...
540
		else state_str += "Unknown";
540
		else state_str += "Unknown";
541
	}
541
	}
542
  
542
  
543
	state_str += (mode == MODE_NORMAL)
543
	state_str += (mode == INPUT_MODE_NORMAL)
544
		? " | r=Run, b=Add breakpoint, p=Print, n=Step Over, s=Step Into, o=Step Out, c=Continue, q=Quit"
544
		? " | r=Run, b=Add breakpoint, p=Print, n=Step Over, s=Step Into, o=Step Out, c=Continue, q=Quit"
545
		: " | Enter=Confirm, Esc=Cancel";
545
		: " | Enter=Confirm, Esc=Cancel";
546
  
546
  
...
581
	TermboxGuard tb_guard;
581
	TermboxGuard tb_guard;
582
  
582
  
583
	bool running = true;
583
	bool running = true;
584
	AppMode mode = MODE_NORMAL;
584
	InputMode mode = INPUT_MODE_NORMAL;
585
	std::string input_buffer;
585
	std::string input_buffer;
586
	std::vector<std::string> log_buffer;
586
	std::vector<std::string> log_buffer;
587
	int log_scroll_offset = 0;
587
	int log_scroll_offset = 0;
...
643
		struct tb_event ev;
643
		struct tb_event ev;
644
		if (tb_poll_event(&ev) == 0) {
644
		if (tb_poll_event(&ev) == 0) {
645
			if (ev.type == TB_EVENT_KEY) {
645
			if (ev.type == TB_EVENT_KEY) {
646
				if (mode == MODE_NORMAL) {
646
				if (mode == INPUT_MODE_NORMAL) {
647
					if (ev.ch == 'q') {
647
					if (ev.ch == 'q') {
648
						running = false;
648
						running = false;
649
					} else if (ev.ch == 'r') {
649
					} else if (ev.ch == 'r') {
...
664
							log_msg(log_buffer, "Already running");
664
							log_msg(log_buffer, "Already running");
665
						}
665
						}
666
					} else if (ev.ch == 'b') {
666
					} else if (ev.ch == 'b') {
667
						mode = MODE_INPUT_BREAKPOINT;
667
						mode = INPUT_MODE_BREAKPOINT;
668
						input_buffer.clear();
668
						input_buffer.clear();
669
					} else if (ev.ch == 'p') {
669
					} else if (ev.ch == 'p') {
670
						mode = MODE_INPUT_VARIABLE;
670
						mode = INPUT_MODE_VARIABLE;
671
						input_buffer.clear();
671
						input_buffer.clear();
672
					} else {
672
					} else {
673
						if (process.IsValid() && process.GetState() == eStateStopped) {
673
						if (process.IsValid() && process.GetState() == eStateStopped) {
...
679
							}
679
							}
680
						}
680
						}
681
					}
681
					}
682
				} else if (mode == MODE_INPUT_BREAKPOINT || mode == MODE_INPUT_VARIABLE) {
682
				} else if (mode == INPUT_MODE_BREAKPOINT || mode == INPUT_MODE_VARIABLE) {
683
					if (ev.key == TB_KEY_ESC) {
683
					if (ev.key == TB_KEY_ESC) {
684
						mode = MODE_NORMAL;
684
						mode = INPUT_MODE_NORMAL;
685
						input_buffer.clear();
685
						input_buffer.clear();
686
					} else if (ev.key == TB_KEY_ENTER) {
686
					} else if (ev.key == TB_KEY_ENTER) {
687
						if (!input_buffer.empty()) {
687
						if (!input_buffer.empty()) {
688
							if (mode == MODE_INPUT_BREAKPOINT) {
688
							if (mode == INPUT_MODE_BREAKPOINT) {
689
								SBBreakpoint bp = create_breakpoint(target, input_buffer);
689
								SBBreakpoint bp = create_breakpoint(target, input_buffer);
690
								if (bp.IsValid() && bp.GetNumLocations() > 0) {
690
								if (bp.IsValid() && bp.GetNumLocations() > 0) {
691
									log_msg(log_buffer, "Breakpoint added: " + input_buffer);
691
									log_msg(log_buffer, "Breakpoint added: " + input_buffer);
692
								} else {
692
								} else {
693
									log_msg(log_buffer, "Failed/Invalid breakpoint: " + input_buffer);
693
									log_msg(log_buffer, "Failed/Invalid breakpoint: " + input_buffer);
694
								}
694
								}
695
							} else if (mode == MODE_INPUT_VARIABLE) {
695
							} else if (mode == INPUT_MODE_VARIABLE) {
696
								if (!frame.IsValid()) {
696
								if (!frame.IsValid()) {
697
									log_msg(log_buffer, "Error: No stack frame available to evaluate '" + input_buffer + "'");
697
									log_msg(log_buffer, "Error: No stack frame available to evaluate '" + input_buffer + "'");
698
								} else {
698
								} else {
...
710
								}
710
								}
711
							}
711
							}
712
						}
712
						}
713
						mode = MODE_NORMAL;
713
						mode = INPUT_MODE_NORMAL;
714
						input_buffer.clear();
714
						input_buffer.clear();
715
					} else if (ev.key == TB_KEY_BACKSPACE || ev.key == TB_KEY_BACKSPACE2) {
715
					} else if (ev.key == TB_KEY_BACKSPACE || ev.key == TB_KEY_BACKSPACE2) {
716
						if (!input_buffer.empty()) input_buffer.pop_back();
716
						if (!input_buffer.empty()) input_buffer.pop_back();
...