should be it

This commit is contained in:
2025-10-24 19:21:19 -05:00
parent a4b23fc57c
commit f09560c7b1
14047 changed files with 3161551 additions and 1 deletions

View File

@@ -0,0 +1,50 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// highlighting.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
#include "duckdb/common/common.hpp"
namespace duckdb {
struct searchMatch;
enum class tokenType : uint8_t {
TOKEN_IDENTIFIER,
TOKEN_NUMERIC_CONSTANT,
TOKEN_STRING_CONSTANT,
TOKEN_OPERATOR,
TOKEN_KEYWORD,
TOKEN_COMMENT,
TOKEN_CONTINUATION,
TOKEN_CONTINUATION_SELECTED,
TOKEN_BRACKET,
TOKEN_ERROR
};
enum class HighlightingType { KEYWORD, CONSTANT, COMMENT, ERROR, CONTINUATION, CONTINUATION_SELECTED };
struct highlightToken {
tokenType type;
size_t start = 0;
bool search_match = false;
};
class Highlighting {
public:
static void Enable();
static void Disable();
static bool IsEnabled();
static const char *GetColorOption(const char *option);
static void SetHighlightingColor(HighlightingType type, const char *color);
static vector<highlightToken> Tokenize(char *buf, size_t len, bool is_dot_command, searchMatch *match);
static string HighlightText(char *buf, size_t len, size_t start_pos, size_t end_pos,
const vector<highlightToken> &tokens);
};
} // namespace duckdb

View File

@@ -0,0 +1,29 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// history.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
#include "duckdb/common/common.hpp"
namespace duckdb {
class History {
public:
static void Free();
static idx_t GetLength();
static const char *GetEntry(idx_t index);
static void Overwrite(idx_t index, const char *new_entry);
static void RemoveLastEntry();
static int Add(const char *line);
static int Add(const char *line, idx_t len);
static int SetMaxLength(idx_t len);
static int Save(const char *filename);
static int Load(const char *filename);
};
} // namespace duckdb

View File

@@ -0,0 +1,76 @@
/* linenoise.h -- VERSION 1.0
*
* Guerrilla line editing library against the idea that a line editing lib
* needs to be 20,000 lines of C code.
*
* See linenoise.c for more information.
*
* ------------------------------------------------------------------------
*
* Copyright (c) 2010-2014, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * 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.
*/
#ifndef __LINENOISE_H
#define __LINENOISE_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct linenoiseCompletions {
size_t len;
char **cvec;
} linenoiseCompletions;
typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);
typedef char *(linenoiseHintsCallback)(const char *, int *color, int *bold);
typedef void(linenoiseFreeHintsCallback)(void *);
void linenoiseSetCompletionCallback(linenoiseCompletionCallback *);
void linenoiseSetHintsCallback(linenoiseHintsCallback *);
void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *);
void linenoiseAddCompletion(linenoiseCompletions *, const char *);
char *linenoise(const char *prompt);
void linenoiseFree(void *ptr);
int linenoiseParseOption(const char **azArg, int nArg, const char **out_error);
int linenoiseHistoryAdd(const char *line);
int linenoiseHistorySetMaxLen(int len);
int linenoiseHistorySave(const char *filename);
int linenoiseHistoryLoad(const char *filename);
void linenoiseClearScreen(void);
void linenoiseSetMultiLine(int ml);
size_t linenoiseComputeRenderWidth(const char *buf, size_t len);
int linenoiseGetRenderPosition(const char *buf, size_t len, int max_width, int *n);
void linenoiseSetPrompt(const char *continuation, const char *continuationSelected);
#ifdef __cplusplus
}
#endif
#endif /* __LINENOISE_H */

View File

