xref: /haiku/src/apps/terminal/HistoryBuffer.h (revision f2b4344867e97c3f4e742a1b4a15e6879644601a)
1 /*
2  * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef HISTORY_BUFFER_H
6 #define HISTORY_BUFFER_H
7 
8 #include <SupportDefs.h>
9 
10 #include "TerminalLine.h"
11 
12 
13 struct HistoryLine;
14 struct TerminalLine;
15 
16 
17 class HistoryBuffer {
18 public:
19 								HistoryBuffer();
20 								~HistoryBuffer();
21 
22 			status_t			Init(int32 width, int32 capacity);
23 
24 			void				Clear();
25 
26 			int32				Width() const		{ return fWidth; }
27 			int32				Capacity() const	{ return fCapacity; }
28 			int32				Size() const		{ return fSize; }
29 
30 	inline	HistoryLine*		LineAt(int32 index) const;
31 			TerminalLine*		GetTerminalLineAt(int32 index,
32 									TerminalLine* buffer) const;
33 
34 			void				AddLine(const TerminalLine* line);
35 			void				AddEmptyLines(int32 count);
36 			void				DropLines(int32 count);
37 
38 private:
39 			HistoryLine*		_AllocateLine(int32 attributesRuns,
40 									int32 byteLength);
41 	inline	HistoryLine*		_LineAt(int32 index) const;
42 
43 private:
44 			HistoryLine*		fLines;
45 			int32				fWidth;
46 			int32				fCapacity;
47 			int32				fNextLine;
48 			int32				fSize;
49 			uint8*				fBuffer;
50 			int32				fBufferSize;
51 			int32				fBufferAllocationOffset;
52 };
53 
54 
55 inline HistoryLine*
56 HistoryBuffer::_LineAt(int32 index) const
57 {
58 	return &fLines[(fCapacity + fNextLine - index - 1) % fCapacity];
59 }
60 
61 
62 inline HistoryLine*
63 HistoryBuffer::LineAt(int32 index) const
64 {
65 	return index >= 0 && index < fSize ? _LineAt(index) : NULL;
66 }
67 
68 
69 #endif	// HISTORY_BUFFER_H
70