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(); final key = argsFmted.substring(start, end).toLowerCase();
// Special funcs // Special funcs
final special = _find(specialCtrl, key); final special = _find(SnippetFuncs.specialCtrl, key);
if (special != null) { if (special != null) {
final raw = key.substring(special.key.length + 1, key.length - 1); final raw = key.substring(special.key.length + 1, key.length - 1);
await special.value(raw); await special.value((term: terminal, raw: raw));
} }
// Term keys // Term keys
@@ -152,15 +152,17 @@ class Snippet implements TagPickable {
// `${ctrl+ad}` -> `ctrla + d` // `${ctrl+ad}` -> `ctrla + d`
final chars = key.substring(termKey.key.length + 1, key.length - 1); final chars = key.substring(termKey.key.length + 1, key.length - 1);
terminal.charInput( if (chars.isEmpty) return;
final ok = terminal.charInput(
chars.codeUnitAt(0), chars.codeUnitAt(0),
ctrl: ctrlAlt.ctrl, ctrl: ctrlAlt.ctrl,
alt: ctrlAlt.alt, alt: ctrlAlt.alt,
); );
if (!ok) {
for (final char in chars.codeUnits.skip(1)) { Loggers.app.warning('Failed to input: $key');
terminal.charInput(char, ctrl: false, alt: false);
} }
terminal.textInput(chars.substring(1));
} }
MapEntry<String, T>? _find<T>(Map<String, T> map, String key) { MapEntry<String, T>? _find<T>(Map<String, T> map, String key) {
@@ -180,12 +182,6 @@ class Snippet implements TagPickable {
static final fmtTermKeys = { static final fmtTermKeys = {
r'${ctrl': TerminalKey.control, r'${ctrl': TerminalKey.control,
r'${alt': TerminalKey.alt, 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 { abstract final class SnippetFuncs {
static FutureOr<void> sleep(String raw) async { static final specialCtrl = {
final seconds = int.tryParse(raw); // `${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; if (seconds == null) return;
final duration = Duration(seconds: seconds); final duration = Duration(seconds: seconds);
await Future.delayed(duration); 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", "followSystem": "System verfolgen",
"font": "Schriftarten", "font": "Schriftarten",
"fontSize": "Schriftgröße", "fontSize": "Schriftgröße",
"forExample": "Zum Beispiel",
"force": "freiwillig", "force": "freiwillig",
"foundNUpdate": "Update {count} gefunden", "foundNUpdate": "Update {count} gefunden",
"fullScreen": "Vollbildmodus", "fullScreen": "Vollbildmodus",

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -200,9 +200,13 @@ class _SnippetEditPageState extends State<SnippetEditPage>
padding: const EdgeInsets.all(13), padding: const EdgeInsets.all(13),
child: SimpleMarkdown( child: SimpleMarkdown(
data: ''' 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( styleSheet: MarkdownStyleSheet(
codeblockDecoration: const BoxDecoration( codeblockDecoration: const BoxDecoration(