गो

2015-06-07 6 views
7

में कर्सर कुंजी टर्मिनल इनपुट मैं टर्मिनल में उपयोग के लिए एक गो एप्लिकेशन बना रहा हूं। निम्नलिखित कोड उपयोगकर्ता को टर्मिनल पर पाठ इनपुट करने के लिए कहता है।गो

package main 

import (
    "bufio" 
    "fmt" 
    "os" 
) 

func main() { 
    for { 
     fmt.Println("Please input something and use arrows to move along the text left and right") 
     in := bufio.NewReader(os.Stdin) 
     _, err := in.ReadString('\n') 
     if err != nil { 
      fmt.Println(err) 
     } 
    } 
} 

समस्या यह है कि एक उपयोगकर्ता है ताकि इसे संशोधित करने के लिए सिर्फ inputted पाठ साथ जाने के लिए बाएँ और दाएँ तीर का उपयोग नहीं कर सकता है। जब वह तीर दबाता है, तो कंसोल ^[[D^[[C^[[A^[[B संकेतों को प्रिंट करता है।

उत्पादन:

Please input something and use arrows to move along the text left and right 
hello^[[D^[[C^[[A^[[B 

कैसे तीर कुंजी अधिक उपयोगकर्ता के अनुकूल व्यवहार करते हैं और एक मानव सिर्फ inputted पाठ के साथ नेविगेट करते हैं, बाएँ और दाएँ तीर का उपयोग करके बनाने के लिए?

मुझे लगता है, मुझे termbox-go या gocui जैसे पुस्तकालयों पर ध्यान देना चाहिए, लेकिन इस उद्देश्य के लिए बिल्कुल उनका उपयोग कैसे करें, मुझे नहीं पता।

उत्तर

3

एक आसान उदाहरण carmark/pseudo-terminal-go होगा, जहां आप terminal in raw mode डाल सकते हैं और पूर्ण अप-डाउन-बाएं-दाएं कर्सर चाल से लाभ उठा सकते हैं।

terminal.go#NewTerminal() से

// NewTerminal runs a VT100 terminal on the given ReadWriter. If the ReadWriter is 
// a local terminal, that terminal must first have been put into raw mode. 
// prompt is a string that is written at the start of each input line (i.e. 
// "> "). 
func NewTerminal(c io.ReadWriter, prompt string) *Terminal 

terminal/terminal.go और terminal/terminal_test.go देखें, साथ ही MakeRaw() रूप

+1

मैं इस पुस्तकालय स्थापित नहीं कर सकता। '$ go github.com/carmark/छद्म-टर्मिनल-जाओ 'आउटपुट ' पैकेज github.com/carmark/pseudo-terminal-go \t आयात टर्मिनल: अपरिचित आयात पथ "टर्मिनल" ' –

+0

@MaximYefremov मैं सहमत हूं। एक टर्मिनल के कार्यान्वयन के संबंध में कोड का एक उदाहरण देने के लिए और अधिक था, और तथ्य यह है कि इसे कच्चे मोड में रखा जाना चाहिए। – VonC

+0

यह बहुत अच्छा काम करता है, आपको "github.com/carmark/pseudo-terminal-go/terminal" आयात करना होगा –

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