refactor: extract save path helpers and remove obsolete JSONC handling

- Extract resolveSavePath() and buildSaveSetup() in SnipCommands.qml
  to eliminate duplicate save path logic between Copy and Edit commands
- Remove unnecessary sed JSONC comment stripping from record.sh since
  config format is now plain JSON
This commit is contained in:
DJ
2025-12-19 18:20:28 -05:00
parent 43ead1c508
commit 52b5a1e5a4
2 changed files with 19 additions and 15 deletions
@@ -33,25 +33,33 @@ Singleton {
// Default save location when savePath is empty
readonly property string defaultSavePath: Directories.home + "/Pictures/hypr-lens"
// Resolve and expand save directory path
function resolveSavePath(saveDir: string): string {
const targetDir = saveDir !== "" ? saveDir : defaultSavePath;
return expandTilde(targetDir);
}
// Build shell commands for save directory setup and filename generation
function buildSaveSetup(expandedDir: string): string {
return `mkdir -p '${StringUtils.shellSingleQuoteEscape(expandedDir)}' && \
saveFileName="screenshot-$(date '+%Y-%m-%d_%H.%M.%S').png" && \
savePath="${expandedDir}/$saveFileName"`;
}
// Copy to clipboard (optionally also saves to disk if copyAlsoSaves is true)
function buildCopyCommand(screenshotPath: string, rx: int, ry: int, rw: int, rh: int, saveDir: string, alsoSave: bool): list<string> {
const cropBase = buildCropBase(screenshotPath, rx, ry, rw, rh);
const cropToStdout = `${cropBase} -`;
const cleanup = buildCleanup(screenshotPath);
// If not saving, just copy to clipboard
if (!alsoSave) {
return ["bash", "-c", `${cropToStdout} | wl-copy && ${cleanup}`];
}
// Save + copy: use provided path or default
const targetDir = saveDir !== "" ? saveDir : defaultSavePath;
const expandedSaveDir = expandTilde(targetDir);
const expandedSaveDir = resolveSavePath(saveDir);
return [
"bash", "-c",
`mkdir -p '${StringUtils.shellSingleQuoteEscape(expandedSaveDir)}' && \
saveFileName="screenshot-$(date '+%Y-%m-%d_%H.%M.%S').png" && \
savePath="${expandedSaveDir}/$saveFileName" && \
`${buildSaveSetup(expandedSaveDir)} && \
${cropToStdout} | tee >(wl-copy) > "$savePath" && \
${cleanup}`
];
@@ -63,14 +71,10 @@ Singleton {
const cropToStdout = `${cropBase} -`;
const cleanup = buildCleanup(screenshotPath);
// Always save: use provided path or default
const targetDir = saveDir !== "" ? saveDir : defaultSavePath;
const expandedSaveDir = expandTilde(targetDir);
const expandedSaveDir = resolveSavePath(saveDir);
return [
"bash", "-c",
`mkdir -p '${StringUtils.shellSingleQuoteEscape(expandedSaveDir)}' && \
saveFileName="screenshot-$(date '+%Y-%m-%d_%H.%M.%S').png" && \
savePath="${expandedSaveDir}/$saveFileName" && \
`${buildSaveSetup(expandedSaveDir)} && \
${cropToStdout} | swappy -f - -o "$savePath" && \
${cleanup}`
];
+2 -2
View File
@@ -25,8 +25,8 @@ get_active_monitor() {
hyprctl monitors -j | jq -r '.[] | select(.focused == true) | .name'
}
# Configuration (strip // comments from JSONC before parsing)
CUSTOM_PATH=$(sed 's|//.*||g' "$CONFIG_FILE" 2>/dev/null | jq -r "$JSON_PATH" 2>/dev/null)
# Configuration
CUSTOM_PATH=$(jq -r "$JSON_PATH" "$CONFIG_FILE" 2>/dev/null)
if [[ -n "$CUSTOM_PATH" && "$CUSTOM_PATH" != "null" ]]; then
RECORDING_DIR="$CUSTOM_PATH"
else