@@ -0,0 +1,190 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// linenoise.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
#include "duckdb/common/common.hpp"
#include "duckdb/common/exception.hpp"
#include "terminal.hpp"
#include "linenoise.h"
#define LINENOISE_MAX_LINE 204800
#define LINENOISE_MAX_HISTORY 104857600
#define LINENOISE_EDITOR
namespace duckdb {
struct highlightToken;
struct AppendBuffer;
enum class HistoryScrollDirection : uint8_t {
LINENOISE_HISTORY_NEXT,
LINENOISE_HISTORY_PREV,
LINENOISE_HISTORY_START,
LINENOISE_HISTORY_END
};
enum class Capitalization : uint8_t { CAPITALIZE, LOWERCASE, UPPERCASE };
struct searchMatch {
size_t history_index;
size_t match_start;
size_t match_end;
};
struct Completion {
string completion;
idx_t cursor_pos;
};
struct TabCompletion {
vector<Completion> completions;
};
class Linenoise {
public:
Linenoise(int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt);
public:
int Edit();
static void SetCompletionCallback(linenoiseCompletionCallback *fn);
static void SetHintsCallback(linenoiseHintsCallback *fn);
static void SetFreeHintsCallback(linenoiseFreeHintsCallback *fn);
static linenoiseHintsCallback *HintsCallback();
static linenoiseFreeHintsCallback *FreeHintsCallback();
static void SetPrompt(const char *continuation, const char *continuationSelected);
static size_t ComputeRenderWidth(const char *buf, size_t len);
static int GetRenderPosition(const char *buf, size_t len, int max_width, int *n);
static int ParseOption(const char **azArg, int nArg, const char **out_error);
int GetPromptWidth() const;
void RefreshLine();
int CompleteLine(EscapeSequence &current_sequence);
void InsertCharacter(char c);
int EditInsert(char c);
int EditInsertMulti(const char *c);
void EditMoveLeft();
void EditMoveRight();
void EditMoveWordLeft();
void EditMoveWordRight();
bool EditMoveRowUp();
bool EditMoveRowDown();
void EditMoveHome();
void EditMoveEnd();
void EditMoveStartOfLine();
void EditMoveEndOfLine();
void EditHistoryNext(HistoryScrollDirection dir);
void EditHistorySetIndex(idx_t index);
void EditDelete();
void EditBackspace();
void EditDeletePrevWord();
void EditDeleteNextWord();
void EditDeleteAll();
void EditCapitalizeNextWord(Capitalization capitalization);
void EditRemoveSpaces();
void EditSwapCharacter();
void EditSwapWord();
void StartSearch();
void CancelSearch();
char AcceptSearch(char nextCommand);
void PerformSearch();
void SearchPrev();
void SearchNext();
#ifdef LINENOISE_EDITOR
bool EditBufferWithEditor(const char *editor);
bool EditFileWithEditor(const string &file_name, const char *editor);
#endif
char Search(char c);
void RefreshMultiLine();
void RefreshSingleLine() const;
void RefreshSearch();
void RefreshShowHints(AppendBuffer &append_buffer, int plen) const;
size_t PrevChar() const;
size_t NextChar() const;
void NextPosition(const char *buf, size_t len, size_t &cpos, int &rows, int &cols, int plen) const;
void PositionToColAndRow(size_t target_pos, int &out_row, int &out_col, int &rows, int &cols) const;
size_t ColAndRowToPosition(int target_row, int target_col) const;
string AddContinuationMarkers(const char *buf, size_t len, int plen, int cursor_row,
vector<highlightToken> &tokens) const;
void AddErrorHighlighting(idx_t render_start, idx_t render_end, vector<highlightToken> &tokens) const;
bool AddCompletionMarker(const char *buf, idx_t len, string &result_buffer, vector<highlightToken> &tokens) const;
static bool IsNewline(char c);
static bool IsWordBoundary(char c);
static bool AllWhitespace(const char *z);
static bool IsSpace(char c);
TabCompletion TabComplete() const;
static void EnableCompletionRendering();
static void DisableCompletionRendering();
static void EnableErrorRendering();
static void DisableErrorRendering();
public:
static void LogTokens(const vector<highlightToken> &tokens);
#ifdef LINENOISE_LOGGING
// Logging
template <typename... Args>
static void Log(const string &msg, Args... params) {
std::vector<ExceptionFormatValue> values;
LogMessageRecursive(msg, values, params...);
}
static void LogMessageRecursive(const string &msg, std::vector<ExceptionFormatValue> &values);
template <class T, typename... Args>
static void LogMessageRecursive(const string &msg, std::vector<ExceptionFormatValue> &values, T param,
Args... params) {
values.push_back(ExceptionFormatValue::CreateFormatValue<T>(param));
LogMessageRecursive(msg, values, params...);
}
#else
template <typename... Args>
static void Log(const string &msg, Args... params) {
// nop
}
#endif
public:
int ifd; /* Terminal stdin file descriptor. */
int ofd; /* Terminal stdout file descriptor. */
char *buf; /* Edited line buffer. */
size_t buflen; /* Edited line buffer size. */
const char *prompt; /* Prompt to display. */
size_t plen; /* Prompt length. */
size_t pos; /* Current cursor position. */
size_t old_cursor_rows; /* Previous refresh cursor position. */
size_t len; /* Current edited line length. */
size_t y_scroll; /* The y scroll position (multiline mode) */
TerminalSize ws; /* Terminal size */
size_t maxrows; /* Maximum num of rows used so far (multiline mode) */
idx_t history_index; /* The history index we are currently editing. */
bool clear_screen; /* Whether we are clearing the screen */
bool continuation_markers; /* Whether or not to render continuation markers */
bool search; /* Whether or not we are searching our history */
bool render; /* Whether or not to re-render */
bool has_more_data; /* Whether or not there is more data available in the buffer (copy+paste)*/
bool insert; /* Whether or not the last action was inserting a new character */
std::string search_buf; //! The search buffer
std::vector<searchMatch> search_matches; //! The set of search matches in our history
size_t search_index; //! The current match index
};
} // namespace duckdb

