feat: Redis command autocomplete in CLI

- New src/data/commands.ts: 139 Redis commands across 11 categories
- CliView.vue: autocomplete dropdown with prefix matching
- Category-colored command names (admin=read=green/write=yellow/etc)
- Up/Down navigate suggestions, Tab/Enter selects, Escape closes
- History navigation preserved when suggestions hidden
- Syntax and description shown for each command
This commit is contained in:
2026-07-07 20:59:59 +08:00
parent 05a6f6aa17
commit 742c62517c
3 changed files with 307 additions and 11 deletions
+144 -10
View File
@@ -1,7 +1,8 @@
<script setup lang="ts">
// src/renderer/src/components/CliView.vue
import { ref, nextTick, watch } from 'vue'
import { ref, computed, nextTick, watch } from 'vue'
import { useConnectionStore } from '@renderer/stores/connection'
import { redisCommands } from '@renderer/data/commands'
const connStore = useConnectionStore()
@@ -9,6 +10,26 @@ const input = ref('')
const history = ref<{ cmd: string; result: string; error?: boolean }[]>([])
const historyIndex = ref(-1)
const outputRef = ref<HTMLDivElement>()
const suggestionIndex = ref(-1)
const showSuggestions = computed(() => {
return suggestions.value.length > 0 && !input.value.includes(' ')
})
const suggestions = computed(() => {
const firstWord = input.value.split(/\s+/)[0]
if (!firstWord) return []
const prefix = firstWord.toUpperCase()
return redisCommands
.filter(cmd => cmd.name.startsWith(prefix))
.slice(0, 8)
})
function selectSuggestion(cmd: string) {
input.value = cmd + ' '
suggestionIndex.value = -1
inputRef.value?.focus()
}
async function execute() {
if (!connStore.activeId || !input.value.trim()) return
@@ -16,6 +37,7 @@ async function execute() {
const cmd = input.value.trim()
input.value = ''
historyIndex.value = -1
suggestionIndex.value = -1
const parts = cmd.split(/\s+/)
const command = parts[0].toUpperCase()
@@ -57,6 +79,30 @@ async function execute() {
}
function onKeydown(e: KeyboardEvent) {
if (showSuggestions.value) {
if (e.key === 'ArrowDown') {
e.preventDefault()
suggestionIndex.value = Math.min(suggestionIndex.value + 1, suggestions.value.length - 1)
return
}
if (e.key === 'ArrowUp') {
e.preventDefault()
suggestionIndex.value = Math.max(suggestionIndex.value - 1, -1)
return
}
if (e.key === 'Enter' || e.key === 'Tab') {
if (suggestionIndex.value >= 0 && suggestionIndex.value < suggestions.value.length) {
e.preventDefault()
selectSuggestion(suggestions.value[suggestionIndex.value].name)
return
}
}
if (e.key === 'Escape') {
suggestionIndex.value = -1
return
}
}
if (e.key === 'Enter') {
e.preventDefault()
execute()
@@ -77,6 +123,19 @@ function onKeydown(e: KeyboardEvent) {
}
}
}
const inputRef = ref<HTMLInputElement>()
function categoryColor(category: string): string {
switch (category) {
case 'admin': return 'var(--color-red)'
case 'read': return 'var(--color-green)'
case 'write': return 'var(--color-yellow)'
case 'server': return 'var(--color-blue)'
case 'pubsub': return 'var(--color-purple)'
default: return 'var(--text-primary)'
}
}
</script>
<template>
@@ -95,13 +154,30 @@ function onKeydown(e: KeyboardEvent) {
</div>
<div class="cli-input-bar">
<span class="prompt">&gt;</span>
<input
v-model="input"
class="cli-input"
placeholder="Enter Redis command..."
@keydown="onKeydown"
autofocus
/>
<div class="cli-input-wrapper">
<input
ref="inputRef"
v-model="input"
class="cli-input"
placeholder="Enter Redis command..."
@keydown="onKeydown"
autofocus
/>
<div v-if="showSuggestions" class="cli-suggestions">
<div
v-for="(cmd, index) in suggestions"
:key="cmd.name"
class="cli-suggestion-item"
:class="{ active: index === suggestionIndex }"
@click="selectSuggestion(cmd.name)"
@mouseenter="suggestionIndex = index"
>
<span class="suggestion-name" :style="{ color: categoryColor(cmd.category) }">{{ cmd.name }}</span>
<span class="suggestion-syntax">{{ cmd.syntax }}</span>
<span class="suggestion-desc">{{ cmd.description }}</span>
</div>
</div>
</div>
</div>
</div>
</template>
@@ -160,18 +236,76 @@ function onKeydown(e: KeyboardEvent) {
flex-shrink: 0;
}
.cli-input {
.cli-input-wrapper {
flex: 1;
position: relative;
margin-left: 6px;
}
.cli-input {
width: 100%;
background: transparent;
border: none;
outline: none;
color: var(--text-primary);
font-family: 'Cascadia Code', 'Fira Code', monospace;
font-size: 13px;
margin-left: 6px;
}
.cli-input::placeholder {
color: var(--text-muted);
}
.cli-suggestions {
position: absolute;
bottom: 100%;
left: 0;
right: 0;
margin-bottom: 4px;
background: var(--bg-secondary);
border: 1px solid var(--border-color);
border-radius: 6px;
max-height: 320px;
overflow-y: auto;
z-index: 100;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}
.cli-suggestion-item {
display: flex;
flex-direction: column;
padding: 6px 10px;
cursor: pointer;
border-bottom: 1px solid var(--border-color);
gap: 2px;
}
.cli-suggestion-item:last-child {
border-bottom: none;
}
.cli-suggestion-item.active,
.cli-suggestion-item:hover {
background: var(--bg-hover);
}
.suggestion-name {
font-weight: 600;
font-size: 13px;
font-family: 'Cascadia Code', 'Fira Code', monospace;
}
.suggestion-syntax {
font-size: 11px;
color: var(--text-muted);
font-family: 'Cascadia Code', 'Fira Code', monospace;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.suggestion-desc {
font-size: 11px;
color: var(--text-secondary);
}
</style>