| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/BinarySearch.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  | #include <AK/ByteBuffer.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  | #include <AK/Function.h>
 | 
					
						
							|  |  |  | #include <AK/HashMap.h>
 | 
					
						
							|  |  |  | #include <AK/NonnullOwnPtr.h>
 | 
					
						
							|  |  |  | #include <AK/QuickSort.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-25 16:57:07 +04:30
										 |  |  | #include <AK/Result.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-21 18:06:43 +04:30
										 |  |  | #include <AK/Utf32View.h>
 | 
					
						
							|  |  |  | #include <AK/Utf8View.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  | #include <AK/Vector.h>
 | 
					
						
							|  |  |  | #include <LibCore/DirIterator.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-26 15:04:39 +04:30
										 |  |  | #include <LibCore/Notifier.h>
 | 
					
						
							|  |  |  | #include <LibCore/Object.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-06 18:40:06 +03:00
										 |  |  | #include <LibLine/Span.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-29 20:08:02 +04:30
										 |  |  | #include <LibLine/StringMetrics.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-06 18:40:06 +03:00
										 |  |  | #include <LibLine/Style.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-22 03:52:34 +04:30
										 |  |  | #include <LibLine/SuggestionDisplay.h>
 | 
					
						
							|  |  |  | #include <LibLine/SuggestionManager.h>
 | 
					
						
							|  |  |  | #include <LibLine/VT.h>
 | 
					
						
							| 
									
										
										
										
											2020-08-17 18:05:29 +04:30
										 |  |  | #include <sys/ioctl.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  | #include <sys/stat.h>
 | 
					
						
							|  |  |  | #include <termios.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-31 13:34:06 +02:00
										 |  |  | namespace Line { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-30 08:19:47 +04:30
										 |  |  | struct Configuration { | 
					
						
							|  |  |  |     enum RefreshBehaviour { | 
					
						
							|  |  |  |         Lazy, | 
					
						
							|  |  |  |         Eager, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2020-05-26 19:52:01 +04:30
										 |  |  |     enum OperationMode { | 
					
						
							| 
									
										
										
										
											2020-08-05 09:58:47 +04:30
										 |  |  |         Unset, | 
					
						
							| 
									
										
										
										
											2020-05-26 19:52:01 +04:30
										 |  |  |         Full, | 
					
						
							|  |  |  |         NoEscapeSequences, | 
					
						
							| 
									
										
										
										
											2020-08-05 09:58:47 +04:30
										 |  |  |         NonInteractive, | 
					
						
							| 
									
										
										
										
											2020-05-26 19:52:01 +04:30
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2020-04-30 08:19:47 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  |     Configuration() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<typename Arg, typename... Rest> | 
					
						
							|  |  |  |     Configuration(Arg arg, Rest... rest) | 
					
						
							|  |  |  |         : Configuration(rest...) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         set(arg); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void set(RefreshBehaviour refresh) { refresh_behaviour = refresh; } | 
					
						
							| 
									
										
										
										
											2020-05-26 19:52:01 +04:30
										 |  |  |     void set(OperationMode mode) { operation_mode = mode; } | 
					
						
							| 
									
										
										
										
											2020-04-30 08:19:47 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 19:13:52 +04:30
										 |  |  |     static Configuration from_config(const StringView& libname = "line"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-30 08:19:47 +04:30
										 |  |  |     RefreshBehaviour refresh_behaviour { RefreshBehaviour::Lazy }; | 
					
						
							| 
									
										
										
										
											2020-08-05 09:58:47 +04:30
										 |  |  |     OperationMode operation_mode { OperationMode::Unset }; | 
					
						
							| 
									
										
										
										
											2020-04-30 08:19:47 +04:30
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-26 15:04:39 +04:30
										 |  |  | class Editor : public Core::Object { | 
					
						
							|  |  |  |     C_OBJECT(Editor); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-05-25 16:57:07 +04:30
										 |  |  |     enum class Error { | 
					
						
							|  |  |  |         ReadFailure, | 
					
						
							|  |  |  |         Empty, | 
					
						
							|  |  |  |         Eof, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-31 13:34:06 +02:00
										 |  |  |     ~Editor(); | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-25 16:57:07 +04:30
										 |  |  |     Result<String, Error> get_line(const String& prompt); | 
					
						
							| 
									
										
										
										
											2020-04-29 01:46:19 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-26 19:52:01 +04:30
										 |  |  |     void initialize(); | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  |     void add_to_history(const String&); | 
					
						
							|  |  |  |     const Vector<String>& history() const { return m_history; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-31 13:37:01 +02:00
										 |  |  |     void register_character_input_callback(char ch, Function<bool(Editor&)> callback); | 
					
						
							| 
									
										
										
										
											2020-06-26 16:43:55 +04:30
										 |  |  |     StringMetrics actual_rendered_string_metrics(const StringView&) const; | 
					
						
							|  |  |  |     StringMetrics actual_rendered_string_metrics(const Utf32View&) const; | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-19 08:42:01 +04:30
										 |  |  |     Function<Vector<CompletionSuggestion>(const Editor&)> on_tab_complete; | 
					
						
							| 
									
										
										
										
											2020-05-13 14:22:47 +04:30
										 |  |  |     Function<void()> on_interrupt_handled; | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     Function<void(Editor&)> on_display_refresh; | 
					
						
							| 
									
										
										
										
											2020-03-30 21:55:09 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 03:19:48 +04:30
										 |  |  |     // FIXME: we will have to kindly ask our instantiators to set our signal handlers,
 | 
					
						
							|  |  |  |     // since we can not do this cleanly ourselves. (signal() limitation: cannot give member functions)
 | 
					
						
							| 
									
										
										
										
											2020-04-29 00:31:22 +04:30
										 |  |  |     void interrupted() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-06-01 16:26:31 +04:30
										 |  |  |         if (m_is_editing) { | 
					
						
							| 
									
										
										
										
											2020-04-29 00:31:22 +04:30
										 |  |  |             m_was_interrupted = true; | 
					
						
							| 
									
										
										
										
											2020-06-01 16:26:31 +04:30
										 |  |  |             handle_interrupt_event(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     void resized() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_was_resized = true; | 
					
						
							| 
									
										
										
										
											2020-07-06 19:58:11 +04:30
										 |  |  |         m_previous_num_columns = m_num_columns; | 
					
						
							|  |  |  |         get_terminal_size(); | 
					
						
							|  |  |  |         m_suggestion_display->set_vt_size(m_num_lines, m_num_columns); | 
					
						
							| 
									
										
										
										
											2020-04-29 00:31:22 +04:30
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  |     size_t cursor() const { return m_cursor; } | 
					
						
							| 
									
										
										
										
											2020-05-18 13:47:34 +04:30
										 |  |  |     const Vector<u32, 1024>& buffer() const { return m_buffer; } | 
					
						
							|  |  |  |     u32 buffer_at(size_t pos) const { return m_buffer.at(pos); } | 
					
						
							| 
									
										
										
										
											2020-05-19 08:42:01 +04:30
										 |  |  |     String line() const { return line(m_buffer.size()); } | 
					
						
							|  |  |  |     String line(size_t up_to_index) const; | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 03:19:48 +04:30
										 |  |  |     // Only makes sense inside a character_input callback or on_* callback.
 | 
					
						
							| 
									
										
										
										
											2020-04-09 07:44:04 +04:30
										 |  |  |     void set_prompt(const String& prompt) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (m_cached_prompt_valid) | 
					
						
							| 
									
										
										
										
											2020-06-26 16:43:55 +04:30
										 |  |  |             m_old_prompt_metrics = m_cached_prompt_metrics; | 
					
						
							| 
									
										
										
										
											2020-04-09 07:44:04 +04:30
										 |  |  |         m_cached_prompt_valid = false; | 
					
						
							| 
									
										
										
										
											2020-06-26 16:43:55 +04:30
										 |  |  |         m_cached_prompt_metrics = actual_rendered_string_metrics(prompt); | 
					
						
							| 
									
										
										
										
											2020-04-09 07:44:04 +04:30
										 |  |  |         m_new_prompt = prompt; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  |     void clear_line(); | 
					
						
							|  |  |  |     void insert(const String&); | 
					
						
							| 
									
										
										
										
											2020-08-06 14:01:56 -04:00
										 |  |  |     void insert(const StringView&); | 
					
						
							| 
									
										
										
										
											2020-05-21 18:06:43 +04:30
										 |  |  |     void insert(const Utf32View&); | 
					
						
							| 
									
										
										
										
											2020-05-18 13:47:34 +04:30
										 |  |  |     void insert(const u32); | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     void stylize(const Span&, const Style&); | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  |     void strip_styles(bool strip_anchored = false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-21 05:14:34 +04:30
										 |  |  |     // Invariant Offset is an offset into the suggested data, hinting the editor what parts of the suggestion will not change
 | 
					
						
							|  |  |  |     // Static Offset is an offset into the token, signifying where the suggestions start
 | 
					
						
							|  |  |  |     // e.g.
 | 
					
						
							|  |  |  |     //    foobar<suggestion initiated>, on_tab_complete returns "barx", "bary", "barz"
 | 
					
						
							|  |  |  |     //       ^ ^
 | 
					
						
							|  |  |  |     //       +-|- static offset: the suggestions start here
 | 
					
						
							|  |  |  |     //         +- invariant offset: the suggestions do not change up to here
 | 
					
						
							| 
									
										
										
										
											2020-05-21 15:15:56 +04:30
										 |  |  |     //
 | 
					
						
							|  |  |  |     void suggest(size_t invariant_offset = 0, size_t static_offset = 0, Span::Mode offset_mode = Span::ByteOriented) const; | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 22:42:50 +04:30
										 |  |  |     const struct termios& termios() const { return m_termios; } | 
					
						
							|  |  |  |     const struct termios& default_termios() const { return m_default_termios; } | 
					
						
							| 
									
										
										
										
											2020-08-17 18:05:29 +04:30
										 |  |  |     struct winsize terminal_size() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         winsize ws { (u16)m_num_lines, (u16)m_num_columns, 0, 0 }; | 
					
						
							|  |  |  |         return ws; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-03-30 22:42:50 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-19 23:34:58 +04:30
										 |  |  |     void finish() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_finish = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-29 00:31:22 +04:30
										 |  |  |     bool is_editing() const { return m_is_editing; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-26 16:43:55 +04:30
										 |  |  |     const Utf32View buffer_view() const { return { m_buffer.data(), m_buffer.size() }; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-08-17 19:13:52 +04:30
										 |  |  |     explicit Editor(Configuration configuration = Configuration::from_config()); | 
					
						
							| 
									
										
										
										
											2020-05-26 15:04:39 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-26 16:43:55 +04:30
										 |  |  |     enum VTState { | 
					
						
							|  |  |  |         Free = 1, | 
					
						
							|  |  |  |         Escape = 3, | 
					
						
							|  |  |  |         Bracket = 5, | 
					
						
							|  |  |  |         BracketArgsSemi = 7, | 
					
						
							|  |  |  |         Title = 9, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     VTState actual_rendered_string_length_step(StringMetrics&, size_t& length, u32, u32, VTState) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-26 15:21:44 +04:30
										 |  |  |     // ^Core::Object
 | 
					
						
							|  |  |  |     virtual void save_to(JsonObject&) override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-19 08:42:01 +04:30
										 |  |  |     struct KeyCallback { | 
					
						
							|  |  |  |         KeyCallback(Function<bool(Editor&)> cb) | 
					
						
							|  |  |  |             : callback(move(cb)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Function<bool(Editor&)> callback; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-01 16:26:31 +04:30
										 |  |  |     void handle_interrupt_event(); | 
					
						
							| 
									
										
										
										
											2020-05-26 15:04:39 +04:30
										 |  |  |     void handle_read_event(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-09 07:44:04 +04:30
										 |  |  |     Vector<size_t, 2> vt_dsr(); | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  |     void remove_at_index(size_t); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     enum class ModificationKind { | 
					
						
							|  |  |  |         Insertion, | 
					
						
							|  |  |  |         Removal, | 
					
						
							| 
									
										
										
										
											2020-05-21 05:14:34 +04:30
										 |  |  |         ForcedOverlapRemoval, | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  |     }; | 
					
						
							|  |  |  |     void readjust_anchored_styles(size_t hint_index, ModificationKind); | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  |     Style find_applicable_style(size_t offset) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-20 17:20:31 +04:30
										 |  |  |     bool search(const StringView&, bool allow_empty = false, bool from_beginning = false); | 
					
						
							| 
									
										
										
										
											2020-04-19 23:34:58 +04:30
										 |  |  |     inline void end_search() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_is_searching = false; | 
					
						
							|  |  |  |         m_refresh_needed = true; | 
					
						
							|  |  |  |         m_search_offset = 0; | 
					
						
							|  |  |  |         if (m_reset_buffer_on_search_end) { | 
					
						
							| 
									
										
										
										
											2020-04-20 17:53:24 +04:30
										 |  |  |             m_buffer.clear(); | 
					
						
							|  |  |  |             for (auto ch : m_pre_search_buffer) | 
					
						
							|  |  |  |                 m_buffer.append(ch); | 
					
						
							| 
									
										
										
										
											2020-04-19 23:34:58 +04:30
										 |  |  |             m_cursor = m_pre_search_cursor; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-04-20 17:53:24 +04:30
										 |  |  |         m_reset_buffer_on_search_end = true; | 
					
						
							| 
									
										
										
										
											2020-04-19 23:34:58 +04:30
										 |  |  |         m_search_editor = nullptr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-09 07:44:04 +04:30
										 |  |  |     void reset() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-06-26 16:43:55 +04:30
										 |  |  |         m_cached_buffer_metrics.reset(); | 
					
						
							| 
									
										
										
										
											2020-05-11 11:55:42 +04:30
										 |  |  |         m_cached_prompt_valid = false; | 
					
						
							|  |  |  |         m_cursor = 0; | 
					
						
							|  |  |  |         m_drawn_cursor = 0; | 
					
						
							|  |  |  |         m_inline_search_cursor = 0; | 
					
						
							| 
									
										
										
										
											2020-06-26 16:43:55 +04:30
										 |  |  |         m_old_prompt_metrics = m_cached_prompt_metrics; | 
					
						
							| 
									
										
										
										
											2020-05-22 03:52:34 +04:30
										 |  |  |         set_origin(0, 0); | 
					
						
							| 
									
										
										
										
											2020-05-11 11:55:42 +04:30
										 |  |  |         m_prompt_lines_at_suggestion_initiation = 0; | 
					
						
							| 
									
										
										
										
											2020-04-09 07:44:04 +04:30
										 |  |  |         m_refresh_needed = true; | 
					
						
							| 
									
										
										
										
											2020-05-25 16:57:07 +04:30
										 |  |  |         m_input_error.clear(); | 
					
						
							| 
									
										
										
										
											2020-05-26 15:04:39 +04:30
										 |  |  |         m_returned_line = String::empty(); | 
					
						
							| 
									
										
										
										
											2020-04-09 07:44:04 +04:30
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     void refresh_display(); | 
					
						
							| 
									
										
										
										
											2020-04-19 23:34:58 +04:30
										 |  |  |     void cleanup(); | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-29 01:46:19 +04:30
										 |  |  |     void restore() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ASSERT(m_initialized); | 
					
						
							|  |  |  |         tcsetattr(0, TCSANOW, &m_default_termios); | 
					
						
							|  |  |  |         m_initialized = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-26 16:43:55 +04:30
										 |  |  |     const StringMetrics& current_prompt_metrics() const | 
					
						
							| 
									
										
										
										
											2020-04-09 07:44:04 +04:30
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-06-26 16:43:55 +04:30
										 |  |  |         return m_cached_prompt_valid ? m_cached_prompt_metrics : m_old_prompt_metrics; | 
					
						
							| 
									
										
										
										
											2020-04-09 07:44:04 +04:30
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     size_t num_lines() const | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-06-26 16:43:55 +04:30
										 |  |  |         return current_prompt_metrics().lines_with_addition(m_cached_buffer_metrics, m_num_columns); | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size_t cursor_line() const | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-06-26 16:43:55 +04:30
										 |  |  |         auto cursor = m_drawn_cursor; | 
					
						
							|  |  |  |         if (cursor > m_cursor) | 
					
						
							|  |  |  |             cursor = m_cursor; | 
					
						
							|  |  |  |         return current_prompt_metrics().lines_with_addition( | 
					
						
							|  |  |  |             actual_rendered_string_metrics(buffer_view().substring_view(0, cursor)), | 
					
						
							|  |  |  |             m_num_columns); | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size_t offset_in_line() const | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-06-26 16:43:55 +04:30
										 |  |  |         auto cursor = m_drawn_cursor; | 
					
						
							|  |  |  |         if (cursor > m_cursor) | 
					
						
							|  |  |  |             cursor = m_cursor; | 
					
						
							|  |  |  |         auto buffer_metrics = actual_rendered_string_metrics(buffer_view().substring_view(0, cursor)); | 
					
						
							|  |  |  |         if (buffer_metrics.line_lengths.size() > 1) | 
					
						
							|  |  |  |             return buffer_metrics.line_lengths.last() % m_num_columns; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return (buffer_metrics.line_lengths.last() + current_prompt_metrics().line_lengths.last()) % m_num_columns; | 
					
						
							| 
									
										
										
										
											2020-04-09 07:44:04 +04:30
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void set_origin() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         auto position = vt_dsr(); | 
					
						
							| 
									
										
										
										
											2020-05-22 03:52:34 +04:30
										 |  |  |         set_origin(position[0], position[1]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 00:48:40 +04:30
										 |  |  |     void set_origin(int row, int col) | 
					
						
							| 
									
										
										
										
											2020-05-22 03:52:34 +04:30
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-06-08 00:48:40 +04:30
										 |  |  |         m_origin_row = row; | 
					
						
							|  |  |  |         m_origin_column = col; | 
					
						
							|  |  |  |         m_suggestion_display->set_origin(row, col, {}); | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-19 08:42:01 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-11 14:06:46 +04:30
										 |  |  |     void recalculate_origin(); | 
					
						
							| 
									
										
										
										
											2020-06-29 20:08:02 +04:30
										 |  |  |     void reposition_cursor(bool to_end = false); | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-21 15:15:56 +04:30
										 |  |  |     struct CodepointRange { | 
					
						
							|  |  |  |         size_t start { 0 }; | 
					
						
							|  |  |  |         size_t end { 0 }; | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2020-08-05 16:31:20 -04:00
										 |  |  |     CodepointRange byte_offset_range_to_code_point_offset_range(size_t byte_start, size_t byte_end, size_t code_point_scan_offset, bool reverse = false) const; | 
					
						
							| 
									
										
										
										
											2020-05-21 15:15:56 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 19:58:11 +04:30
										 |  |  |     void get_terminal_size(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-19 23:34:58 +04:30
										 |  |  |     bool m_finish { false }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-26 15:04:39 +04:30
										 |  |  |     RefPtr<Editor> m_search_editor; | 
					
						
							| 
									
										
										
										
											2020-04-19 23:34:58 +04:30
										 |  |  |     bool m_is_searching { false }; | 
					
						
							|  |  |  |     bool m_reset_buffer_on_search_end { true }; | 
					
						
							|  |  |  |     size_t m_search_offset { 0 }; | 
					
						
							| 
									
										
										
										
											2020-04-20 17:20:31 +04:30
										 |  |  |     bool m_searching_backwards { true }; | 
					
						
							| 
									
										
										
										
											2020-04-19 23:34:58 +04:30
										 |  |  |     size_t m_pre_search_cursor { 0 }; | 
					
						
							| 
									
										
										
										
											2020-05-18 13:47:34 +04:30
										 |  |  |     Vector<u32, 1024> m_pre_search_buffer; | 
					
						
							| 
									
										
										
										
											2020-04-19 23:34:58 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-18 13:47:34 +04:30
										 |  |  |     Vector<u32, 1024> m_buffer; | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     ByteBuffer m_pending_chars; | 
					
						
							| 
									
										
										
										
											2020-05-25 16:57:07 +04:30
										 |  |  |     Vector<char, 512> m_incomplete_data; | 
					
						
							|  |  |  |     Optional<Error> m_input_error; | 
					
						
							| 
									
										
										
										
											2020-05-26 15:04:39 +04:30
										 |  |  |     String m_returned_line; | 
					
						
							| 
									
										
										
										
											2020-05-25 16:57:07 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  |     size_t m_cursor { 0 }; | 
					
						
							| 
									
										
										
										
											2020-04-09 07:44:04 +04:30
										 |  |  |     size_t m_drawn_cursor { 0 }; | 
					
						
							| 
									
										
										
										
											2020-04-20 17:20:31 +04:30
										 |  |  |     size_t m_inline_search_cursor { 0 }; | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     size_t m_chars_inserted_in_the_middle { 0 }; | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  |     size_t m_times_tab_pressed { 0 }; | 
					
						
							|  |  |  |     size_t m_num_columns { 0 }; | 
					
						
							| 
									
										
										
										
											2020-04-11 13:29:55 +04:30
										 |  |  |     size_t m_num_lines { 1 }; | 
					
						
							| 
									
										
										
										
											2020-07-06 19:58:11 +04:30
										 |  |  |     size_t m_previous_num_columns { 0 }; | 
					
						
							| 
									
										
										
										
											2020-06-26 16:43:55 +04:30
										 |  |  |     size_t m_extra_forward_lines { 0 }; | 
					
						
							|  |  |  |     StringMetrics m_cached_prompt_metrics; | 
					
						
							|  |  |  |     StringMetrics m_old_prompt_metrics; | 
					
						
							|  |  |  |     StringMetrics m_cached_buffer_metrics; | 
					
						
							| 
									
										
										
										
											2020-04-14 22:18:56 +04:30
										 |  |  |     size_t m_prompt_lines_at_suggestion_initiation { 0 }; | 
					
						
							| 
									
										
										
										
											2020-04-09 07:44:04 +04:30
										 |  |  |     bool m_cached_prompt_valid { false }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 03:19:48 +04:30
										 |  |  |     // Exact position before our prompt in the terminal.
 | 
					
						
							| 
									
										
										
										
											2020-06-08 00:48:40 +04:30
										 |  |  |     size_t m_origin_row { 0 }; | 
					
						
							|  |  |  |     size_t m_origin_column { 0 }; | 
					
						
							| 
									
										
										
										
											2020-04-09 07:44:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-22 03:52:34 +04:30
										 |  |  |     OwnPtr<SuggestionDisplay> m_suggestion_display; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-09 07:44:04 +04:30
										 |  |  |     String m_new_prompt; | 
					
						
							| 
									
										
										
										
											2020-05-22 03:52:34 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  |     SuggestionManager m_suggestion_manager; | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-19 23:34:58 +04:30
										 |  |  |     bool m_always_refresh { false }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-11 19:35:39 +01:00
										 |  |  |     enum class TabDirection { | 
					
						
							|  |  |  |         Forward, | 
					
						
							|  |  |  |         Backward, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     TabDirection m_tab_direction { TabDirection::Forward }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  |     HashMap<char, NonnullOwnPtr<KeyCallback>> m_key_callbacks; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 03:19:48 +04:30
										 |  |  |     // TODO: handle signals internally.
 | 
					
						
							| 
									
										
										
										
											2020-08-17 19:13:52 +04:30
										 |  |  |     struct termios m_termios { | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     struct termios m_default_termios { | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2020-04-19 23:34:58 +04:30
										 |  |  |     bool m_was_interrupted { false }; | 
					
						
							|  |  |  |     bool m_was_resized { false }; | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: This should be something more take_first()-friendly.
 | 
					
						
							|  |  |  |     Vector<String> m_history; | 
					
						
							|  |  |  |     size_t m_history_cursor { 0 }; | 
					
						
							|  |  |  |     size_t m_history_capacity { 100 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     enum class InputState { | 
					
						
							|  |  |  |         Free, | 
					
						
							| 
									
										
										
										
											2020-08-06 10:52:18 -04:00
										 |  |  |         GotEscape, | 
					
						
							|  |  |  |         GotEscapeFollowedByLeftBracket, | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  |         ExpectTerminator, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     InputState m_state { InputState::Free }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     HashMap<u32, HashMap<u32, Style>> m_spans_starting; | 
					
						
							|  |  |  |     HashMap<u32, HashMap<u32, Style>> m_spans_ending; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-18 02:25:58 +04:30
										 |  |  |     HashMap<u32, HashMap<u32, Style>> m_anchored_spans_starting; | 
					
						
							|  |  |  |     HashMap<u32, HashMap<u32, Style>> m_anchored_spans_ending; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-26 15:04:39 +04:30
										 |  |  |     RefPtr<Core::Notifier> m_notifier; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-05 06:41:33 +04:30
										 |  |  |     bool m_initialized { false }; | 
					
						
							|  |  |  |     bool m_refresh_needed { false }; | 
					
						
							| 
									
										
										
										
											2020-04-29 00:31:22 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  |     bool m_is_editing { false }; | 
					
						
							| 
									
										
										
										
											2020-04-30 08:19:47 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  |     Configuration m_configuration; | 
					
						
							| 
									
										
										
										
											2020-03-30 21:29:04 +04:30
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-03-31 13:34:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |