मुझे लगता है कि बक्षीस जल्द खत्म हो रहा है ... क्या मैं इसे प्राप्त कर सकता हूं? अगर मैंने आपको बताया:
पर जाएं अनुभाग यदि आप इतिहास खोज से परिचित हैं और इसे वैकल्पिक रूप से उपयोग करना चाहते हैं।
क्या आप zsh की इतिहास खोज से परिचित हैं? यह लगभग विपरीत खोज की तरह है, इस तथ्य को छोड़कर कि आप कमांड लाइन पर कुछ दर्ज करने और up
तीर कुंजी दबाकर खोजना शुरू कर सकते हैं। मुझे लगता है कि यह रिवर्स सर्च की तुलना में एक त्वरित विकल्प है और इसे अधिक बार उपयोग करता है। डाउनसाइड (जो मेरी राय में मामूली है) यह है कि आप अपने मूल खोज क्वेरी के बाद खोज क्वेरी को अपडेट नहीं कर सकते हैं जब तक आप इसे "वापस जाने" से वापस नहीं लौटते।उदाहरण के लिए:
इतिहास (ऊपर से नीचे)
foo
bar
foo-ish
fubar
...
अपने शीघ्र पर:
> fo
प्रेस up
> foo
प्रेस up
> foo-ish
प्रेस down
> foo
प्रेस down
> fo
नोट: जैसे ही आप एक खोज परिणामों को बदलने और up
फिर से हिट के रूप में, बदल पाठ अपने नए क्वेरी हो जाता है।
कोड
const PROMPT = '> ';
// history search
let input = '';
let searchInput = input;
let searchResults = [];
let searchResultIndex = 0;
process.stdin.on('keypress', (_, key) => {
// update 'input' on changes, excluding history
if (input !== server.line && !['up', 'down'].includes(key.name)) {
input = server.line;
}
// search is initiated if a user presses the 'up' key after having inputted
if (input !== '' && (
key.name === 'up' ||
(key.name === 'down' && searchResultIndex > 0)
)) {
// reset on fresh search or if user changed input during search
if (searchInput !== input) {
searchResultIndex = 0;
// first search result is always user's input
searchResults = [input, ...server.history.filter(item => item.includes(input))];
searchInput = input;
}
const increment = key.name === 'up' ? 1 : -1;
searchResultIndex = Math.min(searchResultIndex + increment, searchResults.length - 1);
server.historyIndex = -1;
server.line = '';
process.stdout.clearLine();
process.stdout.cursorTo(0);
const result = searchResults[searchResultIndex];
process.stdout.write(PROMPT + result);
server.cursor = result.length;
server.line = result;
}
});
बोनस - लगातार इतिहास को लागू करने के लिए:
const HISTSIZE = 100;
const server = repl.start({
prompt: PROMPT,
historySize: HISTSIZE,
removeHistoryDuplicates: true,
});
// load history or create file
historyFile = path.join(path.dirname(require.main.filename),
'.repl_history');
try {
fs.statSync(historyFile);
fs.readFileSync(historyFile, 'utf8')
.split('\n')
.filter(line => line.trim())
.map(line => server.history.push(line));
}
catch (e) {
console.log(e);
fs.closeSync(fs.openSync(historyFile, 'w'));
}
server.on('exit',() => {
// save history
fs.writeFileSync(historyFile, server.history.slice(0, HISTSIZE).join('\n'));
console.log('Thank you. Come again.');
process.exit();
});