2024-03-18 16:19:42 -05:00
|
|
|
import std/[os, parsecfg, sequtils, tables, strutils]
|
|
|
|
|
|
|
|
import term
|
|
|
|
|
|
|
|
type
|
2024-03-18 16:43:38 -05:00
|
|
|
TsmConfig* = object
|
|
|
|
dirs*: seq[string]
|
|
|
|
sessions*: seq[Session]
|
2024-03-18 16:19:42 -05:00
|
|
|
|
|
|
|
Session = object
|
2024-03-18 16:43:38 -05:00
|
|
|
name*, dir*: string
|
2024-03-18 16:19:42 -05:00
|
|
|
|
|
|
|
proc loadConfigFile(): TsmConfig =
|
|
|
|
let configPath = getHomeDir() / ".config/tsm/config.ini"
|
|
|
|
if configPath.fileExists():
|
|
|
|
let iniTable = loadConfig(configPath)
|
|
|
|
if "sessions" in iniTable:
|
|
|
|
for key, value in iniTable["sessions"].pairs:
|
|
|
|
result.sessions.add Session(name: key, dir: value)
|
|
|
|
if "dirs" in iniTable:
|
|
|
|
for key, value in iniTable["dirs"].pairs:
|
|
|
|
if value != "":
|
|
|
|
termError "[dirs] table should only contain a list of paths"
|
|
|
|
result.dirs.add key
|
|
|
|
|
2024-03-18 16:43:38 -05:00
|
|
|
proc loadTsmConfig*(): TsmConfig =
|
2024-03-18 16:19:42 -05:00
|
|
|
result = loadConfigFile()
|
|
|
|
let tsmDirs = getEnv("TSM_DIRS")
|
|
|
|
if tsmDirs != "":
|
|
|
|
result.dirs = tsmDirs.split(":")
|
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
let dict = loadConfig(getHomeDir() / ".config/tsm/config.ini")
|
|
|
|
let sections = dict.sections().toSeq()
|
|
|
|
if "sessions" in sections:
|
|
|
|
for key, value in dict["sessions"].pairs:
|
|
|
|
echo key, value
|
|
|
|
|
|
|
|
echo loadTsmConfig()
|