ttx 0.1.0
Loading...
Searching...
No Matches
pane.h
Go to the documentation of this file.
1#pragma once
2
3#include "di/container/string/string_view.h"
4#include "di/container/vector/vector.h"
5#include "di/function/container/function.h"
6#include "di/sync/atomic.h"
7#include "di/sync/synchronized.h"
8#include "di/vocab/error/result.h"
9#include "di/vocab/pointer/box.h"
10#include "dius/sync_file.h"
11#include "dius/system/process.h"
12#include "dius/thread.h"
13#include "dius/tty.h"
14#include "ttx/direction.h"
15#include "ttx/key_event.h"
16#include "ttx/mouse.h"
17#include "ttx/paste_event.h"
18#include "ttx/renderer.h"
19#include "ttx/terminal.h"
20
21namespace ttx {
23 di::Vector<di::TransparentStringView> command {};
24 di::Optional<di::Path> capture_command_output_path {};
25 di::Optional<di::Path> replay_path {};
26 di::Optional<di::Path> save_state_path {};
27};
28
29class Pane {
30public:
31 static auto create_from_replay(di::PathView replay_path, di::Optional<di::Path> save_state_path,
32 dius::tty::WindowSize size, di::Function<void(Pane&)> did_exit,
33 di::Function<void(Pane&)> did_update,
34 di::Function<void(di::Span<byte const>)> did_selection,
35 di::Function<void(di::StringView)> apc_passthrough) -> di::Result<di::Box<Pane>>;
36 static auto create(CreatePaneArgs args, dius::tty::WindowSize size, di::Function<void(Pane&)> did_exit,
37 di::Function<void(Pane&)> did_update, di::Function<void(di::Span<byte const>)> did_selection,
38 di::Function<void(di::StringView)> apc_passthrough) -> di::Result<di::Box<Pane>>;
39
40 // For testing, create a mock pane. This doesn't actually create a psuedo terminal or a subprocess.
41 static auto create_mock() -> di::Box<Pane>;
42
43 explicit Pane(dius::SyncFile pty_controller, dius::system::ProcessHandle process,
44 di::Function<void(Pane&)> did_exit, di::Function<void(Pane&)> did_update,
45 di::Function<void(di::Span<byte const>)> did_selection,
46 di::Function<void(di::StringView)> apc_passthrough)
47 : m_pty_controller(di::move(pty_controller))
48 , m_terminal(m_pty_controller)
49 , m_process(process)
50 , m_did_exit(di::move(did_exit))
51 , m_did_update(di::move(did_update))
52 , m_did_selection(di::move(did_selection))
53 , m_apc_passthrough(di::move(apc_passthrough)) {}
54 ~Pane();
55
56 auto draw(Renderer& renderer) -> RenderedCursor;
57
58 auto event(KeyEvent const& event) -> bool;
59 auto event(MouseEvent const& event) -> bool;
60 auto event(FocusEvent const& event) -> bool;
61 auto event(PasteEvent const& event) -> bool;
62
63 void invalidate_all();
64 void resize(dius::tty::WindowSize const& size);
65 void scroll(Direction direction, i32 amount_in_cells);
66 auto save_state(di::PathView path) -> di::Result<>;
67 void stop_capture();
68 void exit();
69
70private:
71 auto selection_text() -> di::String;
72 auto in_selection(MouseCoordinate coordinate) -> bool;
73 void clear_selection();
74 void reset_scroll();
75
76 di::Atomic<bool> m_done { false };
77 di::Atomic<bool> m_capture { true };
78 di::Optional<MousePosition> m_last_mouse_position;
79 dius::SyncFile m_pty_controller;
80 di::Synchronized<Terminal> m_terminal;
81 dius::system::ProcessHandle m_process;
82
83 u32 m_vertical_scroll_offset { 0 };
84 u32 m_horizontal_scroll_offset { 0 };
85
86 di::Optional<MouseCoordinate> m_selection_start;
87 di::Optional<MouseCoordinate> m_selection_end;
88
89 // Application controlled callback when the internal process exits.
90 di::Function<void(Pane&)> m_did_exit;
91
92 // Application controlled callback when the terminal buffer has updated.
93 di::Function<void(Pane&)> m_did_update;
94
95 // Application controlled callback when text is selected.
96 di::Function<void(di::Span<byte const>)> m_did_selection;
97
98 // Application controlled callback when APC command is set.
99 di::Function<void(di::StringView)> m_apc_passthrough;
100
101 // These are declared last, for when dius::Thread calls join() in the destructor.
102 dius::Thread m_process_thread;
103 dius::Thread m_reader_thread;
104};
105}
Definition focus_event.h:7
Definition key_event.h:24
Definition mouse.h:39
Definition mouse_event.h:23
static auto create_mock() -> di::Box< Pane >
Definition pane.cpp:215
void exit()
Definition pane.cpp:445
static auto create_from_replay(di::PathView replay_path, di::Optional< di::Path > save_state_path, dius::tty::WindowSize size, di::Function< void(Pane &)> did_exit, di::Function< void(Pane &)> did_update, di::Function< void(di::Span< byte const >)> did_selection, di::Function< void(di::StringView)> apc_passthrough) -> di::Result< di::Box< Pane > >
Definition pane.cpp:47
void stop_capture()
Definition pane.cpp:441
auto save_state(di::PathView path) -> di::Result<>
Definition pane.cpp:433
void resize(dius::tty::WindowSize const &size)
Definition pane.cpp:377
void invalidate_all()
Definition pane.cpp:371
~Pane()
Definition pane.cpp:221
void scroll(Direction direction, i32 amount_in_cells)
Definition pane.cpp:385
static auto create(CreatePaneArgs args, dius::tty::WindowSize size, di::Function< void(Pane &)> did_exit, di::Function< void(Pane &)> did_update, di::Function< void(di::Span< byte const >)> did_selection, di::Function< void(di::StringView)> apc_passthrough) -> di::Result< di::Box< Pane > >
Definition pane.cpp:118
Pane(dius::SyncFile pty_controller, dius::system::ProcessHandle process, di::Function< void(Pane &)> did_exit, di::Function< void(Pane &)> did_update, di::Function< void(di::Span< byte const >)> did_selection, di::Function< void(di::StringView)> apc_passthrough)
Definition pane.h:43
auto draw(Renderer &renderer) -> RenderedCursor
Definition pane.cpp:228
auto event(KeyEvent const &event) -> bool
Definition pane.cpp:262
Definition paste_event.h:8
Definition renderer.h:18
Definition cursor_style.h:5
Direction
Definition direction.h:7
Definition pane.h:22
di::Optional< di::Path > capture_command_output_path
Definition pane.h:24
di::Optional< di::Path > replay_path
Definition pane.h:25
di::Vector< di::TransparentStringView > command
Definition pane.h:23
di::Optional< di::Path > save_state_path
Definition pane.h:26
Definition renderer.h:11