package with cabal

This commit is contained in:
Primrose 2026-02-01 16:50:16 +01:00
parent 81bb6fad24
commit 46f265557a
Signed by: primrose
GPG key ID: 4E887A4CA9714ADA
5 changed files with 35 additions and 85 deletions

View file

@ -0,0 +1 @@
dist-newstyle/

View file

@ -1,61 +0,0 @@
# This is here because scanimage's batch mode is broken for my scanner
OUTPUT_FILE="${1:-./scan_"$(date)".pdf}"
if [ -e "$OUTPUT_FILE" ]; then
echo "$OUTPUT_FILE" already exists, you are probably making a mistake!
exit 1
fi
tempdir="$(mktemp -d)"
filenames=()
counter=1
function clean() {
# Make sure I don't remove things other than pdf
rm "$tempdir/"*.pdf
rm -d "$tempdir"
}
trap clean EXIT
while :; do
ok="true"
echo "Scanning page $counter"
CUR_FNAME="$tempdir/easyscan_$counter.pdf"
# If no size is set, the output will be wonky-sized
# 210,297 is the size of A4
scanimage -x 210 -y 297 --resolution 300 -o "$CUR_FNAME" >/dev/null 2>&1 ||
{
echo "Failed to scan page..."
ok="false"
}
if $ok; then
counter=$((counter += 1))
filenames+=("$CUR_FNAME")
fi
IFS= read -r -p "Continue scanning? [Y/n] " cont
case "$cont" in
[nN])
echo "Exiting..."
break
;;
[yY] | *) ;;
esac
done
# Multiple files are scanned, join them
#
# Note: do NOT use the * glob, because bash orders lexicographically and not
# numerically, the merged ordering will be wrong.
merged_filename="$tempdir/easyscan_final.pdf"
pdfunite "${filenames[@]}" "$merged_filename"
# Copy scan to current directory
mkdir -p "$(dirname "$OUTPUT_FILE")"
cp "$merged_filename" "$OUTPUT_FILE"
clean

View file

@ -1,19 +1,10 @@
{
writeShellApplication,
sane-backends,
poppler-utils,
fzf,
ghostscript,
haskellPackages,
haskell,
}:
writeShellApplication {
name = "easyscan";
let
inherit (haskell.lib.compose) justStaticExecutables;
runtimeInputs = [
sane-backends
poppler-utils
fzf
ghostscript
];
text = builtins.readFile ./easyscan.sh;
}
drv = haskellPackages.callCabal2nix "easyscan" ./src { };
in
justStaticExecutables drv

View file

@ -5,20 +5,15 @@
import Control.Monad
import Data.Char
import Data.List (dropWhileEnd)
import System.Directory
import System.Environment
import System.Exit
import System.FilePath
import System.Posix.Files
import System.Posix.Temp
import System.IO
import System.Process
data ScanState = ScanState
{ sExitCode :: Maybe ExitCode
, sNumbering :: Word
}
data ScanState = ScanState (Maybe ExitCode) Word
defaultScanimageArgs :: [String]
defaultScanimageArgs =
@ -54,14 +49,14 @@ main = do
putStrLn "No file was scanned, exiting."
exitWith (ExitFailure 1)
else do
readProcessWithExitCodeTraced "pdfunite" (allFiles ++ [ mergedFilename ]) ""
() <$ readProcessWithExitCodeTraced "pdfunite" (allFiles ++ [ mergedFilename ]) ""
copyFile mergedFilename targetFile
removeDirectoryRecursive workingDir
-- TODO: help page
_ -> exitWith (ExitFailure 1)
where
loop :: forall s. IO Bool -> (s -> IO s) -> s -> IO s
loop :: IO Bool -> (s -> IO s) -> s -> IO s
loop cond action state0 = do
x <- action state0
c <- cond

View file

@ -0,0 +1,24 @@
cabal-version: 3.0
name: easyscan
version: 0.1.0.0
description: scanimage helper
author: Léana 江
maintainer: leana.jiang+git@icloud.com
build-type: Simple
common common
ghc-options:
-Wall -Wcompat -Widentities -Wincomplete-record-updates
-Wincomplete-patterns -Wincomplete-uni-patterns
-Wredundant-constraints -Werror=missing-fields
build-depends: base
default-language: Haskell2010
executable manage
import: common
main-is: ./Main.hs
build-depends:
, filepath
, directory
, process
, unix