chore(cli): add new package
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:wyatt_cli_toolbox/src/sequences/sequences.dart';
|
||||
|
||||
/// The [frames] field holds each individual frame of an animation, whilst the
|
||||
/// [interval] represents the sleep time between each frame.
|
||||
class Indicator {
|
||||
|
||||
Indicator({
|
||||
required this.name,
|
||||
required this.interval,
|
||||
required this.frames,
|
||||
this.label,
|
||||
});
|
||||
|
||||
factory Indicator.fromMap(Map<String, Map<String, Object>> map) {
|
||||
final String name = map.keys.first;
|
||||
return Indicator(
|
||||
name: name,
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
interval: map[name]!['interval'] as int,
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
frames: List<String>.from(map[name]!['frames'] as List<String>),
|
||||
);
|
||||
}
|
||||
|
||||
final String name;
|
||||
final int interval;
|
||||
final List<String> frames;
|
||||
final String? label;
|
||||
Timer? _timer;
|
||||
|
||||
|
||||
Map<String, Map<String, Object>> toMap() {
|
||||
return <String, Map<String, Object>>{
|
||||
name: <String, Object>{
|
||||
'interval': interval,
|
||||
'frames': frames,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/// Animate the spinner.
|
||||
void animate() {
|
||||
stop();
|
||||
int frame = 0;
|
||||
_timer = Timer.periodic(Duration(milliseconds: interval), (Timer t) {
|
||||
Sequences.clearEntireLine();
|
||||
frame = (frame + 1) % frames.length;
|
||||
if (label != null) {
|
||||
stdout.write('${frames[frame]} $label');
|
||||
} else {
|
||||
stdout.write(frames[frame]);
|
||||
}
|
||||
Sequences.moveHorizontal(1);
|
||||
});
|
||||
}
|
||||
|
||||
/// Stop the spinner (but don't kill isolate)
|
||||
void stop() {
|
||||
Sequences.clearEntireLine();
|
||||
_timer?.cancel();
|
||||
}
|
||||
|
||||
Indicator copyWith({
|
||||
String? name,
|
||||
int? interval,
|
||||
List<String>? frames,
|
||||
String? label,
|
||||
}) {
|
||||
return Indicator(
|
||||
name: name ?? this.name,
|
||||
interval: interval ?? this.interval,
|
||||
frames: frames ?? this.frames,
|
||||
label: label ?? this.label,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() =>
|
||||
'Indicator(name: $name, interval: $interval, frames: $frames)';
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:isolate';
|
||||
|
||||
import 'package:wyatt_cli_toolbox/src/sequences/sequences.dart';
|
||||
import 'package:wyatt_cli_toolbox/src/spinners/indicator.dart';
|
||||
import 'package:wyatt_cli_toolbox/src/spinners/spinner_type.dart';
|
||||
|
||||
|
||||
class Spinner {
|
||||
/// Active isolate object. Is created when [start] is called.
|
||||
static Isolate? _spinnerIsolate;
|
||||
|
||||
/// Run the animation of active [_spinnerIsolate].
|
||||
static Future<void> _animate(Indicator indicator) async {
|
||||
indicator.animate();
|
||||
}
|
||||
|
||||
/// Starts an animation on a new line.
|
||||
static Future<Isolate> start(SpinnerType spinner, {String? text}) async {
|
||||
return custom(spinner.indicator.copyWith(label: text));
|
||||
}
|
||||
|
||||
/// Starts a custom animation on a new line.
|
||||
static Future<Isolate> custom(Indicator indicator) async {
|
||||
Sequences.hideCursor();
|
||||
return _spinnerIsolate ??
|
||||
(_spinnerIsolate = await Isolate.spawn(
|
||||
_animate,
|
||||
indicator,
|
||||
));
|
||||
}
|
||||
|
||||
/// Stops current spinner animation by killing the isolate instance,
|
||||
/// and clears current line.
|
||||
static void stop() {
|
||||
if (_spinnerIsolate != null) {
|
||||
_spinnerIsolate!.kill(priority: Isolate.immediate);
|
||||
}
|
||||
_spinnerIsolate = null;
|
||||
Sequences.clearEntireLine();
|
||||
Sequences.moveHorizontal(1);
|
||||
Sequences.unhideCursor();
|
||||
}
|
||||
|
||||
/// Stop and restart a new indicator on same line.
|
||||
static void update(Indicator indicator) {
|
||||
stop();
|
||||
custom(indicator);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:wyatt_cli_toolbox/src/spinners/spinners.dart';
|
||||
|
||||
enum SpinnerType {
|
||||
dots,
|
||||
dots2,
|
||||
dots3,
|
||||
dots4,
|
||||
dots5,
|
||||
dots6,
|
||||
dots7,
|
||||
dots8,
|
||||
dots9,
|
||||
dots10,
|
||||
dots11,
|
||||
dots12,
|
||||
dots8Bit,
|
||||
line,
|
||||
line2,
|
||||
pipe,
|
||||
simpleDots,
|
||||
simpleDotsScrolling,
|
||||
star,
|
||||
star2,
|
||||
flip,
|
||||
hamburger,
|
||||
growVertical,
|
||||
growHorizontal,
|
||||
balloon,
|
||||
balloon2,
|
||||
noise,
|
||||
bounce,
|
||||
boxBounce,
|
||||
boxBounce2,
|
||||
triangle,
|
||||
arc,
|
||||
circle,
|
||||
squareCorners,
|
||||
circleQuarters,
|
||||
circleHalves,
|
||||
squish,
|
||||
toggle,
|
||||
toggle2,
|
||||
toggle3,
|
||||
toggle4,
|
||||
toggle5,
|
||||
toggle6,
|
||||
toggle7,
|
||||
toggle8,
|
||||
toggle9,
|
||||
toggle10,
|
||||
toggle11,
|
||||
toggle12,
|
||||
toggle13,
|
||||
arrow,
|
||||
arrow2,
|
||||
arrow3,
|
||||
bouncingBar,
|
||||
bouncingBall,
|
||||
smiley,
|
||||
monkey,
|
||||
hearts,
|
||||
clock,
|
||||
earth,
|
||||
material,
|
||||
moon,
|
||||
runner,
|
||||
pong,
|
||||
shark,
|
||||
dqpb,
|
||||
weather,
|
||||
christmas,
|
||||
grenade,
|
||||
point,
|
||||
layer,
|
||||
betaWave,
|
||||
fingerDance,
|
||||
fistBump,
|
||||
soccerHeader,
|
||||
mindblown,
|
||||
speaker,
|
||||
orangePulse,
|
||||
bluePulse,
|
||||
orangeBluePulse,
|
||||
timeTravel,
|
||||
aesthetic;
|
||||
|
||||
String get name => toString().replaceAll(RegExp('SpinnerType.'), '');
|
||||
Indicator get indicator {
|
||||
if (spinners.keys.contains(name)) {
|
||||
return Indicator.fromMap(
|
||||
<String, Map<String, Object>>{name: spinners[name]!},
|
||||
);
|
||||
} else {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user