搜尋套件、儲存庫與 APP 分類

interact 封面
套件詳情v2.2.0

interact

一組可自訂的互動式命令列元件。

macOSWindowsLinux
近 30 天下載
37萬
喜歡數
95
Pub 分數
140

安裝套件

flutter pub add interact

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Interact 是一個 Flutter/Dart 套件,提供一組可自訂的互動式命令列元件,用於建構功能豐富、使用者友善的 CLI 應用程式。它讓開發者能輕鬆建立提示、選單與輸入表單,支援驗證、預設值與即時回饋等功能。此套件專為 macOS、Windows 與 Linux 上的跨平台使用而設計,可無縫整合至基於 Dart 的 CLI 工具中。透過簡化互動式終端體驗的建立過程,無需處理底層終端操作,該套件顯著提升開發者的工作效率。

適用情境

  • 建構互動式 CLI 工具
  • 建立設定向導
  • 開發命令列配置工具
  • 自動化使用者輸入工作流程
  • 透過動態提示增強CLI使用者體驗
  • 支援多步驟終端互動

主要特色

  • 可自定義的提示組件
  • 輸入驗證和錯誤處理
  • 跨平台相容性
  • 即時用戶反饋
  • 支援預設值與建議值
  • 與 Flutter/Dart CLI 應用的簡單整合

適合對象

  • CLI 工具開發者
  • DevOps 自動化腳本
  • 互動式設定流程
  • 基於 Dart 的命令列應用程式
  • 尋求直覺式終端介面的使用者

使用建議

  • 需要終端環境
  • 僅限基於控制台的互動
  • 不適用於GUI應用程式
  • 依賴於平台特定的終端行為
  • 可能需要額外的樣式來適應複雜佈局

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Interact

A collection of customizable interactive command-line components.


Table of Contents


Overview

The library contains a bunch of command-line components that are easy to use and customizable, including text and password inputs, radio or select inputs, checkbox or multiple select inputs, spinners, and progress bars. Examples for all the available components can be found in the example folder, and the API Documentation section will cover all about them.

As an overview, you can make a Select component like this.

final languages = ['Rust', 'Dart', 'TypeScript'];
final selection = Select(
  prompt: 'Your favorite programming language',
  options: languages,
).interact();

print('${languages[selection]}');

It will result in something like this,


Installation

Install the latest version of interact as a dependency as shown in pub.dev.


API Documentation

Components

These are the snippets of components with their properties and arguments. Check the pub documentation to get to know more about them in detail.


Confirm Component

A confirm component asks the user for a simple yes or no and will return a boolean accordingly.

final answer = Confirm(
  prompt: 'Does it work?',
  defaultValue: true, // this is optional
  waitForNewLine: true, // optional and will be false by default
).interact();

If waitForNewLine is true, the prompt will wait for an Enter key from the user regardless of the answer.


Input Component

An input component asks the user for a string that could be validated.

final email = Input(
  prompt: 'Your email',
  defaultValue: '', // optional, will provide the user as a hint
  initialText: '', // optional, will be autofilled in the input
  validator: (String x) { // optional
    if (x.contains('@')) {
      return true;
    } else {
      throw ValidationError('Not a valid email');
    }
  },
).interact();

The message passed in the ValidationError exception will be shown as an error until the validator returns true.


Password Component

A password component behaves pretty much the same as an input component, but the user input will be hidden and by default, it has a repeat password validator that checks if two password inputs are the same or not.

final password = Password(
  prompt: 'Password',
  confirmation: true, // optional and will be false by default
  confirmPrompt: 'Repeat password', // optional
  confirmError: 'Passwords do not match' // optional
).interact();

Select Component

A select component asks the user to choose between the options supplied and the index of the chosen option will be returned.

final languages = ['Rust', 'Dart', 'TypeScript'];

final selection = Select(
  prompt: 'Your favorite programming language',
  options: languages,
  initialIndex: 2, // optional, will be 0 by default
).interact();

MultiSelect Component

A multi-select component asks the user for multiple options check by using the SpaceBar. Similarly, the multi-select component will return a list of selected indexes.

final answers = MultiSelect(
  prompt: 'Let me know your answers',
  options: ['A', 'B', 'C'],
  defaults: [false, true, false], // optional, will be all false by default
).interact();

