eterm: Improved history buffer allocation.

This commit is contained in:
Martín Lucas Golini
2022-06-26 04:10:39 -03:00
parent 716eaea2c0
commit 983f0491e2
5 changed files with 48 additions and 9 deletions

View File

@@ -96,7 +96,7 @@ EE_MAIN_FUNC int main( int, char*[] ) {
if ( !terminal || terminal->hasTerminated() ) {
terminal = TerminalDisplay::create( win, fontMono, PixelDensity::dpToPx( 11 ),
win->getSize().asFloat(), "", {}, "", 10000 );
win->getSize().asFloat() );
}
win->getInput()->pushCallback( &inputCallback );

View File

@@ -63,7 +63,10 @@ static TerminalShortcut shortcuts[] = {
{ KEY_PAGEUP, KEYMOD_SHIFT, TerminalShortcutAction::SCROLLUP_SCREEN, 0, 0, -1 },
{ KEY_PAGEDOWN, KEYMOD_SHIFT, TerminalShortcutAction::SCROLLDOWN_SCREEN, 0, 0, -1 },
{ KEY_UP, KEYMOD_SHIFT, TerminalShortcutAction::SCROLLUP_ROW, 0, 0, -1 },
{ KEY_DOWN, KEYMOD_SHIFT, TerminalShortcutAction::SCROLLDOWN_ROW, 0, 0, -1 } };
{ KEY_DOWN, KEYMOD_SHIFT, TerminalShortcutAction::SCROLLDOWN_ROW, 0, 0, -1 },
{ KEY_HOME, KEYMOD_SHIFT, TerminalShortcutAction::SCROLLUP_HISTORY, 0, 0, -1 },
{ KEY_END, KEYMOD_SHIFT, TerminalShortcutAction::SCROLLDOWN_HISTORY, 0, 0, -1 },
};
static TerminalMouseShortcut mouseShortcuts[] = {
{ EE_BUTTON_WUMASK, KEYMOD_SHIFT, TerminalShortcutAction::SCROLLUP_SCREEN, 0, 0, -1 },
@@ -542,6 +545,16 @@ void TerminalDisplay::action( TerminalShortcutAction action ) {
mTerminal->kscrolldown( &arg );
break;
}
case TerminalShortcutAction::SCROLLUP_HISTORY: {
TerminalArg arg( (int)INT_MAX );
mTerminal->kscrollup( &arg );
break;
}
case TerminalShortcutAction::SCROLLDOWN_HISTORY: {
TerminalArg arg( (int)INT_MAX );
mTerminal->kscrolldown( &arg );
break;
}
}
}

View File

