diff options
| author | Craig Jennings <c@cjennings.net> | 2026-04-19 07:12:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-04-19 07:12:34 -0500 |
| commit | 7c7a1ea9da1b6020c4f0e34cc47b752f20d81ce8 (patch) | |
| tree | 7375c468a2c17e0ffb88cf9bb1dbdeb2453b3bff /modules/transcription-config.el | |
| parent | e18d90875c221b489a568421e8356c1ec8fdfe85 (diff) | |
| download | dotemacs-7c7a1ea9da1b6020c4f0e34cc47b752f20d81ce8.tar.gz dotemacs-7c7a1ea9da1b6020c4f0e34cc47b752f20d81ce8.zip | |
refactor(transcription): extract four sentinel side-effect helpers
Break cj/--transcription-sentinel's seven inline side-effects into named
helpers:
- cj/--write-transcript-on-success: writes process output to .txt on success
- cj/--append-to-log: appends event marker + process output to log
- cj/--update-transcription-status: marks tracking-list entry complete/error
- cj/--notify-completion: sends success or critical notification
Also: switch the tautological (cj/--should-keep-log t) to use the local
success-p (equivalent but matches the function signature), and rename
the unused audio-file sentinel arg to _audio-file.
Sentinel shrinks from 48 lines with 7 inline blocks to 14 lines of
straight-line helper calls. 10 tests cover the extracted helpers.
Diffstat (limited to 'modules/transcription-config.el')
| -rw-r--r-- | modules/transcription-config.el | 84 |
1 files changed, 44 insertions, 40 deletions
diff --git a/modules/transcription-config.el b/modules/transcription-config.el index e8fe1159..1c864b95 100644 --- a/modules/transcription-config.el +++ b/modules/transcription-config.el @@ -193,53 +193,57 @@ Returns the process object." (format "Started on %s" (file-name-nondirectory audio-file))) process))) -(defun cj/--transcription-sentinel (process event audio-file txt-file log-file) +(defun cj/--write-transcript-on-success (process-buffer success-p txt-file) + "Write PROCESS-BUFFER contents to TXT-FILE when SUCCESS-P is non-nil. +No-op if PROCESS-BUFFER is dead or SUCCESS-P is nil." + (when (and success-p (buffer-live-p process-buffer)) + (with-current-buffer process-buffer + (write-region (point-min) (point-max) txt-file nil 'silent)))) + +(defun cj/--append-to-log (process-buffer log-file event) + "Append an EVENT marker plus PROCESS-BUFFER contents to LOG-FILE. +No-op if PROCESS-BUFFER is dead." + (when (buffer-live-p process-buffer) + (with-temp-buffer + (insert-file-contents log-file) + (goto-char (point-max)) + (insert "\n" (format-time-string "[%Y-%m-%d %H:%M:%S] ") event "\n") + (insert-buffer-substring process-buffer) + (write-region (point-min) (point-max) log-file nil 'silent)))) + +(defun cj/--update-transcription-status (process success-p) + "Mark PROCESS's entry as `complete' or `error' based on SUCCESS-P. +No-op if PROCESS isn't tracked." + (when-let ((entry (assq process cj/transcriptions-list))) + (setf (nth 3 entry) (if success-p 'complete 'error)))) + +(defun cj/--notify-completion (success-p txt-file log-file) + "Send completion notification based on SUCCESS-P. +References TXT-FILE on success (normal urgency), LOG-FILE on failure +\(critical urgency)." + (if success-p + (cj/--notify "Transcription" + (format "Complete. Transcript in %s" (file-name-nondirectory txt-file))) + (cj/--notify "Transcription" + (format "Errored. Logs in %s" (file-name-nondirectory log-file)) + 'critical))) + +(defun cj/--transcription-sentinel (process event _audio-file txt-file log-file) "Sentinel for transcription PROCESS. -EVENT is the process event string. -AUDIO-FILE, TXT-FILE, and LOG-FILE are the associated files." +EVENT is the process event string. TXT-FILE and LOG-FILE are the +associated output files." (let* ((success-p (and (string-match-p "finished" event) (= 0 (process-exit-status process)))) - (process-buffer (process-buffer process)) - (entry (assq process cj/transcriptions-list))) - - ;; Write process output to txt file - (when (and success-p (buffer-live-p process-buffer)) - (with-current-buffer process-buffer - (write-region (point-min) (point-max) txt-file nil 'silent))) - - ;; Append process output to log file - (when (buffer-live-p process-buffer) - (with-temp-buffer - (insert-file-contents log-file) - (goto-char (point-max)) - (insert "\n" (format-time-string "[%Y-%m-%d %H:%M:%S] ") event "\n") - (insert-buffer-substring process-buffer) - (write-region (point-min) (point-max) log-file nil 'silent))) - - ;; Update transcription status - (when entry - (setf (nth 3 entry) (if success-p 'complete 'error))) - - ;; Cleanup log file if successful and configured to do so - (when (and success-p (not (cj/--should-keep-log t))) + (process-buffer (process-buffer process))) + (cj/--write-transcript-on-success process-buffer success-p txt-file) + (cj/--append-to-log process-buffer log-file event) + (cj/--update-transcription-status process success-p) + (when (and success-p (not (cj/--should-keep-log success-p))) (delete-file log-file)) - - ;; Kill process buffer (when (buffer-live-p process-buffer) (kill-buffer process-buffer)) - - ;; Notify user - (if success-p - (cj/--notify "Transcription" - (format "Complete. Transcript in %s" (file-name-nondirectory txt-file))) - (cj/--notify "Transcription" - (format "Errored. Logs in %s" (file-name-nondirectory log-file)) - 'critical)) - - ;; Clean up completed transcriptions after 10 minutes + (cj/--notify-completion success-p txt-file log-file) (run-at-time 600 nil #'cj/--cleanup-completed-transcriptions) - - ;; Update modeline (force-mode-line-update t))) (defun cj/--cleanup-completed-transcriptions () |