Sort Component

A sort component asks the user to sort the given list of options and returns the list ordered by the user.

final sorted = Sort(
  prompt: 'Sort Tesla models from favorite to least',
  options: ['S', '3', 'X', 'Y'],
  showOutput: false, // optional, will be false by default
).interact();

Sometimes the list given can be massive, so setting the showOutput to false makes the list not be shown in the success prompt.


Spinner and MultiSpinner Components

A spinner will show a spinning indicator until the user calls it's done method. When it's done, it shows the icon given in place of the spinner.

final gift = Spinner(
  icon: '🏆',
  leftPrompt: (done) => '', // prompts are optional
  rightPrompt: (done) => done
      ? 'here is a trophy for being patient'
      : 'searching a thing for you',
).interact();

await Future.delayed(const Duration(seconds: 5));
gift.done();

Using multiple spinners at once is a common practice, but because of the way the library renders things, it's not possible to have multiple them normally. It will break the rendering process of all components.

final spinners = MultiSpinner();

final horse = spinners.add(Spinner(
  icon: '🐴',
  rightPrompt: (done) => done ? 'finished' : 'waiting',
)); // notice how you don't need to call the `.interact()` function

await Future.delayed(const Duration(seconds: 5));
horse.done();

Now you can have multiple of them without breaking things.


Progress and MultiProgress Components

A progress component shows a progress bar.

final progress = Progress(
  length: 1000, // required, the length of the progress to use
  size: 0.5, // optional, will be 1 by default
  rightPrompt: (current) => ' ${current.toString().padLeft(3)}/$length',
).interact();

for (var i = 0; i < 500; i++) {
  await Future.delayed(const Duration(milliseconds: 5));
  progress.increase(2);
}

progress.done();

The size is the multiplier for the rendered progress bar and it will be 1 by default. 0.5 means the rendered progress bar will be half the width of the terminal.

A MultiProgress does pretty much the same as what MultiSpinner did for spinners.

final bars = MultiProgress();

final p1 = bars.add(Progress(
  length: 100,
  rightPrompt: (current) => ' ${current.toString().padLeft(3)}/$length',
)); // notice how you don't need to call the `.interact()` function

for (var i = 0; i < 500; i++) {
  await Future.delayed(const Duration(milliseconds: 5));
  p1.increase(2);
}

p1.done();

Customizing Themes

Because most of the visually rendered parts come from the theme object which is available for all components, you can customize a lot of them by changing the theme. Changing a theme for a component can be done by using the withTheme constructor.

final progress = Progress.withTheme(
  theme: Theme(),
  length: length,
  rightPrompt: (current) => ' ${current.toString().padLeft(3)}/$length',
).interact();

The components used the Theme.defaultTheme as the theme by default. The Theme object has two premade themes,

  • Theme.colorfulTheme which is made with colorful ASCII/emojis
  • Theme.basicTheme which is mostly text characters and without colors

and the Theme.defaultTheme is the colorful theme by default.

Because constructing a theme from scrap requires you to write a lot of properties, it might be easier to extend existing themes to create a new one which can be done using the copyWith method.

import 'package:tint/tint.dart'; // for extension methods
// ...
Theme customTheme = Theme.colorfulTheme.copyWith(
  activeItemPrefix: '👉'
  activeItemStyle: (x) => x.yellow().underline(),
);

Technically, you can also override Theme.defaultTheme as a shortcut.

Handling Exceptions

If your program throw exceptions and exit midway, interact's components won't be able to finish their tasks and gracefully quit therefore causing certain problems like cursors not showing up, terminal colors got modified etc. To fix these problems you should always try to catch exceptions and reset to terminal defaults using reset function.

try {
  Spinner(icon: '🚨').interact();
  throw Exception(); // spinner couldn't finished
} catch (e) {
  reset(); // Reset everything to terminal defaults
  rethrow;
}

Acknowledgement

This library is mostly inspired by dialouger library from Rust. The lack of a properly maintained library for Dart with a well-made API is what pushed me into making this library.

所屬分類

依賴

依賴清單 3 項

原始碼儲存庫

使用此套件的專案