Allow for piped input

This commit is contained in:
Milan Nikolic
2023-08-22 11:29:33 +02:00
parent c5494af60b
commit 69b0b51010
3 changed files with 55 additions and 17 deletions

View File

@@ -14,7 +14,7 @@ It can convert comics to different formats to fit your various devices.
* rotate, flip, adjust brightness/contrast, adjust levels (Photoshop-like) or grayscale images * rotate, flip, adjust brightness/contrast, adjust levels (Photoshop-like) or grayscale images
* resize algorithms (NearestNeighbor, Box, Linear, MitchellNetravali, CatmullRom, Gaussian, Lanczos) * resize algorithms (NearestNeighbor, Box, Linear, MitchellNetravali, CatmullRom, Gaussian, Lanczos)
* export covers from comics * export covers from comics
* create thumbnails from covers by [FreeDesktop](http://www.freedesktop.org/wiki/) specification * create thumbnails from covers by [FreeDesktop](http://specifications.freedesktop.org/thumbnail-spec/thumbnail-spec-latest.html) specification
### Download ### Download
@@ -48,8 +48,8 @@ This is what it looks like in the PCManFM file manager:
    Commands:     Commands:
      convert*       convert
            Convert archive or document (default command)             Convert archive or document
        --width         --width
            Image width (default "0")             Image width (default "0")
@@ -176,10 +176,6 @@ Convert all images in pdf to 4bit BMP images and save the result in ~/comics dir
[BMP](http://en.wikipedia.org/wiki/BMP_file_format) format is a very good choice for black&white pages. Archive size can be smaller 2-3x and the file will be readable by comic readers. [BMP](http://en.wikipedia.org/wiki/BMP_file_format) format is a very good choice for black&white pages. Archive size can be smaller 2-3x and the file will be readable by comic readers.
Generate thumbnails by [freedesktop specification](http://specifications.freedesktop.org/thumbnail-spec/thumbnail-spec-latest.html) in ~/.cache/thumbnails/normal directory with width 512:
`cbconvert thumbnail --width 512 --outdir ~/.cache/thumbnails/normal /media/comics/GrooTheWanderer/`
Extract covers to ~/covers dir for all supported files found in the directory, Lanczos algorithm is used for resizing: Extract covers to ~/covers dir for all supported files found in the directory, Lanczos algorithm is used for resizing:
`cbconvert cover --outdir ~/covers --filter=7 /media/comics/GrooTheWanderer/` `cbconvert cover --outdir ~/covers --filter=7 /media/comics/GrooTheWanderer/`

View File

@@ -1,7 +1,9 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"io"
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
@@ -124,7 +126,7 @@ func main() {
_, _ = fmt.Fprintf(os.Stderr, "\r") _, _ = fmt.Fprintf(os.Stderr, "\r")
} }
// parseFlags parses command line flags // parseFlags parses command line flags.
func parseFlags() (cbconvert.Options, []string) { func parseFlags() (cbconvert.Options, []string) {
opts := cbconvert.Options{} opts := cbconvert.Options{}
var args []string var args []string
@@ -193,7 +195,7 @@ func parseFlags() (cbconvert.Options, []string) {
convert.Usage = func() { convert.Usage = func() {
_, _ = fmt.Fprintf(os.Stderr, "Usage: %s <command> [<flags>] [file1 dir1 ... fileOrDirN]\n\n", filepath.Base(os.Args[0])) _, _ = fmt.Fprintf(os.Stderr, "Usage: %s <command> [<flags>] [file1 dir1 ... fileOrDirN]\n\n", filepath.Base(os.Args[0]))
_, _ = fmt.Fprintf(os.Stderr, "\nCommands:\n") _, _ = fmt.Fprintf(os.Stderr, "\nCommands:\n")
_, _ = fmt.Fprintf(os.Stderr, "\n convert*\n \tConvert archive or document (default command)\n\n") _, _ = fmt.Fprintf(os.Stderr, "\n convert\n \tConvert archive or document\n\n")
convert.VisitAll(func(f *flag.Flag) { convert.VisitAll(func(f *flag.Flag) {
_, _ = fmt.Fprintf(os.Stderr, " --%s", f.Name) _, _ = fmt.Fprintf(os.Stderr, " --%s", f.Name)
_, _ = fmt.Fprintf(os.Stderr, "\n \t") _, _ = fmt.Fprintf(os.Stderr, "\n \t")
@@ -222,29 +224,39 @@ func parseFlags() (cbconvert.Options, []string) {
if len(os.Args) < 2 { if len(os.Args) < 2 {
convert.Usage() convert.Usage()
_, _ = fmt.Fprintf(os.Stderr, "no arguments\n") _, _ = fmt.Fprintf(os.Stderr, "no command\n")
os.Exit(1) os.Exit(1)
} }
pipe := piped()
if pipe {
args = lines(os.Stdin)
}
switch os.Args[1] { switch os.Args[1] {
case "convert": case "convert":
_ = convert.Parse(os.Args[2:]) _ = convert.Parse(os.Args[2:])
if !pipe {
args = convert.Args() args = convert.Args()
}
case "cover": case "cover":
opts.Cover = true opts.Cover = true
_ = cover.Parse(os.Args[2:]) _ = cover.Parse(os.Args[2:])
if !pipe {
args = cover.Args() args = cover.Args()
}
case "thumbnail": case "thumbnail":
opts.Thumbnail = true opts.Thumbnail = true
_ = thumbnail.Parse(os.Args[2:]) _ = thumbnail.Parse(os.Args[2:])
if !pipe {
args = thumbnail.Args() args = thumbnail.Args()
}
case "meta": case "meta":
opts.Meta = true opts.Meta = true
_ = meta.Parse(os.Args[2:]) _ = meta.Parse(os.Args[2:])
if !pipe {
args = meta.Args() args = meta.Args()
default: }
_ = convert.Parse(os.Args[1:])
args = convert.Args()
} }
if len(args) == 0 { if len(args) == 0 {
@@ -255,3 +267,30 @@ func parseFlags() (cbconvert.Options, []string) {
return opts, args return opts, args
} }
// piped checks if we have a piped stdin.
func piped() bool {
f, err := os.Stdin.Stat()
if err != nil {
return false
}
if f.Mode()&os.ModeNamedPipe == 0 {
return false
} else {
return true
}
}
// lines returns slice of lines from reader.
func lines(r io.Reader) []string {
data := make([]string, 0)
scanner := bufio.NewScanner(r)
for scanner.Scan() {
text := scanner.Text()
data = append(data, text)
}
return data
}

3
go.work Normal file
View File

@@ -0,0 +1,3 @@
go 1.21.0
use ./cmd/cbconvert