LineBuffer.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * LineBuffer.h - support for buffering/editing a line of text
8  *
9  * author mikee47 <mike@sillyhouse.net> Feb 2019
10  *
11  ****/
12 
13 #pragma once
14 
15 #include <cstdint>
16 #include <cstring>
17 #include <WString.h>
19 
25 {
26 public:
27  LineBufferBase(char* buffer, uint16_t size) : buffer(buffer), size(size)
28  {
29  }
30 
34  enum class Action {
35  none,
36  clear,
37  echo,
38  backspace,
39  submit,
40  };
41 
49 
56  Action processKey(char key, ReadWriteStream* output = nullptr);
57 
62  char addChar(char c);
63 
67  void clear()
68  {
69  length = 0;
70  }
71 
72  explicit operator bool() const
73  {
74  return length != 0;
75  }
76 
81  explicit operator String() const
82  {
83  return length ? String(buffer, length) : nullptr;
84  }
85 
89  char* getBuffer()
90  {
91  return buffer;
92  }
93 
97  unsigned getLength() const
98  {
99  return length;
100  }
101 
107  bool startsWith(const char* text) const;
108 
114  bool contains(const char* text) const;
115 
120  bool backspace();
121 
122  size_t printTo(Print& p) const
123  {
124  return p.write(buffer, length);
125  }
126 
127 private:
128  char* buffer;
129  uint16_t size;
130  uint16_t length{0};
131  char previousKey{'\0'};
132 };
133 
138 template <uint16_t BUFSIZE> class LineBuffer : public LineBufferBase
139 {
140 public:
141  LineBuffer() : LineBufferBase(buffer, BUFSIZE)
142  {
143  }
144 
145 private:
146  char buffer[BUFSIZE]{};
147 };
void size_t const void * key
Definition: blake2s.h:33
Class to enable buffering of a single line of text, with simple editing.
Definition: LineBuffer.h:25
bool backspace()
Remove last character from buffer.
char addChar(char c)
Add a character to the buffer.
unsigned getLength() const
Get number of characters in the text line.
Definition: LineBuffer.h:97
Action process(Stream &input, ReadWriteStream &output)
Process all available data from input
Action processKey(char key, ReadWriteStream *output=nullptr)
Process a keypress in a consistent manner for console editing.
bool startsWith(const char *text) const
Check for matching text at start of line, case-sensitive.
Action
Returned from processKey method directing caller.
Definition: LineBuffer.h:34
@ clear
Line is cleared: typically perform a carriage return.
@ none
Do nothing, ignore the key.
@ backspace
Perform backspace edit, e.g. output "\b \b".
@ submit
User hit return, process line and clear it.
@ echo
Key should be echoed.
void clear()
Clear contents of buffer.
Definition: LineBuffer.h:67
char * getBuffer()
Get the text, nul-terminated.
Definition: LineBuffer.h:89
bool contains(const char *text) const
Check for matching text anywhere in line, case-sensitive.
LineBufferBase(char *buffer, uint16_t size)
Definition: LineBuffer.h:27
size_t printTo(Print &p) const
Definition: LineBuffer.h:122
Class to enable buffering of a single line of text, with simple editing.
Definition: LineBuffer.h:139
LineBuffer()
Definition: LineBuffer.h:141
Provides formatted output to stream.
Definition: Print.h:37
virtual size_t write(uint8_t c)=0
Writes a single character to output stream.
Base class for read/write stream.
Definition: ReadWriteStream.h:20
Base Stream class.
Definition: Wiring/Stream.h:33
The String class.
Definition: WString.h:137