Skip to content

Commit

Permalink
added ability to pass config file as app options
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Mar 10, 2024
1 parent 517213b commit d2f62c0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/absytree.nim
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ block: ## Parse command line options
opts.disableNimScriptPlugins = true
opts.disableWasmPlugins = true

of "session", "s":
opts.sessionOverride = val.some

of cmdEnd: assert(false) # cannot happen

if backend.isNone:
Expand Down
43 changes: 40 additions & 3 deletions src/absytree_js.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ logCategory "main-js"

logger.enableConsoleLogger()

import std/[strformat, dom, macros]
import std/[strformat, dom, macros, jsffi]
import misc/[util, timer, event, custom_async]
import platform/[platform, browser_platform]
import ui/[widget_builders]
import text/text_document
import text/language/language_server
from scripting_api import Backend
import app
import app, app_options

import ui/node

Expand Down Expand Up @@ -105,8 +105,45 @@ proc requestRender(redrawEverything = false) =

discard window.requestAnimationFrame doRender

proc getAppOptions(): AppOptions =
type URLSearchParams = ref object of JsObject
proc getUrlSearchParams(): URLSearchParams {.importjs: "new URLSearchParams(window.location.search)".}
proc forEachEntry(self: URLSearchParams, callback: proc(args: openArray[cstring])) {.importjs: "#.entries().forEach(#)".}

let params = getUrlSearchParams()
params.forEachEntry proc(args: openArray[cstring]) =
debugf"args: {args}"
let key = $args[0]
let val = $args[1]
case key
of "no-nimscript", "n":
result.disableNimScriptPlugins = true

of "no-wasm", "w":
result.disableWasmPlugins = true

of "no-opts", "o":
result.dontRestoreOptions = true

of "no-config", "c":
result.dontRestoreConfig = true

of "no-config-opts":
result.dontRestoreConfig = true
result.dontRestoreOptions = true

of "clean":
result.dontRestoreConfig = true
result.dontRestoreOptions = true
result.disableNimScriptPlugins = true
result.disableWasmPlugins = true

of "session", "s":
result.sessionOverride = val.some

proc runApp(): Future[void] {.async.} =
discard await newEditor(Backend.Browser, rend)
let options = getAppOptions()
discard await newEditor(Backend.Browser, rend, options)

discard rend.onKeyPress.subscribe proc(event: auto): void = requestRender()
discard rend.onKeyRelease.subscribe proc(event: auto): void = requestRender()
Expand Down
11 changes: 9 additions & 2 deletions src/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ type

scriptActions: Table[string, ScriptAction]

sessionFile: string

var gEditor* {.exportc.}: App = nil

implTrait ConfigProvider, App:
Expand Down Expand Up @@ -753,6 +755,8 @@ proc newEditor*(backend: api.Backend, platform: Platform, options = AppOptions()

addHandler AppLogger(app: self)

log lvlInfo, fmt"Creating App with backend {backend} and options {options}"

gEditor = self
gAppInterface = self.asAppInterface
self.platform = platform
Expand Down Expand Up @@ -838,8 +842,11 @@ proc newEditor*(backend: api.Backend, platform: Platform, options = AppOptions()

var state = EditorState()
try:
self.sessionFile = "./config/config.json"

if not options.dontRestoreConfig:
let stateJson = fs.loadApplicationFile("./config/config.json").parseJson
self.sessionFile = options.sessionOverride.get("./config/config.json")
let stateJson = fs.loadApplicationFile(self.sessionFile).parseJson
state = stateJson.jsonTo(EditorState, JOptions(allowMissingKeys: true, allowExtraKeys: true))
log(lvlInfo, fmt"Restoring state {stateJson.pretty}")

Expand Down Expand Up @@ -1082,7 +1089,7 @@ proc saveAppState*(self: App) {.expose("editor").} =
state.hiddenEditors.add editorState

let serialized = state.toJson
fs.saveApplicationFile("./config/config.json", serialized.pretty)
fs.saveApplicationFile(self.sessionFile, serialized.pretty)
fs.saveApplicationFile("./config/options.json", self.options.pretty)

proc requestRender*(self: App, redrawEverything: bool = false) {.expose("editor").} =
Expand Down
3 changes: 2 additions & 1 deletion src/app_options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ type AppOptions* = object
disableWasmPlugins*: bool
dontRestoreOptions*: bool
dontRestoreConfig*: bool
fileToOpen*: Option[string]
fileToOpen*: Option[string]
sessionOverride*: Option[string]

0 comments on commit d2f62c0

Please sign in to comment.