Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-photo

Source of release 0.1.1

0.1.1 / src / remembered.luc

## What luced-photo remembers between runs, in ~/.luced-photo/settings.prisma:
## the library last open, which the next run opens again, and each mode's
## arrangement of panels (luce-ui's DStack.layout_text), which it lays out again.
from luce_prism.prism import Document, Value, Encoding
from luce_std import files, paths
import library

func settings_path() -> str!:
    return paths.join(paths.join(files.home_directory(), ".luced-photo"), "settings.prisma")

## The settings as they are, or new ones.
func settings() -> Document!:
    let path = settings_path() catch failure:
        return fresh()
    let text = files.read_text(path, limit = 1048576) catch failure:
        return fresh()
    let doc = Document.parse(text) catch failure:
        return fresh()
    return doc

func fresh() -> Document!:
    let doc = Document()
    doc.add("/settings", "luced_photo_settings")
    return doc

func text_in(doc: Document, name: str) -> str:
    let value = doc.get("/settings", name) catch failure:
        return ""
    return value.text_at() catch failure:
        return ""

func save(doc: Document) -> unit!:
    let path = settings_path()
    files.ensure_directory(paths.directory(path))
    doc.save(path, Encoding.text)

## The library last open, or "" when there is none or it is gone.
pub func last_library() -> str:
    let doc = settings() catch failure:
        return ""
    let root = text_in(doc, "library")
    if root == "":
        return ""
    let catalog = library.catalog_path(root) catch failure:
        return ""
    return root if library.exists(catalog) else ""

## Remember `root` as the library open now.
pub func remember_library(root: str) -> unit!:
    let doc = settings()
    doc.set("/settings", "library", Value.text(root))
    save(doc)

## Mode `mode`'s arrangement of panels last time, or "" for its usual one.
pub func layout(mode: int) -> str:
    let doc = settings() catch failure:
        return ""
    return text_in(doc, f"layout_{mode}")

## Remember `layouts`, one arrangement per mode ("" for its usual one).
pub func remember_layouts(layouts: list[str]) -> unit!:
    let doc = settings()
    for (mode, text) in layouts.indexed():
        doc.set("/settings", f"layout_{mode}", Value.text(text))
    save(doc)