2020-03-30 16:59:04 +00:00
|
|
|
/*
|
|
|
|
* 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 02:11:33 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2020-03-30 16:59:04 +00:00
|
|
|
#include <AK/FileSystemPath.h>
|
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/NonnullOwnPtr.h>
|
|
|
|
#include <AK/QuickSort.h>
|
|
|
|
#include <AK/String.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <LibCore/DirIterator.h>
|
2020-04-05 02:11:33 +00:00
|
|
|
#include <Libraries/LibLine/Span.h>
|
|
|
|
#include <Libraries/LibLine/Style.h>
|
2020-03-30 16:59:04 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <termios.h>
|
|
|
|
|
2020-03-31 11:34:06 +00:00
|
|
|
namespace Line {
|
|
|
|
|
|
|
|
class Editor;
|
2020-03-30 16:59:04 +00:00
|
|
|
|
|
|
|
struct KeyCallback {
|
2020-03-31 11:34:06 +00:00
|
|
|
KeyCallback(Function<bool(Editor&)> cb)
|
2020-03-30 16:59:04 +00:00
|
|
|
: callback(move(cb))
|
|
|
|
{
|
|
|
|
}
|
2020-03-31 11:34:06 +00:00
|
|
|
Function<bool(Editor&)> callback;
|
2020-03-30 16:59:04 +00:00
|
|
|
};
|
|
|
|
|
2020-04-19 14:02:28 +00:00
|
|
|
struct CompletionSuggestion {
|
|
|
|
// intentionally not explicit (allows suggesting bare strings)
|
|
|
|
CompletionSuggestion(const String& completion)
|
|
|
|
: text(completion)
|
|
|
|
, trailing_trivia("")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
CompletionSuggestion(const StringView& completion, const StringView& trailing_trivia)
|
|
|
|
: text(completion)
|
|
|
|
, trailing_trivia(trailing_trivia)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const CompletionSuggestion& suggestion) const
|
|
|
|
{
|
|
|
|
return suggestion.text == text;
|
|
|
|
}
|
|
|
|
|
|
|
|
String text;
|
|
|
|
String trailing_trivia;
|
|
|
|
};
|
|
|
|
|
2020-04-30 03:49:47 +00:00
|
|
|
struct Configuration {
|
|
|
|
enum TokenSplitMechanism {
|
|
|
|
Spaces,
|
|
|
|
UnescapedSpaces,
|
|
|
|
};
|
|
|
|
enum RefreshBehaviour {
|
|
|
|
Lazy,
|
|
|
|
Eager,
|
|
|
|
};
|
|
|
|
|
|
|
|
Configuration()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Arg, typename... Rest>
|
|
|
|
Configuration(Arg arg, Rest... rest)
|
|
|
|
: Configuration(rest...)
|
|
|
|
{
|
|
|
|
set(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(RefreshBehaviour refresh) { refresh_behaviour = refresh; }
|
|
|
|
void set(TokenSplitMechanism split) { split_mechanism = split; }
|
|
|
|
|
|
|
|
RefreshBehaviour refresh_behaviour { RefreshBehaviour::Lazy };
|
|
|
|
TokenSplitMechanism split_mechanism { TokenSplitMechanism::Spaces };
|
|
|
|
};
|
|
|
|
|
2020-03-31 11:34:06 +00:00
|
|
|
class Editor {
|
2020-03-30 16:59:04 +00:00
|
|
|
public:
|
2020-04-30 03:49:47 +00:00
|
|
|
explicit Editor(Configuration configuration = {});
|
2020-03-31 11:34:06 +00:00
|
|
|
~Editor();
|
2020-03-30 16:59:04 +00:00
|
|
|
|
2020-04-28 21:16:19 +00:00
|
|
|
String get_line(const String& prompt);
|
|
|
|
|
2020-03-30 18:12:50 +00:00
|
|
|
void initialize()
|
2020-03-30 16:59:04 +00:00
|
|
|
{
|
2020-04-28 21:16:19 +00:00
|
|
|
if (m_initialized)
|
|
|
|
return;
|
|
|
|
|
2020-03-30 18:12:50 +00:00
|
|
|
struct termios termios;
|
|
|
|
tcgetattr(0, &termios);
|
|
|
|
m_default_termios = termios; // grab a copy to restore
|
|
|
|
// Because we use our own line discipline which includes echoing,
|
|
|
|
// we disable ICANON and ECHO.
|
|
|
|
termios.c_lflag &= ~(ECHO | ICANON);
|
|
|
|
tcsetattr(0, TCSANOW, &termios);
|
2020-03-30 16:59:04 +00:00
|
|
|
m_termios = termios;
|
|
|
|
m_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_to_history(const String&);
|
|
|
|
const Vector<String>& history() const { return m_history; }
|
|
|
|
|
2020-03-31 11:37:01 +00:00
|
|
|
void register_character_input_callback(char ch, Function<bool(Editor&)> callback);
|
2020-03-30 16:59:04 +00:00
|
|
|
|
2020-04-19 14:02:28 +00:00
|
|
|
Function<Vector<CompletionSuggestion>(const String&)> on_tab_complete_first_token;
|
|
|
|
Function<Vector<CompletionSuggestion>(const String&)> on_tab_complete_other_token;
|
2020-04-05 02:11:33 +00:00
|
|
|
Function<void(Editor&)> on_display_refresh;
|
2020-03-30 17:25:09 +00:00
|
|
|
|
|
|
|
// 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-28 20:01:22 +00:00
|
|
|
void interrupted()
|
|
|
|
{
|
|
|
|
if (m_is_editing)
|
|
|
|
m_was_interrupted = true;
|
|
|
|
}
|
2020-03-30 17:25:09 +00:00
|
|
|
void resized() { m_was_resized = true; }
|
2020-03-30 16:59:04 +00:00
|
|
|
|
|
|
|
size_t cursor() const { return m_cursor; }
|
|
|
|
const Vector<char, 1024>& buffer() const { return m_buffer; }
|
|
|
|
char buffer_at(size_t pos) const { return m_buffer.at(pos); }
|
|
|
|
|
2020-04-09 03:14:04 +00:00
|
|
|
// only makes sense inside a char_input callback or on_* callback
|
|
|
|
void set_prompt(const String& prompt)
|
|
|
|
{
|
|
|
|
if (m_cached_prompt_valid)
|
|
|
|
m_old_prompt_length = m_cached_prompt_length;
|
|
|
|
m_cached_prompt_valid = false;
|
|
|
|
m_cached_prompt_length = actual_rendered_string_length(prompt);
|
|
|
|
m_new_prompt = prompt;
|
|
|
|
}
|
|
|
|
|
2020-03-30 16:59:04 +00:00
|
|
|
void clear_line();
|
|
|
|
void insert(const String&);
|
|
|
|
void insert(const char);
|
2020-04-05 02:11:33 +00:00
|
|
|
void stylize(const Span&, const Style&);
|
|
|
|
void strip_styles()
|
|
|
|
{
|
|
|
|
m_spans_starting.clear();
|
|
|
|
m_spans_ending.clear();
|
|
|
|
m_refresh_needed = true;
|
|
|
|
}
|
2020-04-11 14:32:15 +00:00
|
|
|
void suggest(size_t invariant_offset = 0, size_t index = 0)
|
|
|
|
{
|
|
|
|
m_next_suggestion_index = index;
|
|
|
|
m_next_suggestion_invariant_offset = invariant_offset;
|
|
|
|
}
|
2020-03-30 16:59:04 +00:00
|
|
|
|
2020-03-30 18:12:50 +00:00
|
|
|
const struct termios& termios() const { return m_termios; }
|
|
|
|
const struct termios& default_termios() const { return m_default_termios; }
|
|
|
|
|
2020-04-19 19:04:58 +00:00
|
|
|
void finish()
|
|
|
|
{
|
|
|
|
m_finish = true;
|
|
|
|
}
|
|
|
|
|
2020-04-28 20:01:22 +00:00
|
|
|
bool is_editing() const { return m_is_editing; }
|
|
|
|
|
2020-03-30 16:59:04 +00:00
|
|
|
private:
|
|
|
|
void vt_save_cursor();
|
|
|
|
void vt_restore_cursor();
|
|
|
|
void vt_clear_to_end_of_line();
|
2020-04-05 02:11:33 +00:00
|
|
|
void vt_clear_lines(size_t count_above, size_t count_below = 0);
|
|
|
|
void vt_move_relative(int x, int y);
|
2020-04-09 03:14:04 +00:00
|
|
|
void vt_move_absolute(u32 x, u32 y);
|
2020-04-05 02:11:33 +00:00
|
|
|
void vt_apply_style(const Style&);
|
2020-04-09 03:14:04 +00:00
|
|
|
Vector<size_t, 2> vt_dsr();
|
2020-04-05 02:11:33 +00:00
|
|
|
|
|
|
|
Style find_applicable_style(size_t offset) const;
|
|
|
|
|
2020-04-20 12:50:31 +00:00
|
|
|
bool search(const StringView&, bool allow_empty = false, bool from_beginning = false);
|
2020-04-19 19:04:58 +00:00
|
|
|
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 13:23:24 +00:00
|
|
|
m_buffer.clear();
|
|
|
|
for (auto ch : m_pre_search_buffer)
|
|
|
|
m_buffer.append(ch);
|
2020-04-19 19:04:58 +00:00
|
|
|
m_cursor = m_pre_search_cursor;
|
|
|
|
}
|
2020-04-20 13:23:24 +00:00
|
|
|
m_reset_buffer_on_search_end = true;
|
2020-04-19 19:04:58 +00:00
|
|
|
m_search_editor = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-04-09 03:14:04 +00:00
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
m_origin_x = 0;
|
|
|
|
m_origin_y = 0;
|
|
|
|
m_old_prompt_length = m_cached_prompt_length;
|
|
|
|
m_refresh_needed = true;
|
|
|
|
m_cursor = 0;
|
2020-04-30 10:23:49 +00:00
|
|
|
m_inline_search_cursor = 0;
|
2020-04-09 03:14:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 02:11:33 +00:00
|
|
|
void refresh_display();
|
2020-04-19 19:04:58 +00:00
|
|
|
void cleanup();
|
2020-04-05 02:11:33 +00:00
|
|
|
|
2020-04-28 21:16:19 +00:00
|
|
|
void restore()
|
|
|
|
{
|
|
|
|
ASSERT(m_initialized);
|
|
|
|
tcsetattr(0, TCSANOW, &m_default_termios);
|
|
|
|
m_initialized = false;
|
|
|
|
}
|
|
|
|
|
2020-04-09 03:14:04 +00:00
|
|
|
size_t current_prompt_length() const
|
|
|
|
{
|
|
|
|
return m_cached_prompt_valid ? m_cached_prompt_length : m_old_prompt_length;
|
|
|
|
}
|
|
|
|
|
2020-04-05 02:11:33 +00:00
|
|
|
size_t num_lines() const
|
|
|
|
{
|
2020-04-14 17:48:56 +00:00
|
|
|
return (m_cached_buffer_size + m_num_columns + current_prompt_length() - 1) / m_num_columns;
|
2020-04-05 02:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t cursor_line() const
|
|
|
|
{
|
2020-04-14 17:48:56 +00:00
|
|
|
return (m_drawn_cursor + m_num_columns + current_prompt_length() - 1) / m_num_columns;
|
2020-04-05 02:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t offset_in_line() const
|
|
|
|
{
|
2020-04-09 03:14:04 +00:00
|
|
|
return (m_drawn_cursor + current_prompt_length()) % m_num_columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t actual_rendered_string_length(const StringView& string) const;
|
|
|
|
|
|
|
|
void set_origin()
|
|
|
|
{
|
|
|
|
auto position = vt_dsr();
|
|
|
|
m_origin_x = position[0];
|
|
|
|
m_origin_y = position[1];
|
2020-04-05 02:11:33 +00:00
|
|
|
}
|
2020-04-11 09:36:46 +00:00
|
|
|
void recalculate_origin();
|
2020-04-09 03:14:04 +00:00
|
|
|
void reposition_cursor();
|
2020-03-30 16:59:04 +00:00
|
|
|
|
2020-04-19 19:04:58 +00:00
|
|
|
bool m_finish { false };
|
|
|
|
|
|
|
|
OwnPtr<Editor> m_search_editor;
|
|
|
|
bool m_is_searching { false };
|
|
|
|
bool m_reset_buffer_on_search_end { true };
|
|
|
|
size_t m_search_offset { 0 };
|
2020-04-20 12:50:31 +00:00
|
|
|
bool m_searching_backwards { true };
|
2020-04-19 19:04:58 +00:00
|
|
|
size_t m_pre_search_cursor { 0 };
|
|
|
|
Vector<char, 1024> m_pre_search_buffer;
|
|
|
|
|
2020-03-30 16:59:04 +00:00
|
|
|
Vector<char, 1024> m_buffer;
|
2020-04-05 02:11:33 +00:00
|
|
|
ByteBuffer m_pending_chars;
|
2020-03-30 16:59:04 +00:00
|
|
|
size_t m_cursor { 0 };
|
2020-04-09 03:14:04 +00:00
|
|
|
size_t m_drawn_cursor { 0 };
|
2020-04-20 12:50:31 +00:00
|
|
|
size_t m_inline_search_cursor { 0 };
|
2020-04-05 02:11:33 +00:00
|
|
|
size_t m_chars_inserted_in_the_middle { 0 };
|
2020-03-30 16:59:04 +00:00
|
|
|
size_t m_times_tab_pressed { 0 };
|
|
|
|
size_t m_num_columns { 0 };
|
2020-04-11 08:59:55 +00:00
|
|
|
size_t m_num_lines { 1 };
|
2020-04-09 03:14:04 +00:00
|
|
|
size_t m_cached_prompt_length { 0 };
|
|
|
|
size_t m_old_prompt_length { 0 };
|
|
|
|
size_t m_cached_buffer_size { 0 };
|
2020-04-11 08:59:55 +00:00
|
|
|
size_t m_lines_used_for_last_suggestions { 0 };
|
2020-04-14 17:48:56 +00:00
|
|
|
size_t m_prompt_lines_at_suggestion_initiation { 0 };
|
2020-04-09 03:14:04 +00:00
|
|
|
bool m_cached_prompt_valid { false };
|
|
|
|
|
|
|
|
// exact position before our prompt in the terminal
|
|
|
|
size_t m_origin_x { 0 };
|
|
|
|
size_t m_origin_y { 0 };
|
|
|
|
|
|
|
|
String m_new_prompt;
|
2020-04-19 14:02:28 +00:00
|
|
|
Vector<CompletionSuggestion> m_suggestions;
|
|
|
|
CompletionSuggestion m_last_shown_suggestion { String::empty() };
|
2020-04-19 11:25:17 +00:00
|
|
|
size_t m_last_shown_suggestion_display_length { 0 };
|
|
|
|
bool m_last_shown_suggestion_was_complete { false };
|
2020-04-11 14:32:15 +00:00
|
|
|
size_t m_next_suggestion_index { 0 };
|
|
|
|
size_t m_next_suggestion_invariant_offset { 0 };
|
2020-04-19 11:25:17 +00:00
|
|
|
size_t m_largest_common_suggestion_prefix_length { 0 };
|
2020-03-30 16:59:04 +00:00
|
|
|
|
2020-04-19 19:04:58 +00:00
|
|
|
bool m_always_refresh { false };
|
|
|
|
|
2020-04-11 18:35:39 +00:00
|
|
|
enum class TabDirection {
|
|
|
|
Forward,
|
|
|
|
Backward,
|
|
|
|
};
|
|
|
|
TabDirection m_tab_direction { TabDirection::Forward };
|
|
|
|
|
2020-03-30 16:59:04 +00:00
|
|
|
HashMap<char, NonnullOwnPtr<KeyCallback>> m_key_callbacks;
|
|
|
|
|
|
|
|
// TODO: handle signals internally
|
2020-03-30 18:12:50 +00:00
|
|
|
struct termios m_termios, m_default_termios;
|
2020-04-19 19:04:58 +00:00
|
|
|
bool m_was_interrupted { false };
|
|
|
|
bool m_was_resized { false };
|
2020-03-30 16:59:04 +00:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
ExpectBracket,
|
|
|
|
ExpectFinal,
|
|
|
|
ExpectTerminator,
|
|
|
|
};
|
|
|
|
InputState m_state { InputState::Free };
|
|
|
|
|
2020-04-05 02:11:33 +00:00
|
|
|
HashMap<u32, HashMap<u32, Style>> m_spans_starting;
|
|
|
|
HashMap<u32, HashMap<u32, Style>> m_spans_ending;
|
|
|
|
|
|
|
|
bool m_initialized { false };
|
|
|
|
bool m_refresh_needed { false };
|
2020-04-28 20:01:22 +00:00
|
|
|
|
|
|
|
bool m_is_editing { false };
|
2020-04-30 03:49:47 +00:00
|
|
|
|
|
|
|
Configuration m_configuration;
|
2020-03-30 16:59:04 +00:00
|
|
|
};
|
2020-03-31 11:34:06 +00:00
|
|
|
|
|
|
|
}
|