-
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.
Merge pull request #54 from melvic-ybanez/scala-prefs
Scala prefs
- Loading branch information
Showing
28 changed files
with
449 additions
and
111 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 |
---|---|---|
|
@@ -30,4 +30,6 @@ org.scala-ide.sdt.update-site/site.xml | |
*.ppm | ||
*.png | ||
|
||
*.sc | ||
*.sc | ||
|
||
user-prefs.json |
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,7 @@ | ||
{ | ||
"scala": { | ||
"pointFree": true, | ||
"simplifyMatch": true, | ||
"usePredef": true | ||
} | ||
} |
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,10 @@ | ||
package com.melvic.chi | ||
|
||
// TODO: Store this on a properties file | ||
object Config { | ||
val MaxColumnWidth: Int = 80 | ||
val DialogFontSize: Int = 14 | ||
val DialogHeaderFontSize: Int = 15 | ||
val CustomSettingsPath = "user-prefs.json" | ||
val DefaultSettingsPath = "preferences.json" | ||
} |
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,24 +1,28 @@ | ||
package com.melvic.chi | ||
|
||
import com.melvic.chi.config.Preferences | ||
|
||
import scala.annotation.tailrec | ||
import scala.io.StdIn.readLine | ||
|
||
//noinspection SpellCheckingInspection | ||
object Repl { | ||
def apply(): Unit = { | ||
println("Welcome to the Chi Repl. Write your propositions or function signatures below " + | ||
"and press enter to see the results") | ||
loop() | ||
def apply(implicit prefs: Preferences): Unit = { | ||
println( | ||
"Welcome to the Chi Repl. Write your propositions or function signatures below " + | ||
"and press enter to see the results" | ||
) | ||
loop | ||
} | ||
|
||
@tailrec | ||
def loop(): Unit = | ||
def loop(implicit prefs: Preferences): Unit = | ||
readLine("chi> ") match { | ||
case "exit" => println("Bye!") | ||
case "" => loop() | ||
case "" => loop | ||
case input => | ||
println(generateAndShow(input)) | ||
println() | ||
loop() | ||
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
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,26 @@ | ||
package com.melvic.chi.config | ||
|
||
import com.melvic.chi.config.SettingsContent.save | ||
import os.Path | ||
|
||
class Preferences { | ||
var content: SettingsContent = SettingsContent.dummy | ||
|
||
def reload(): Unit = | ||
content = SettingsContent.load | ||
|
||
def save(settingsContent: SettingsContent): Unit = { | ||
SettingsContent.save(settingsContent) | ||
reload() | ||
} | ||
} | ||
|
||
object Preferences { | ||
def load: Preferences = new Preferences { | ||
content = SettingsContent.load | ||
} | ||
|
||
def loadDefaults: Preferences = new Preferences { | ||
content = SettingsContent.loadDefaults | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/scala/com/melvic/chi/config/SettingsContent.scala
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,50 @@ | ||
package com.melvic.chi.config | ||
|
||
import com.melvic.chi.Config | ||
import com.melvic.chi.config.SettingsContent.ScalaSettings | ||
import os.{Path, ReadablePath, ResourcePath} | ||
import upickle.default._ | ||
|
||
import java.io.{BufferedWriter, FileWriter} | ||
|
||
final case class SettingsContent(scala: ScalaSettings) | ||
|
||
object SettingsContent { | ||
final case class ScalaSettings(pointFree: Boolean, simplifyMatch: Boolean, usePredef: Boolean) | ||
|
||
implicit def scalaPrefs(implicit prefs: Preferences): ScalaSettings = | ||
prefs.content.scala | ||
|
||
implicit val scalaConfigRW: ReadWriter[ScalaSettings] = macroRW[ScalaSettings] | ||
implicit val prefRW: ReadWriter[SettingsContent] = macroRW[SettingsContent] | ||
|
||
val dummy = SettingsContent( | ||
ScalaSettings(pointFree = false, simplifyMatch = false, usePredef = false) | ||
) | ||
|
||
val customSettingsPath = os.pwd / Config.CustomSettingsPath | ||
val defaultSettingsPath = os.resource / Config.DefaultSettingsPath | ||
|
||
def load: SettingsContent = { | ||
val prefPath = | ||
if (os.exists(customSettingsPath)) os.read(customSettingsPath) | ||
else os.read(defaultSettingsPath) | ||
loadFromPathString(prefPath) | ||
} | ||
|
||
def loadFromPathString(path: String) = | ||
read[SettingsContent](ujson.read(path)) | ||
|
||
def loadDefaults: SettingsContent = | ||
loadFromPathString(os.read(defaultSettingsPath)) | ||
|
||
def save(settingsContent: SettingsContent): Unit = { | ||
if (!os.exists(customSettingsPath)) | ||
customSettingsPath.toIO.getParentFile.mkdirs() | ||
|
||
val settingsJson = write(settingsContent) | ||
val writer = new BufferedWriter(new FileWriter(customSettingsPath.toIO)) | ||
writer.write(settingsJson) | ||
writer.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
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.