ttx 0.1.0
Loading...
Searching...
No Matches
semantic_prompt.h
Go to the documentation of this file.
1#pragma once
2
3#include "di/container/ring/prelude.h"
4#include "di/container/tree/tree_map.h"
5#include "di/reflect/prelude.h"
9
10namespace ttx::terminal {
21
22constexpr auto tag_invoke(di::Tag<di::reflect>, di::InPlaceType<PromptClickMode>) {
23 using enum PromptClickMode;
24 return di::make_enumerators<"PromptClickMode">(
25 di::enumerator<"None", None>, di::enumerator<"Line", Line>,
26 di::enumerator<"MultipleLeftRight", MultipleLeftRight>, di::enumerator<"MultipleUpDown", MultipleUpDown>,
27 di::enumerator<"MultipleUpDownConservative", MultipleUpDownConservative>);
28}
29
37
38constexpr auto tag_invoke(di::Tag<di::reflect>, di::InPlaceType<PromptKind>) {
39 using enum PromptKind;
40 return di::make_enumerators<"PromptKind">(di::enumerator<"Initial", Initial>,
41 di::enumerator<"Continuation", Continuation>,
42 di::enumerator<"Secondary", Secondary>, di::enumerator<"Right", Right>);
43}
44
50struct Command {
51 di::String application_id;
54 bool prompt_redraw { true };
59 u32 depth { 0 };
60 bool failed { false };
61 bool ended { false };
62
63 auto operator==(Command const&) const -> bool = default;
64
65 constexpr friend auto tag_invoke(di::Tag<di::reflect>, di::InPlaceType<Command>) {
66 return di::make_fields<"Command">(
67 di::field<"application_id", &Command::application_id>,
68 di::field<"prompt_click_mode", &Command::prompt_click_mode>,
69 di::field<"prompt_kind", &Command::prompt_kind>, di::field<"prompt_redraw", &Command::prompt_redraw>,
70 di::field<"prompt_start", &Command::prompt_start>, di::field<"prompt_end", &Command::prompt_end>,
71 di::field<"output_start", &Command::output_start>, di::field<"output_end", &Command::output_end>,
72 di::field<"depth", &Command::depth>, di::field<"failed", &Command::failed>,
73 di::field<"ended", &Command::ended>);
74 }
75};
76
78class Commands {
79 // Cap the maximum command depth to more sanely handle cases
80 // where the shell fails to terminate the command, or otherwise
81 // spam us with nonsense like infinite number of prompts.
82 constexpr static auto max_depth = u32(20);
83
84 // Cap the maximum number of commands we store to prevent excessive
85 // memory usage.
86 constexpr static auto max_commands = u32(10000);
87
88public:
89 void clamp_commands(u64 absolute_row_start, u64 absolute_row_end);
90
91 void begin_prompt(di::String application_id, PromptClickMode click_mode, PromptKind kind, bool redraw,
92 u64 absolute_row, u32 col);
93 void end_prompt(u64 absolute_row, u32 col);
94 void end_input(u64 absolute_row, u32 col);
95 void end_command(di::String application_id, bool failed, u64 absolute_row, u32 col);
96
97 auto last_command() const -> di::Optional<Command const&>;
98 auto will_redraw_prompt_at_row(u64 absolute_row, u32 col) const -> di::Optional<u64>;
99 auto first_command_before(u64 absolute_row) const -> di::Optional<Command const&>;
100 auto first_command_after(u64 absolute_row) const -> di::Optional<Command const&>;
101
102 void apply_reflow_result(ReflowResult const& reflow_result);
103
104 auto commands() const -> di::Ring<Command> const& { return m_commands; }
105
106 constexpr friend auto tag_invoke(di::Tag<di::reflect>, di::InPlaceType<Commands>) {
107 return di::make_fields<"Commands">(di::field<"commands", &Commands::m_commands>);
108 }
109
110private:
111 di::Ring<Command> m_commands;
112 u32 m_current_depth { 0 };
113};
114}
Represents all commands received for the screen.
Definition semantic_prompt.h:78
auto will_redraw_prompt_at_row(u64 absolute_row, u32 col) const -> di::Optional< u64 >
Definition semantic_prompt.cpp:147
void end_command(di::String application_id, bool failed, u64 absolute_row, u32 col)
Definition semantic_prompt.cpp:105
void apply_reflow_result(ReflowResult const &reflow_result)
Definition semantic_prompt.cpp:185
auto last_command() const -> di::Optional< Command const & >
Definition semantic_prompt.cpp:137
auto first_command_after(u64 absolute_row) const -> di::Optional< Command const & >
Definition semantic_prompt.cpp:175
auto first_command_before(u64 absolute_row) const -> di::Optional< Command const & >
Definition semantic_prompt.cpp:165
void begin_prompt(di::String application_id, PromptClickMode click_mode, PromptKind kind, bool redraw, u64 absolute_row, u32 col)
Definition semantic_prompt.cpp:35
void end_input(u64 absolute_row, u32 col)
Definition semantic_prompt.cpp:86
void end_prompt(u64 absolute_row, u32 col)
Definition semantic_prompt.cpp:68
constexpr friend auto tag_invoke(di::Tag< di::reflect >, di::InPlaceType< Commands >)
Definition semantic_prompt.h:106
void clamp_commands(u64 absolute_row_start, u64 absolute_row_end)
Definition semantic_prompt.cpp:14
auto commands() const -> di::Ring< Command > const &
Definition semantic_prompt.h:104
Represents the result of reflowing a RowGroup.
Definition reflow_result.h:22
Definition absolute_position.h:6
PromptKind
Kind of prompt.
Definition semantic_prompt.h:31
@ Secondary
Secondary prompt (user cannot edit previous lines)
Definition semantic_prompt.h:34
@ Initial
Initial prompt (default)
Definition semantic_prompt.h:32
@ Right
Right-aligned prompt (can reflow to the right on resize)
Definition semantic_prompt.h:35
@ Continuation
Continuation prompt (user can edit previous lines)
Definition semantic_prompt.h:33
@ None
Definition mode.h:11
PromptClickMode
Controls how mouse events on the shell prompt are translated to the application.
Definition semantic_prompt.h:14
@ MultipleLeftRight
Can simulate up-down movement via lots of left-right presses.
Definition semantic_prompt.h:17
@ Line
Can only navigative a single line via mouse movement, using left-right presses.
Definition semantic_prompt.h:16
@ MultipleUpDown
Can use up-down and left-right.
Definition semantic_prompt.h:18
@ None
Doesn't support prompt click mode.
Definition semantic_prompt.h:15
@ MultipleUpDownConservative
Only allowed to use up down at col 0.
Definition semantic_prompt.h:19
constexpr auto tag_invoke(di::Tag< di::reflect >, di::InPlaceType< AnsiMode >)
Definition mode.h:14
Represents a coordinate anywhere in a screen, including scroll back.
Definition absolute_position.h:11
Represents a completed annotated command via OSC 133.
Definition semantic_prompt.h:50
constexpr friend auto tag_invoke(di::Tag< di::reflect >, di::InPlaceType< Command >)
Definition semantic_prompt.h:65
bool ended
Has the command been completed?
Definition semantic_prompt.h:61
PromptClickMode prompt_click_mode
Prompt click mode.
Definition semantic_prompt.h:52
auto operator==(Command const &) const -> bool=default
u32 depth
Level of nesting within other commands.
Definition semantic_prompt.h:59
AbsolutePosition output_start
Absolute position marking the command output start.
Definition semantic_prompt.h:57
AbsolutePosition prompt_start
Absolute position marking the prompt start.
Definition semantic_prompt.h:55
bool failed
Did the command exit successfully?
Definition semantic_prompt.h:60
AbsolutePosition output_end
Absolute position marking the command output end (exclusive)
Definition semantic_prompt.h:58
AbsolutePosition prompt_end
Absolute position marking the prompt end (inclusive)
Definition semantic_prompt.h:56
di::String application_id
Application id of the command.
Definition semantic_prompt.h:51
bool prompt_redraw
Does the application redraw the prompt?
Definition semantic_prompt.h:54
PromptKind prompt_kind
Prompt kind - for now we only support a single prompt region.
Definition semantic_prompt.h:53