wmtools/src/model/config.nim

58 lines
1.5 KiB
Nim
Raw Normal View History

2023-11-22 19:16:47 +00:00
import os
2023-12-12 22:46:15 +00:00
import strutils
2023-11-22 19:16:47 +00:00
import parsetoml
2023-11-23 09:41:16 +00:00
import tool
2023-11-23 12:14:24 +00:00
import screenshot
2023-11-22 19:16:47 +00:00
type
Config* = ref object
exec*: string
2023-11-22 21:19:37 +00:00
run*: Tool
2023-11-22 19:16:47 +00:00
max_lines*: int
prepend*: bool
2023-11-23 20:23:25 +00:00
to_stdout*: bool
2023-11-23 12:14:24 +00:00
screenshot_tool*: ScreenshotTool
2023-12-12 22:46:15 +00:00
unsplash_key*: string
bg_dir*: string
2023-11-22 19:16:47 +00:00
let config_dir* = getHomeDir() & ".config/wm_tools/"
let config_file* = config_dir & "config.toml"
proc `$`(c: Config): string =
var str = "exec = \"" & c.exec & "\"\n"
str &= "prepend = " & $c.prepend & "\n"
2023-11-23 12:14:24 +00:00
str &= "screenshot_tool = \"" & $c.screenshot_tool & "\"\n"
2023-11-22 19:16:47 +00:00
str &= "max_lines = " & $c.max_lines
str &= "\n"
return str
proc newConfig*(): Config =
var cfg = Config()
cfg.exec = "rofi -dmenu"
cfg.prepend = true
2023-11-23 12:14:24 +00:00
cfg.screenshot_tool = Maim
2023-11-22 19:16:47 +00:00
cfg.max_lines = 20
discard existsOrCreateDir(config_dir)
if not fileExists(config_file):
writeFile(config_file,$cfg)
else:
let content = readFile(config_file)
try:
let toml = parseString(content)
if toml.hasKey("exec"):
cfg.exec = toml["exec"].getStr
if toml.hasKey("max_lines"):
cfg.max_lines = toml["max_lines"].getInt
2023-11-23 12:14:24 +00:00
if toml.hasKey("screenshot_tool"):
cfg.screenshot_tool = toml["screenshot_tool"].getStr.toScreenshotTool
2023-12-12 22:46:15 +00:00
if toml.hasKey("unsplash_key"):
cfg.unsplash_key = toml["unsplash_key"].getStr
if toml.hasKey("bg_dir"):
cfg.bg_dir = toml["bg_dir"].getStr.replace("$HOME",getHomeDir())
2023-11-22 19:16:47 +00:00
except:
echo "Error with Config File:"
echo getCurrentExceptionMsg()
return cfg