View File

@@ -0,0 +1,122 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// terminal.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
#include "duckdb/common/common.hpp"
namespace duckdb {
enum KEY_ACTION {
KEY_NULL = 0, /* NULL */
CTRL_A = 1, /* Ctrl+a */
CTRL_B = 2, /* Ctrl-b */
CTRL_C = 3, /* Ctrl-c */
CTRL_D = 4, /* Ctrl-d */
CTRL_E = 5, /* Ctrl-e */
CTRL_F = 6, /* Ctrl-f */
CTRL_G = 7, /* Ctrl-g */
CTRL_H = 8, /* Ctrl-h */
TAB = 9, /* Tab */
CTRL_J = 10, /* Ctrl+j*/
CTRL_K = 11, /* Ctrl+k */
CTRL_L = 12, /* Ctrl+l */
ENTER = 13, /* Enter */
CTRL_N = 14, /* Ctrl-n */
CTRL_O = 15, /* Ctrl-O */
CTRL_P = 16, /* Ctrl-p */
CTRL_R = 18, /* Ctrl-r */
CTRL_S = 19, /* Ctrl-s */
CTRL_T = 20, /* Ctrl-t */
CTRL_U = 21, /* Ctrl+u */
CTRL_W = 23, /* Ctrl+w */
CTRL_X = 24, /* Ctrl+x */
CTRL_Y = 25, /* Ctrl+y */
CTRL_Z = 26, /* Ctrl+z */
ESC = 27, /* Escape */
BACKSPACE = 127 /* Backspace */
};
enum class EscapeSequence {
INVALID = 0,
UNKNOWN = 1,
CTRL_MOVE_BACKWARDS,
CTRL_MOVE_FORWARDS,
HOME,
END,
UP,
DOWN,
RIGHT,
LEFT,
DELETE,
SHIFT_TAB,
ESCAPE,
ALT_A,
ALT_B,
ALT_C,
ALT_D,
ALT_E,
ALT_F,
ALT_G,
ALT_H,
ALT_I,
ALT_J,
ALT_K,
ALT_L,
ALT_M,
ALT_N,
ALT_O,
ALT_P,
ALT_Q,
ALT_R,
ALT_S,
ALT_T,
ALT_U,
ALT_V,
ALT_W,
ALT_X,
ALT_Y,
ALT_Z,
ALT_BACKSPACE,
ALT_LEFT_ARROW,
ALT_RIGHT_ARROW,
ALT_BACKSLASH,
};
struct TerminalSize {
int ws_col = 0;
int ws_row = 0;
};
class Terminal {
public:
static int IsUnsupportedTerm();
static int EnableRawMode();
static void DisableRawMode();
static bool IsMultiline();
static void SetMultiLine(int ml);
static void ClearScreen();
static void Beep();
static bool IsAtty();
static int HasMoreData(int fd);
static TerminalSize GetTerminalSize();
static char *EditNoTTY();
static int EditRaw(char *buf, size_t buflen, const char *prompt);
static EscapeSequence ReadEscapeSequence(int ifd);
private:
static TerminalSize TryMeasureTerminalSize();
static TerminalSize GetCursorPosition();
static idx_t ReadEscapeSequence(int ifd, char sequence[]);
};
} // namespace duckdb