-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd1a8cf
commit d50c5c4
Showing
5 changed files
with
299 additions
and
147 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,19 +1,36 @@ | ||
plugins { | ||
id 'scala' | ||
id 'idea' | ||
id "com.github.maiflai.scalatest" version "0.24" | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
implementation 'org.scala-lang:scala-library:2.12.8' | ||
implementation 'org.scala-lang.modules:scala-async_2.12:0.9.7' | ||
compile 'com.monovore:decline_2.12:0.6.1' | ||
compile 'org.typelevel:cats-core_2.12:1.5.0' | ||
testImplementation 'org.pegdown:pegdown:1.4.2' | ||
testImplementation 'org.scalatest:scalatest_2.12:3.0.6' | ||
testImplementation 'junit:junit:4.12' | ||
} | ||
|
||
task wrapper(type: Wrapper) { | ||
gradleVersion = '4.10' | ||
} | ||
} | ||
|
||
idea{ | ||
module { | ||
testSourceDirs += sourceSets.main.runtimeClasspath | ||
} | ||
} | ||
|
||
task run(type: JavaExec, dependsOn: classes) { | ||
main = 'chatapp.Chat' | ||
standardInput = System.in | ||
classpath sourceSets.main.runtimeClasspath | ||
} |
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,66 @@ | ||
package chatapp | ||
|
||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.{Await, Future, Promise} | ||
import scala.async.Async.{async, await} | ||
|
||
|
||
class REPL[A](reader: REPL[A]#Reader, executor: REPL[A]#Executor, printer: REPL[A]#Printer) { | ||
type Reader = () => String | ||
type Executor = String => Future[A] | ||
type Printer = A => String | ||
|
||
def loop(): Future[Unit] = { | ||
var looping = true | ||
async { | ||
while(looping) { | ||
val input = reader() | ||
|
||
if (input == null || input == "/exit") { | ||
looping = false | ||
} else if(input == "") { | ||
print(s"${REPL.UP}${REPL.ERASE_LINE_AFTER}\r") | ||
} else { | ||
val result = await(executor(input)) | ||
val output = printer(result) | ||
|
||
print(output) | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
object REPL { | ||
val ERASE_SCREEN_AFTER="\u001b[0J" | ||
val ERASE_LINE_BEFORE="\u001b[1K" | ||
val ERASE_LINE_AFTER="\u001b[0K" | ||
|
||
val HOME="\u001b[1H" | ||
val UP="\u001b[1A" | ||
val DOWN="\u001b[1B" | ||
val FORWARD="\u001b[1C" | ||
val BACKWARD="\u001b[1D" | ||
|
||
val SAVE_CURSOR="\u001b[0s" | ||
val RESTORE_CURSOR="\u001b[0u" | ||
|
||
def main(args: Array[String]): Unit = { | ||
val reader = () => { | ||
print(s"${REPL.ERASE_LINE_BEFORE}${REPL.ERASE_SCREEN_AFTER}\r> ") | ||
scala.io.StdIn.readLine() | ||
} | ||
val executor = (input: String) => { | ||
async { input } | ||
} | ||
val printer = (string: String) => { | ||
val cls = s"${string.split("\n").map(c => UP).mkString("")}$ERASE_LINE_BEFORE$ERASE_SCREEN_AFTER\r" | ||
// Prepend two spaces to match input indentation of "> " | ||
val text = string.split("\n").map(line => s" $line").mkString("\n") | ||
s"$cls$text\n" | ||
} | ||
val repl = new REPL(reader, executor, printer) | ||
repl.loop() | ||
} | ||
} |
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
Oops, something went wrong.