Serenity Operating System
at portability 79 lines 2.7 kB view raw
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 27#pragma once 28 29#include <AK/BinarySearch.h> 30#include <AK/FileSystemPath.h> 31#include <AK/QuickSort.h> 32#include <AK/String.h> 33#include <AK/Vector.h> 34#include <LibCore/DirIterator.h> 35#include <sys/stat.h> 36 37class LineEditor { 38public: 39 LineEditor(); 40 ~LineEditor(); 41 42 String get_line(const String& prompt); 43 44 void add_to_history(const String&); 45 const Vector<String>& history() const { return m_history; } 46 47 void cache_path(); 48 49private: 50 void clear_line(); 51 void insert(const String&); 52 void insert(const char); 53 void cut_mismatching_chars(String& completion, const String& other, size_t start_compare); 54 Vector<String> tab_complete_first_token(const String&); 55 Vector<String> tab_complete_other_token(String&); 56 void vt_save_cursor(); 57 void vt_restore_cursor(); 58 void vt_clear_to_end_of_line(); 59 60 Vector<char, 1024> m_buffer; 61 size_t m_cursor { 0 }; 62 size_t m_times_tab_pressed { 0 }; 63 size_t m_num_columns { 0 }; 64 65 // FIXME: This should be something more take_first()-friendly. 66 Vector<String> m_history; 67 size_t m_history_cursor { 0 }; 68 size_t m_history_capacity { 100 }; 69 70 Vector<String, 256> m_path; 71 72 enum class InputState { 73 Free, 74 ExpectBracket, 75 ExpectFinal, 76 ExpectTerminator, 77 }; 78 InputState m_state { InputState::Free }; 79};