Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "scrolling" support for Select and MultiSelect menus #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/src/multi_select.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:dart_console/dart_console.dart';
import 'package:interact/src/framework/framework.dart';
import 'package:interact/src/theme/theme.dart';
Expand Down Expand Up @@ -40,6 +42,7 @@ class MultiSelect extends Component<List<int>> {
class _MultiSelectState extends State<MultiSelect> {
late List<int> selection;
late int index;
int offset = 0;

@override
void init() {
Expand Down Expand Up @@ -97,9 +100,20 @@ class _MultiSelectState extends State<MultiSelect> {
super.dispose();
}

int maxLength() {
final lines = stdout.terminalLines - 3;
return lines > component.options.length ? component.options.length : lines;
}

@override
void render() {
for (var i = 0; i < component.options.length; i++) {
final length = maxLength();
if (index >= offset + length) {
offset = index - length + 1;
} else if (index < offset) {
offset = index;
}
for (var i = offset; i < offset + length; i++) {
final option = component.options[i];
final line = StringBuffer();

Expand Down
16 changes: 15 additions & 1 deletion lib/src/select.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:dart_console/dart_console.dart';
import 'package:interact/src/framework/framework.dart';
import 'package:interact/src/theme/theme.dart';
Expand Down Expand Up @@ -41,6 +43,7 @@ class Select extends Component<int> {

class _SelectState extends State<Select> {
int index = 0;
int offset = 0;

@override
void init() {
Expand Down Expand Up @@ -79,9 +82,20 @@ class _SelectState extends State<Select> {
super.dispose();
}

int maxLength() {
final lines = stdout.terminalLines - 3;
return lines > component.options.length ? component.options.length : lines;
}

@override
void render() {
for (var i = 0; i < component.options.length; i++) {
final length = maxLength();
if (index >= offset + length) {
offset = index - length + 1;
} else if (index < offset) {
offset = index;
}
for (var i = offset; i < offset + length; i++) {
final option = component.options[i];
final line = StringBuffer();

Expand Down