const DEFAULT_LIMIT = 10000;
const DEBUG = { ENABLED: false };
const debug = (...args) => DEBUG.ENABLED && console.log(...args)
const info = (...args) => console.log(...args)
export const enableDebugMode = () => { DEBUG.ENABLED = true }
const getImages = (filepath, maxdepth) => {
const findCommand = `find -E ${filepath} -iregex '.*\.(jpg|jpeg|png|gif)' -maxdepth ${maxdepth}`
const findSortedCommand = `${findCommand} -print0 | xargs -0 ls -at`
debug("findSortedCommand", findSortedCommand)
return exec(findSortedCommand, { silent: true }).toString().split("\n").filter(v => v)
}
const buildImageModal = (payload) => {
let {file} = payload;
const img = `<img src="${file}">`
return `<div class="imgContainer">${img}</div>`
}
const injectCss = (html) => {
const css = `
/* Mimic tailwind grid css */
.grid {display:grid}
.grid-cols-3 {grid-template-columns: repeat(3, minmax(0, 1fr))}
.grid-cols-4 {grid-template-columns: repeat(4, minmax(0, 1fr))}
.grid-cols-5 {grid-template-columns: repeat(5, minmax(0, 1fr))}
/* custom css to center images in grid */
.grid div {place-items: center; padding: clamp(1px, 4%, 25px);}
.imgContainer {display: flex;}
`
const style = `<style type="text/css">${css}</style>`
return `${style}${html}`
}
const buildPage = (imageObjects, limit = DEFAULT_LIMIT) => {
const subset = imageObjects
.slice(0, limit)
.map(file => { return { file } })
const columns = subset.length > 32 ? (subset.length > 64 ? 5 : 4) : 3
const modals = subset.map(buildImageModal).join('\n')
const html = `<div class="grid grid-cols-${columns} pt-1 m-1">${modals}</div>`
const page = injectCss(html)
debug(page);
info('buildPage: Done')
return page
}
export const buildImagesPanel = async (filepath, maxdepth, limit) => {
const images = getImages(filepath, maxdepth);
info(`Found ${images.length} images`);
await arg({
input: " ",
}, buildPage(images, limit));
}