-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from evan-buss/development
OpenBooks v4
- Loading branch information
Showing
81 changed files
with
5,380 additions
and
1,665 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,5 +1,6 @@ | ||
# start persist | ||
openbooks | ||
!openbooks/ | ||
*.DS_Store | ||
**/dist | ||
# end persist | ||
|
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,35 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch OpenBooks Server", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "debug", | ||
"program": "cmd/openbooks", | ||
"args": [ | ||
"server", | ||
"--log", | ||
"--server", | ||
"localhost" | ||
], | ||
}, | ||
{ | ||
"name": "Launch OpenBooks CLI", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "debug", | ||
"program": "cmd/openbooks", | ||
"args": [ | ||
"cli", | ||
"download", | ||
"--server", | ||
"localhost", | ||
"!test" | ||
], | ||
} | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,94 @@ | ||
package cli | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/evan-buss/openbooks/core" | ||
"github.com/evan-buss/openbooks/irc" | ||
) | ||
|
||
// Config is used to configure CLI mode settings. | ||
type Config struct { | ||
UserName string // Username to use when connecting to IRC | ||
Log bool // True if IRC messages should be logged | ||
Dir string | ||
Server string | ||
irc *irc.Conn | ||
} | ||
|
||
// Reader is a way to recieve input from the user | ||
var reader *bufio.Reader | ||
|
||
// IRC is the current IRC connection | ||
var conn *irc.Conn | ||
|
||
// Start instantiates the OpenBooks CLI interface | ||
func Start(config Config) { | ||
conn := irc.New(config.UserName, "OpenBooks CLI") | ||
|
||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, os.Interrupt, syscall.SIGTERM) | ||
go func() { | ||
<-c | ||
conn.Disconnect() | ||
os.Exit(1) | ||
}() | ||
|
||
// StartInteractive instantiates the OpenBooks CLI interface | ||
func StartInteractive(config Config) { | ||
fmt.Println("=======================================") | ||
fmt.Println(" Welcome to OpenBooks ") | ||
fmt.Println("=======================================") | ||
|
||
core.Join(conn) | ||
conn := instantiate(config) | ||
config.irc = conn | ||
|
||
cwd, err := os.Getwd() | ||
if err != nil { | ||
log.Fatalln("Could not get current working directory.", err) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
registerShutdown(conn, cancel) | ||
|
||
handler := fullHandler(config) | ||
if config.Log { | ||
file := config.setupLogger(handler) | ||
defer file.Close() | ||
} | ||
|
||
exitSignal := make(chan struct{}) | ||
go core.ReadDaemon(conn, config.Log, Handler{cwd}, exitSignal) | ||
go core.StartReader(ctx, conn, handler) | ||
terminalMenu(conn) | ||
|
||
fmt.Println("Connection established...") | ||
<-ctx.Done() | ||
} | ||
|
||
fmt.Print("\r") | ||
func StartDownload(config Config, download string) { | ||
conn := instantiate(config) | ||
defer conn.Close() | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
reader = bufio.NewReader(os.Stdin) | ||
handler := core.EventHandler{} | ||
handler[core.BookResult] = func(text string) { | ||
fmt.Printf("%sReceived file response.\n", clearLine) | ||
config.downloadHandler(text) | ||
cancel() | ||
} | ||
if config.Log { | ||
file := config.setupLogger(handler) | ||
defer file.Close() | ||
} | ||
|
||
fmt.Printf("Sending download request.") | ||
go core.StartReader(ctx, conn, handler) | ||
core.DownloadBook(conn, download) | ||
fmt.Printf("%sSent download request.", clearLine) | ||
fmt.Printf("Waiting for file response.") | ||
|
||
// Get the first input | ||
// Reader, IRC | ||
menu() | ||
// We make a channel to block forever. We want the reader daemon to run forever | ||
<-exitSignal | ||
registerShutdown(conn, cancel) | ||
<-ctx.Done() | ||
} | ||
|
||
//reader *bufio.Reader, irc *irc.Conn | ||
func menu() { | ||
fmt.Print("\ns)search\ng)et book\nd)one\n~> ") | ||
|
||
input, _ := reader.ReadString('\n') | ||
input = strings.TrimRight(input, "\n") | ||
input = strings.TrimRight(input, "\r") | ||
|
||
switch input { | ||
case "s": | ||
fmt.Print("@search ") | ||
message, _ := reader.ReadString('\n') | ||
core.SearchBook(conn, message) | ||
case "g": | ||
fmt.Print("Download String: ") | ||
message, _ := reader.ReadString('\n') | ||
core.DownloadBook(conn, message) | ||
case "d": | ||
fmt.Println("Disconnecting.") | ||
conn.Disconnect() | ||
os.Exit(0) | ||
default: | ||
fmt.Println("Invalid Selection.") | ||
menu() | ||
func StartSearch(config Config, query string) { | ||
conn := instantiate(config) | ||
defer conn.Close() | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
handler := core.EventHandler{} | ||
handler[core.SearchResult] = func(text string) { | ||
fmt.Printf("%sReceived file response.\n", clearLine) | ||
config.searchHandler(text) | ||
cancel() | ||
} | ||
handler[core.MatchesFound] = config.matchesFoundHandler | ||
if config.Log { | ||
file := config.setupLogger(handler) | ||
defer file.Close() | ||
} | ||
|
||
fmt.Printf("Sending search request.") | ||
go core.StartReader(ctx, conn, handler) | ||
core.SearchBook(conn, query) | ||
fmt.Printf("%sSent search request.", clearLine) | ||
fmt.Printf("Waiting for file response.") | ||
|
||
registerShutdown(conn, cancel) | ||
<-ctx.Done() | ||
} |
Oops, something went wrong.