import { log , err } from "./log" ; import { existsSync , readFile , readFileSync , fstat , Stats , stat , statSync } from "fs" ; import wallpaper = require ( "wallpaper" ) ; import { resolvePath } from "./fs-plus" ; type path = string ; interface wallpaperManager { [ key ] : { configfile : path ; proc : ( file : Buffer ) => path | undefined ; } ; } const wallpaperManagers = { feh : { configfile : "~/.fehbg" , proc : file => { const match = file . toString ( ) . match ( /--bg[-\w]+ '(.*?)'/ ) ; if ( match && match [ 1 ] ) return match [ 1 ] ; } } , nitrogen : { configfile : "~/.config/nitrogen/bg-saved.cfg" , proc : file => { const match = file . toString ( ) . match ( /file=(.*)/ ) ; if ( match && match [ 1 ] ) return match [ 1 ] ; } } } ; function getWallpaper ( determined ) { if ( determined in wallpaperManagers ) { log ( `You select ${ determined } for background manager` ) ; let { configfile , proc } = wallpaperManagers [ determined ] ; configfile = resolvePath ( configfile ) ; if ( ! existsSync ( configfile ) ) err ( `${ determined } configuration file "${ configfile } not found"` ) ; const wallpaperPath = proc ( readFileSync ( configfile ) ) ; if ( ! wallpaperPath ) err ( `Wallpaper path not found in ${ determined } configuration file "${ configfile }"` ) ; else if ( ! existsSync ( wallpaperPath ) ) { err ( `${ wallpaperPath } not found` ) ; } else return wallpaperPath ; } err ( `${ determined } wallpaper manager not found` ) ; } function detectWallpaperManager ( ) { let newest = [ "" , - Infinity ] ; for ( let key in wallpaperManagers ) { const stat = getStat ( resolvePath ( wallpaperManagers [ key ] . configfile ) ) ; if ( ! stat ) continue ; if ( stat . atimeMs > newest [ 1 ] ) newest = [ key , stat . atimeMs ] ; } return newest [ 0 ] ; } function getWallpaperFromConfigVariable ( str ) { const regex = /^\$(.*)/ ; const match = str . match ( regex ) ; if ( match && match [ 1 ] ) { return getWallpaper ( match [ 1 ] ) ; } } function getStat ( filepath ) { if ( existsSync ( filepath ) ) { return statSync ( filepath ) ; } } export { getWallpaper , detectWallpaperManager , getWallpaperFromConfigVariable } ;