2016-01-25 11 views
6

पर एकाधिक कमांड को पूरा करने के लिए जेलाइन का उपयोग करें, मैं सोच रहा था कि मैं ArgumentCompleter को कैसे कार्यान्वित कर सकता हूं जैसे कि अगर मैं एक पूर्ण और वैध कमांड पूरा करता हूं, तो यह टैब को एक नए कमांड के लिए पूरा करना शुरू कर देगा।वन लाइन

मैं इसे कर सकता है कुछ इस तरह कर रही निर्माण किया जा मान लिया है जाएगा:

final ConsoleReader consoleReader = new ConsoleReader() 

final ArgumentCompleter cyclicalArgument = new ArgumentCompleter(); 
cyclicalArgument.getCompleters().addAll(Arrays.asList(
     new StringsCompleter("foo"), 
     new StringsCompleter("bar"), 
     cyclicalArgument)); 

consoleReader.addCompleter(cyclicalArgument); 
consoleReader.readLine(); 

हालांकि अभी यह पहली foo bar

completeing टैब के बाद काम करना बंद किसी पुस्तकालय मुझे बताने की के साथ पर्याप्त परिचित है मैं इसे लागू करने के बारे में कैसे जाउंगा? या ऐसा करने का कोई ज्ञात तरीका है कि मुझे याद आ रही है? यह भी JLine2 का उपयोग कर रहा है।

उत्तर

4

काफी काम :-)

यह Completer प्रयोग कर रहे हैं द्वारा नियंत्रित किया जाता था। complete() पूर्ण करने की विधि को केवल अंतिम रिक्त स्थान के बाद ही खोज के लिए उपयोग करना होगा।

आप पुस्तकालय की FileNameCompleter पर उदाहरण के लिए देखें, तो इस बिल्कुल नहीं, किया जाता है ताकि आप कोई पूरा होने मिलेगा क्योंकि <input1> <input2> के लिए Completer खोजों और न केवल के लिए <input2>

:-)

आप होगा इनपुट 2 खोजने में सक्षम एक पूर्ण के अपने कार्यान्वयन करना है।

इसके अतिरिक्त CompletionHandler को जो भी आपने पहले से लिखा है उसे जोड़ना है।

protected int matchFiles(final String buffer, final String translated, final File[] files, 
     final List<CharSequence> candidates) { 
     // THIS IS NEW 
     String[] allWords = translated.split(" "); 
     String lastWord = allWords[allWords.length - 1]; 
     // the lastWord is used when searching the files now 
     // --- 

     if (files == null) { 
     return -1; 
     } 

     int matches = 0; 

     // first pass: just count the matches 
     for (File file : files) { 
     if (file.getAbsolutePath().startsWith(lastWord)) { 
      matches++; 
     } 
     } 
     for (File file : files) { 
     if (file.getAbsolutePath().startsWith(lastWord)) { 
      CharSequence name = file.getName() + (matches == 1 && file.isDirectory() ? this.separator() : " "); 
      candidates.add(this.render(file, name).toString()); 
     } 
     } 

     final int index = buffer.lastIndexOf(this.separator()); 

     return index + this.separator().length(); 
    } 

और यहाँ CompletionHandler की complete() -Method डिफ़ॉल्ट CandidateListCompletionHandler बदल रहा:

यहाँ डिफ़ॉल्ट FileNameCompleter बदलते एक बुनियादी कार्यान्वयन है

@Override 
    public boolean complete(final ConsoleReader reader, final List<CharSequence> candidates, final int pos) 
     throws IOException { 
     CursorBuffer buf = reader.getCursorBuffer(); 

     // THIS IS NEW 
     String[] allWords = buf.toString().split(" "); 
     String firstWords = ""; 
     if (allWords.length > 1) { 
     for (int i = 0; i < allWords.length - 1; i++) { 
      firstWords += allWords[i] + " "; 
     } 
     } 
     //----- 

     // if there is only one completion, then fill in the buffer 
     if (candidates.size() == 1) { 
     String value = Ansi.stripAnsi(candidates.get(0).toString()); 

     if (buf.cursor == buf.buffer.length() && this.printSpaceAfterFullCompletion && !value.endsWith(" ")) { 
      value += " "; 
     } 

     // fail if the only candidate is the same as the current buffer 
     if (value.equals(buf.toString())) { 
      return false; 
     } 

     CandidateListCompletionHandler.setBuffer(reader, firstWords + " " + value, pos); 

     return true; 
     } else if (candidates.size() > 1) { 
     String value = this.getUnambiguousCompletions(candidates); 
     CandidateListCompletionHandler.setBuffer(reader, value, pos); 
     } 

     CandidateListCompletionHandler.printCandidates(reader, candidates); 

     // redraw the current console buffer 
     reader.drawLine(); 

     return true; 
    } 
+0

हम्म ,. तो यह मैंने सोचा से कुछ बड़े बदलाव करने जा रहा है। अंतर्दृष्टि के लिए धन्यवाद! – flakes

+0

कम से कम ऐसा लगता है कि आप इसे कुछ कॉन्फ़िगरेशन द्वारा सक्रिय नहीं कर सकते हैं। लेकिन आप एक मानक कार्यान्वयन कर सकते हैं और अपने अगले वर्गों के लिए इसका वारिस कर सकते हैं। –

संबंधित मुद्दे