@@ -27,7 +27,9 @@ enum class TerminalShortcutAction {
SCROLLUP_ROW,
SCROLLDOWN_ROW,
SCROLLUP_SCREEN,
SCROLLDOWN_SCREEN
SCROLLDOWN_SCREEN,
SCROLLUP_HISTORY,
SCROLLDOWN_HISTORY
};
struct TerminalKey {
@@ -131,7 +133,7 @@ class TerminalDisplay : public ITerminalDisplay {
static std::shared_ptr<TerminalDisplay>
create( EE::Window::Window* window, Font* font, const Float& fontSize, const Sizef& pixelsSize,
std::string program = "", const std::vector<std::string>& args = {},
const std::string& workingDir = "", const size_t& historySize = 1000,
const std::string& workingDir = "", const size_t& historySize = 10000,
IProcessFactory* processFactory = nullptr );
virtual void resetColors();

View File

@@ -107,7 +107,7 @@ static const unsigned int tabspaces = 4;
#define ISCONTROL( c ) ( ISCONTROLC0( c ) || ISCONTROLC1( c ) )
#define ISDELIM( u ) ( u && _wcschr( worddelimiters, u ) )
#define TLINE( y ) \
( ( y ) < mTerm.scr \
( ( y ) < mTerm.scr && mTerm.histsize > 0 \
? mTerm.hist[( ( y ) + mTerm.histi - mTerm.scr + mTerm.histsize + 1 ) % mTerm.histsize] \
: mTerm.line[(y)-mTerm.scr] )
@@ -642,6 +642,9 @@ size_t TerminalEmulator::ttyread( void ) {
void TerminalEmulator::kscrolldown( const TerminalArg* a ) {
int n = a->i;
if ( n == INT_MAX )
n = mTerm.scr;
if ( n < 0 )
n = mTerm.row + n;
@@ -658,6 +661,9 @@ void TerminalEmulator::kscrolldown( const TerminalArg* a ) {
void TerminalEmulator::kscrollup( const TerminalArg* a ) {
int n = a->i;
if ( n == INT_MAX )
n = mTerm.histi - mTerm.scr;
if ( n < 0 )
n = mTerm.row + n;
@@ -824,13 +830,30 @@ void TerminalEmulator::tswapscreen( void ) {
tfulldirt();
}
void TerminalEmulator::resizeHistory() {
size_t oriSize = mTerm.hist.size();
if ( mTerm.histi >= (int)mTerm.hist.size() ) {
int newSize = eemin( mTerm.histi + mTerm.row, mTerm.histsize );
mTerm.hist.resize( newSize, nullptr );
for ( size_t i = oriSize; i < mTerm.hist.size(); i++ ) {
mTerm.hist[i] =
(TerminalGlyph*)xrealloc( mTerm.hist[i], mTerm.col * sizeof( TerminalGlyph ) );
for ( int j = 0; j < mTerm.col; j++ ) {
mTerm.hist[i][j] = mTerm.c.attr;
mTerm.hist[i][j].u = ' ';
}
}
}
}
void TerminalEmulator::tscrolldown( int orig, int n, int copyhist ) {
int i;
Line temp;
LIMIT( n, 0, mTerm.bot - orig + 1 );
if ( copyhist ) {
if ( copyhist && mTerm.histsize > 0 ) {
mTerm.histi = ( mTerm.histi - 1 + mTerm.histsize ) % mTerm.histsize;
resizeHistory();
temp = mTerm.hist[mTerm.histi];
mTerm.hist[mTerm.histi] = mTerm.line[mTerm.bot];
mTerm.line[mTerm.bot] = temp;
@@ -855,8 +878,9 @@ void TerminalEmulator::tscrollup( int orig, int n, int copyhist ) {
LIMIT( n, 0, mTerm.bot - orig + 1 );
if ( copyhist ) {
if ( copyhist && mTerm.histsize > 0 ) {
mTerm.histi = ( mTerm.histi + 1 ) % mTerm.histsize;
resizeHistory();
temp = mTerm.hist[mTerm.histi];
mTerm.hist[mTerm.histi] = mTerm.line[orig];
mTerm.line[orig] = temp;
@@ -2242,8 +2266,7 @@ void TerminalEmulator::tresize( int col, int row ) {
mTerm.dirty = (int*)xrealloc( mTerm.dirty, row * sizeof( *mTerm.dirty ) );
mTerm.tabs = (int*)xrealloc( mTerm.tabs, col * sizeof( *mTerm.tabs ) );
mTerm.hist.resize( mTerm.histsize, nullptr );
for ( i = 0; i < mTerm.histsize; i++ ) {
for ( size_t i = 0; i < mTerm.hist.size(); i++ ) {
mTerm.hist[i] = (TerminalGlyph*)xrealloc( mTerm.hist[i], col * sizeof( TerminalGlyph ) );
for ( j = mincol; j < col; j++ ) {
mTerm.hist[i][j] = mTerm.c.attr;

View File

@@ -226,6 +226,7 @@ class TerminalEmulator final {
int mAllowAltScreen;
int mAllowWindowOps;
void resizeHistory();
void setClipboard( const char* str );
void loadColors();