linenoise.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* linenoise.h -- VERSION 1.0
  2. *
  3. * Guerrilla line editing library against the idea that a line editing lib
  4. * needs to be 20,000 lines of C++ code.
  5. *
  6. * See linenoise.cpp for more information.
  7. *
  8. * ------------------------------------------------------------------------
  9. *
  10. * Copyright (c) 2010-2023, Salvatore Sanfilippo <antirez at gmail dot com>
  11. * Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
  12. * Copyright (c) 2025, Eric Curtin <ericcurtin17 at gmail dot com>
  13. *
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions are
  18. * met:
  19. *
  20. * * Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions and the following disclaimer.
  22. *
  23. * * Redistributions in binary form must reproduce the above copyright
  24. * notice, this list of conditions and the following disclaimer in the
  25. * documentation and/or other materials provided with the distribution.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. #ifndef __LINENOISE_H
  40. #define __LINENOISE_H
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. #include <stddef.h> /* For size_t. */
  45. #include <stdlib.h>
  46. extern const char *linenoiseEditMore;
  47. /* The linenoiseState structure represents the state during line editing.
  48. * We pass this state to functions implementing specific editing
  49. * functionalities. */
  50. struct linenoiseState {
  51. int in_completion; /* The user pressed TAB and we are now in completion
  52. * mode, so input is handled by completeLine(). */
  53. size_t completion_idx; /* Index of next completion to propose. */
  54. int ifd; /* Terminal stdin file descriptor. */
  55. int ofd; /* Terminal stdout file descriptor. */
  56. char *buf; /* Edited line buffer. */
  57. size_t buflen; /* Edited line buffer size. */
  58. const char *prompt; /* Prompt to display. */
  59. size_t plen; /* Prompt length. */
  60. size_t pos; /* Current cursor position. */
  61. size_t oldpos; /* Previous refresh cursor position. */
  62. size_t len; /* Current edited line length. */
  63. size_t cols; /* Number of columns in terminal. */
  64. size_t oldrows; /* Rows used by last refrehsed line (multiline mode) */
  65. int history_index; /* The history index we are currently editing. */
  66. };
  67. struct linenoiseCompletions {
  68. size_t len = 0;
  69. char ** cvec = nullptr;
  70. bool to_free = true;
  71. ~linenoiseCompletions() {
  72. if (!to_free) {
  73. return;
  74. }
  75. for (size_t i = 0; i < len; ++i) {
  76. free(cvec[i]);
  77. }
  78. free(cvec);
  79. }
  80. };
  81. /* Non blocking API. */
  82. int linenoiseEditStart(struct linenoiseState *l, int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt);
  83. const char *linenoiseEditFeed(struct linenoiseState *l);
  84. void linenoiseEditStop(struct linenoiseState *l);
  85. void linenoiseHide(struct linenoiseState *l);
  86. void linenoiseShow(struct linenoiseState *l);
  87. /* Blocking API. */
  88. const char *linenoise(const char *prompt);
  89. void linenoiseFree(void *ptr);
  90. /* Completion API. */
  91. typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);
  92. typedef const char*(linenoiseHintsCallback)(const char *, int *color, int *bold);
  93. typedef void(linenoiseFreeHintsCallback)(const char *);
  94. void linenoiseSetCompletionCallback(linenoiseCompletionCallback *);
  95. void linenoiseSetHintsCallback(linenoiseHintsCallback *);
  96. void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *);
  97. void linenoiseAddCompletion(linenoiseCompletions *, const char *);
  98. /* History API. */
  99. int linenoiseHistoryAdd(const char *line);
  100. int linenoiseHistorySetMaxLen(int len);
  101. int linenoiseHistorySave(const char *filename);
  102. int linenoiseHistoryLoad(const char *filename);
  103. /* Other utilities. */
  104. void linenoiseClearScreen(void);
  105. void linenoiseSetMultiLine(int ml);
  106. void linenoisePrintKeyCodes(void);
  107. void linenoiseMaskModeEnable(void);
  108. void linenoiseMaskModeDisable(void);
  109. #ifdef __cplusplus
  110. }
  111. #endif
  112. #endif /* __LINENOISE_H */