opt.: add tip (#380 #382)

This commit is contained in:
lollipopkit
2024-06-10 21:29:41 +08:00
parent 490d71f8c9
commit 3bc03c1364
13 changed files with 47 additions and 16 deletions

View File

@@ -115,10 +115,10 @@ class Snippet implements TagPickable {
final key = argsFmted.substring(start, end).toLowerCase();
// Special funcs
final special = _find(specialCtrl, key);
final special = _find(SnippetFuncs.specialCtrl, key);
if (special != null) {
final raw = key.substring(special.key.length + 1, key.length - 1);
await special.value(raw);
await special.value((term: terminal, raw: raw));
}
// Term keys
@@ -152,15 +152,17 @@ class Snippet implements TagPickable {
// `${ctrl+ad}` -> `ctrla + d`
final chars = key.substring(termKey.key.length + 1, key.length - 1);
terminal.charInput(
if (chars.isEmpty) return;
final ok = terminal.charInput(
chars.codeUnitAt(0),
ctrl: ctrlAlt.ctrl,
alt: ctrlAlt.alt,
);
for (final char in chars.codeUnits.skip(1)) {
terminal.charInput(char, ctrl: false, alt: false);
if (!ok) {
Loggers.app.warning('Failed to input: $key');
}
terminal.textInput(chars.substring(1));
}
MapEntry<String, T>? _find<T>(Map<String, T> map, String key) {
@@ -180,12 +182,6 @@ class Snippet implements TagPickable {
static final fmtTermKeys = {
r'${ctrl': TerminalKey.control,
r'${alt': TerminalKey.alt,
r'${enter': TerminalKey.enter,
};
static final specialCtrl = {
// `${sleep 3}` -> sleep 3 seconds
r'${sleep': SnippetFuncs.sleep,
};
}
@@ -201,11 +197,31 @@ class SnippetResult {
});
}
typedef SnippetFuncCtx = ({Terminal term, String raw});
abstract final class SnippetFuncs {
static FutureOr<void> sleep(String raw) async {
final seconds = int.tryParse(raw);
static final specialCtrl = {
// `${sleep 3}` -> sleep 3 seconds
r'${sleep': SnippetFuncs.sleep,
r'${enter': SnippetFuncs.enter,
};
static const help = {
'sleep': 'Sleep for a few seconds',
'enter': 'Enter a few times',
};
static FutureOr<void> sleep(SnippetFuncCtx ctx) async {
final seconds = int.tryParse(ctx.raw);
if (seconds == null) return;
final duration = Duration(seconds: seconds);
await Future.delayed(duration);
}
static FutureOr<void> enter(SnippetFuncCtx ctx) async {
final times = int.tryParse(ctx.raw) ?? 1;
for (var i = 0; i < times; i++) {
ctx.term.keyInput(TerminalKey.enter);
}
}
}

View File

@@ -112,6 +112,7 @@
"followSystem": "System verfolgen",
"font": "Schriftarten",
"fontSize": "Schriftgröße",
"forExample": "Zum Beispiel",
"force": "freiwillig",
"foundNUpdate": "Update {count} gefunden",
"fullScreen": "Vollbildmodus",

View File

@@ -112,6 +112,7 @@
"followSystem": "Follow system",
"font": "Font",
"fontSize": "Font size",
"forExample": "For example",
"force": "Force",
"foundNUpdate": "Found {count} update",
"fullScreen": "Full screen mode",

View File

@@ -112,6 +112,7 @@
"followSystem": "Seguir al sistema",
"font": "Fuente",
"fontSize": "Tamaño de fuente",
"forExample": "Por ejemplo",
"force": "Forzar",
"foundNUpdate": "Encontradas {count} actualizaciones",
"fullScreen": "Modo pantalla completa",

View File

@@ -112,6 +112,7 @@
"followSystem": "Suivre le système",
"font": "Police",
"fontSize": "Taille de la police",
"forExample": "Par exemple",
"force": "Forcer",
"foundNUpdate": "{count} mise à jour trouvée",
"fullScreen": "Mode plein écran",

View File

@@ -112,6 +112,7 @@
"followSystem": "Ikuti sistem",
"font": "Font",
"fontSize": "Ukuran huruf",
"forExample": "Sebagai contoh",
"force": "sukarela",
"foundNUpdate": "Menemukan {count} pembaruan",
"fullScreen": "Mode Layar Penuh",

View File

@@ -112,6 +112,7 @@
"followSystem": "システムに従う",
"font": "フォント",
"fontSize": "フォントサイズ",
"forExample": "例えば",
"force": "強制",
"foundNUpdate": "{count}個の更新が見つかりました",
"fullScreen": "フルスクリーンモード",

View File

@@ -112,6 +112,7 @@
"followSystem": "Volg systeem",
"font": "Lettertype",
"fontSize": "Lettergrootte",
"forExample": "Bijvoorbeeld",
"force": "Forceer",
"foundNUpdate": "{count} update gevonden",
"fullScreen": "Volledig schermmodus",

View File

@@ -112,6 +112,7 @@
"followSystem": "Seguir sistema",
"font": "Fonte",
"fontSize": "Tamanho da fonte",
"forExample": "Por exemplo",
"force": "Forçar",
"foundNUpdate": "Encontradas {count} atualizações",
"fullScreen": "Modo tela cheia",

View File

@@ -112,6 +112,7 @@
"followSystem": "следовать за системой",
"font": "шрифт",
"fontSize": "размер шрифта",
"forExample": "Например",
"force": "принудительно",
"foundNUpdate": "Найдено {count} обновлений",
"fullScreen": "полноэкранный режим",

View File

@@ -112,6 +112,7 @@
"followSystem": "跟随系统",
"font": "字体",
"fontSize": "字体大小",
"forExample": "例如",
"force": "强制",
"foundNUpdate": "找到 {count} 个更新",
"fullScreen": "全屏模式",

View File

@@ -112,6 +112,7 @@
"followSystem": "跟隨系統",
"font": "字體",
"fontSize": "字體大小",
"forExample": "例如",
"force": "強制",
"foundNUpdate": "找到 {count} 個更新",
"fullScreen": "全屏模式",

View File

@@ -200,9 +200,13 @@ class _SnippetEditPageState extends State<SnippetEditPage>
padding: const EdgeInsets.all(13),
child: SimpleMarkdown(
data: '''
📌 ${l10n.supportFmtArgs}
📌 ${l10n.supportFmtArgs}\n
${Snippet.fmtArgs.keys.map((e) => '`$e`').join(', ')}\n
${Snippet.fmtArgs.keys.map((e) => '`$e`').join(', ')}
${Snippet.fmtTermKeys.keys.map((e) => '`$e+?}`').join(', ')}\n
${l10n.forExample}:
- `\${ctrl+c}` (Control + C)
- `\${ctrl+b}d` (Tmux Detach)
''',
styleSheet: MarkdownStyleSheet(
codeblockDecoration: const BoxDecoration(