-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Readline like input with persistent history
- Loading branch information
Showing
12 changed files
with
163 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
bbz | ||
dist/ | ||
firmware.o | ||
.bzzhistory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
type console interface { | ||
readline() (string, bool) | ||
readChar() (uint8, bool) | ||
write(s string) | ||
close() | ||
} | ||
|
||
type consoleSimple struct { | ||
in *bufio.Scanner | ||
} | ||
|
||
func newConsoleSimple() *consoleSimple { | ||
var c consoleSimple | ||
c.in = bufio.NewScanner(os.Stdin) | ||
return &c | ||
} | ||
|
||
func (c *consoleSimple) readline() (string, bool) { | ||
if !c.in.Scan() { | ||
return "", true | ||
} | ||
line := c.in.Text() | ||
return line, false | ||
} | ||
|
||
func (c *consoleSimple) readChar() (uint8, bool) { | ||
// TODO: capture keystrokes. We will just get the first char of the line | ||
// and ignore the rest. | ||
s, stop := c.readline() | ||
if s == "" { | ||
return ' ', stop | ||
} else { | ||
return s[0], stop | ||
} | ||
} | ||
|
||
func (c *consoleSimple) write(s string) { | ||
fmt.Print(s) | ||
} | ||
|
||
func (c *consoleSimple) close() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/peterh/liner" | ||
) | ||
|
||
const historyFilename = ".bzzhistory" | ||
|
||
type consoleLiner struct { | ||
liner *liner.State | ||
prompt string | ||
} | ||
|
||
func newConsoleLiner() *consoleLiner { | ||
var c consoleLiner | ||
|
||
c.liner = liner.NewLiner() | ||
c.liner.SetCtrlCAborts(true) | ||
if f, err := os.Open(historyFilename); err == nil { | ||
c.liner.ReadHistory(f) | ||
f.Close() | ||
} | ||
|
||
return &c | ||
} | ||
|
||
func (c *consoleLiner) close() { | ||
|
||
if f, err := os.Create(historyFilename); err == nil { | ||
c.liner.WriteHistory(f) | ||
f.Close() | ||
} | ||
|
||
c.liner.Close() | ||
} | ||
|
||
func (c *consoleLiner) readline() (string, bool) { | ||
fmt.Printf("\r") | ||
line, err := c.liner.Prompt(c.prompt) | ||
c.prompt = "" | ||
if err == liner.ErrPromptAborted || err == io.EOF { | ||
return "", true | ||
} | ||
if err != nil { | ||
panic(err) | ||
} | ||
c.liner.AppendHistory(line) | ||
return line, false | ||
} | ||
|
||
func (c *consoleLiner) readChar() (uint8, bool) { | ||
// TODO: capture keystrokes. We will just get the first char of the line | ||
// and ignore the rest. | ||
s, stop := c.readline() | ||
if s == "" { | ||
return ' ', stop | ||
} else { | ||
return s[0], stop | ||
} | ||
} | ||
|
||
func (c *consoleLiner) write(s string) { | ||
if strings.HasSuffix(s, "\n") || strings.HasSuffix(s, "\r") { | ||
c.prompt = "" | ||
} else { | ||
c.prompt += s | ||
} | ||
fmt.Print(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters