linenoise.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. extern const char *linenoiseEditMore;
  46. /* The linenoiseState structure represents the state during line editing.
  47. * We pass this state to functions implementing specific editing
  48. * functionalities. */
  49. struct linenoiseState {
  50. int in_completion; /* The user pressed TAB and we are now in completion
  51. * mode, so input is handled by completeLine(). */
  52. size_t completion_idx; /* Index of next completion to propose. */
  53. int ifd; /* Terminal stdin file descriptor. */
  54. int ofd; /* Terminal stdout file descriptor. */
  55. char *buf; /* Edited line buffer. */
  56. size_t buflen; /* Edited line buffer size. */
  57. const char *prompt; /* Prompt to display. */
  58. size_t plen; /* Prompt length. */
  59. size_t pos; /* Current cursor position. */
  60. size_t oldpos; /* Previous refresh cursor position. */
  61. size_t len; /* Current edited line length. */
  62. size_t cols; /* Number of columns in terminal. */
  63. size_t oldrows; /* Rows used by last refrehsed line (multiline mode) */
  64. int history_index; /* The history index we are currently editing. */
  65. };
  66. typedef struct linenoiseCompletions {
  67. size_t len;
  68. char **cvec;
  69. } linenoiseCompletions;
  70. /* Non blocking API. */
  71. int linenoiseEditStart(struct linenoiseState *l, int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt);
  72. const char *linenoiseEditFeed(struct linenoiseState *l);
  73. void linenoiseEditStop(struct linenoiseState *l);
  74. void linenoiseHide(struct linenoiseState *l);
  75. void linenoiseShow(struct linenoiseState *l);
  76. /* Blocking API. */
  77. const char *linenoise(const char *prompt);
  78. void linenoiseFree(void *ptr);
  79. /* Completion API. */
  80. typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);
  81. typedef const char*(linenoiseHintsCallback)(const char *, int *color, int *bold);
  82. typedef void(linenoiseFreeHintsCallback)(const char *);
  83. void linenoiseSetCompletionCallback(linenoiseCompletionCallback *);
  84. void linenoiseSetHintsCallback(linenoiseHintsCallback *);
  85. void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *);
  86. void linenoiseAddCompletion(linenoiseCompletions *, const char *);
  87. /* History API. */
  88. int linenoiseHistoryAdd(const char *line);
  89. int linenoiseHistorySetMaxLen(int len);
  90. int linenoiseHistorySave(const char *filename);
  91. int linenoiseHistoryLoad(const char *filename);
  92. /* Other utilities. */
  93. void linenoiseClearScreen(void);
  94. void linenoiseSetMultiLine(int ml);
  95. void linenoisePrintKeyCodes(void);
  96. void linenoiseMaskModeEnable(void);
  97. void linenoiseMaskModeDisable(void);
  98. #ifdef __cplusplus
  99. }
  100. #endif
  101. #endif /* __LINENOISE_H */