LineEditor.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/BinarySearch.h>
  28. #include <AK/FileSystemPath.h>
  29. #include <AK/Function.h>
  30. #include <AK/HashMap.h>
  31. #include <AK/NonnullOwnPtr.h>
  32. #include <AK/QuickSort.h>
  33. #include <AK/String.h>
  34. #include <AK/Vector.h>
  35. #include <LibCore/DirIterator.h>
  36. #include <sys/stat.h>
  37. #include <termios.h>
  38. class LineEditor;
  39. struct KeyCallback {
  40. KeyCallback(Function<bool(LineEditor&)> cb)
  41. : callback(move(cb))
  42. {
  43. }
  44. Function<bool(LineEditor&)> callback;
  45. };
  46. class LineEditor {
  47. public:
  48. LineEditor();
  49. ~LineEditor();
  50. void initialize()
  51. {
  52. ASSERT(!m_initialized);
  53. struct termios termios;
  54. tcgetattr(0, &termios);
  55. m_default_termios = termios; // grab a copy to restore
  56. // Because we use our own line discipline which includes echoing,
  57. // we disable ICANON and ECHO.
  58. termios.c_lflag &= ~(ECHO | ICANON);
  59. tcsetattr(0, TCSANOW, &termios);
  60. m_termios = termios;
  61. m_initialized = true;
  62. }
  63. String get_line(const String& prompt);
  64. void add_to_history(const String&);
  65. const Vector<String>& history() const { return m_history; }
  66. void on_char_input(char ch, Function<bool(LineEditor&)> callback);
  67. Function<Vector<String>(const String&)> on_tab_complete_first_token = nullptr;
  68. Function<Vector<String>(const String&)> on_tab_complete_other_token = nullptr;
  69. // FIXME: we will have to kindly ask our instantiators to set our signal handlers
  70. // since we can not do this cleanly ourselves (signal() limitation: cannot give member functions)
  71. void interrupted() { m_was_interrupted = true; }
  72. void resized() { m_was_resized = true; }
  73. size_t cursor() const { return m_cursor; }
  74. const Vector<char, 1024>& buffer() const { return m_buffer; }
  75. char buffer_at(size_t pos) const { return m_buffer.at(pos); }
  76. void clear_line();
  77. void insert(const String&);
  78. void insert(const char);
  79. void cut_mismatching_chars(String& completion, const String& other, size_t start_compare);
  80. const struct termios& termios() const { return m_termios; }
  81. const struct termios& default_termios() const { return m_default_termios; }
  82. private:
  83. void vt_save_cursor();
  84. void vt_restore_cursor();
  85. void vt_clear_to_end_of_line();
  86. Vector<char, 1024> m_buffer;
  87. size_t m_cursor { 0 };
  88. size_t m_times_tab_pressed { 0 };
  89. size_t m_num_columns { 0 };
  90. HashMap<char, NonnullOwnPtr<KeyCallback>> m_key_callbacks;
  91. // TODO: handle signals internally
  92. struct termios m_termios, m_default_termios;
  93. bool m_was_interrupted = false;
  94. bool m_was_resized = false;
  95. // FIXME: This should be something more take_first()-friendly.
  96. Vector<String> m_history;
  97. size_t m_history_cursor { 0 };
  98. size_t m_history_capacity { 100 };
  99. enum class InputState {
  100. Free,
  101. ExpectBracket,
  102. ExpectFinal,
  103. ExpectTerminator,
  104. };
  105. InputState m_state { InputState::Free };
  106. bool m_initialized = false;
  107. };