Skip to content

Commit

Permalink
Add shortcuts for copy, paste & scrolling (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpnurmi committed Jun 20, 2022
1 parent 6b78e72 commit 9e5f185
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
13 changes: 13 additions & 0 deletions packages/terminal_view/lib/src/terminal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Terminal extends ChangeNotifier {
maxLines ?? 10000, // TODO
theme?.palette.toXtermTheme() ?? xterm.TerminalThemes.defaultTheme,
);
_xterm!.setBracketedPasteMode(false);
}
return _xterm!;
}
Expand Down Expand Up @@ -72,6 +73,18 @@ class Terminal extends ChangeNotifier {
}

void selectAll() => _xterm?.selectAll();

int get _bufferHeight => _xterm?.bufferHeight ?? 0;
int get _pageHeight => _xterm?.viewHeight ?? 0;
int get _scrollOffset => _xterm?.scrollOffsetFromBottom ?? 0;
void _scrollTo(int offset) => _xterm?.setScrollOffsetFromBottom(offset);

void scrollUp() => _scrollTo(_scrollOffset + 1);
void scrollDown() => _scrollTo(_scrollOffset - 1);
void scrollPageUp() => _scrollTo(_scrollOffset + _pageHeight);
void scrollPageDown() => _scrollTo(_scrollOffset - _pageHeight);
void scrollToTop() => _scrollTo(_bufferHeight - _pageHeight);
void scrollToBottom() => _scrollTo(0);
}

extension _XtermTheme on TerminalPalette {
Expand Down
55 changes: 38 additions & 17 deletions packages/terminal_view/lib/src/terminal_view.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xterm/flutter.dart' as xterm;
import 'package:xterm/xterm.dart' as xterm;

Expand All @@ -13,23 +14,43 @@ class TerminalView extends StatelessWidget {
@override
Widget build(BuildContext context) {
final theme = TerminalTheme.maybeOf(context);
return FocusScope(
child: AnimatedBuilder(
animation: terminal,
builder: (context, child) {
return xterm.TerminalView(
padding: 2,
autofocus: true,
terminal: terminal.buildXterm(theme: theme),
style: xterm.TerminalStyle(
fontSize: theme?.fontSize ?? 16,
fontFamily: [
if (theme != null) theme.fontFamily,
...xterm.TerminalStyle.defaultFontFamily,
],
),
);
},
return CallbackShortcuts(
bindings: {
const SingleActivator(LogicalKeyboardKey.keyC,
control: true, shift: true): terminal.copy,
const SingleActivator(LogicalKeyboardKey.keyV,
control: true, shift: true): terminal.paste,
const SingleActivator(LogicalKeyboardKey.arrowUp,
control: true, shift: true): terminal.scrollUp,
const SingleActivator(LogicalKeyboardKey.arrowDown,
control: true, shift: true): terminal.scrollDown,
const SingleActivator(LogicalKeyboardKey.pageUp, shift: true):
terminal.scrollPageUp,
const SingleActivator(LogicalKeyboardKey.pageDown, shift: true):
terminal.scrollPageDown,
const SingleActivator(LogicalKeyboardKey.home, shift: true):
terminal.scrollToTop,
const SingleActivator(LogicalKeyboardKey.end, shift: true):
terminal.scrollToBottom,
},
child: FocusScope(
child: AnimatedBuilder(
animation: terminal,
builder: (context, child) {
return xterm.TerminalView(
padding: 2,
autofocus: true,
terminal: terminal.buildXterm(theme: theme),
style: xterm.TerminalStyle(
fontSize: theme?.fontSize ?? 16,
fontFamily: [
if (theme != null) theme.fontFamily,
...xterm.TerminalStyle.defaultFontFamily,
],
),
);
},
),
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/terminal_view/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies:
xterm: #^2.6.0
git:
url: https://github.com/jpnurmi/xterm.dart
ref: flutter-3
ref: dev

dev_dependencies:
flutter_lints: ^2.0.0
Expand Down

0 comments on commit 9e5f185

Please sign in to comment.