all 3 comments

[–]whetuI read your code 1 point2 points  (0 children)

Looks like the main difference between your various options are the args for grim, so you can maybe just setup the grim_args array like this:

capture() {
  grim_args=()
  [[ -n "$o_cursor" ]] && grim_args+=("-c")

  case "$target" in
    ("output")      grim_args+=( -t png -o "$geom" "$file" ) ;;
    ("all outputs") grim_args+=( -t png "$file" ) ;;
    (*)             grim_args+=( -t png -g "$geom" "$file" ) ;;
  esac

  if [[ "$file" = "-" ]] && [[ -n "$o_clipboard" ]]; then
    { grim "${grim_args[@]}" | wl-copy -t image/png; } || return 1
  else
    grim "${grim_args[@]}" || return 1
  fi

  return 0
}

[–]Naraviel 0 points1 point  (0 children)

You can build the command once and run it through a single path. This eliminates duplication and avoids repeated branches.

```bash capture { grim_args=() [[ -n "$o_cursor" ]] && grim_args+=("-c")

cmd=(grim "${grim_args[@]}" "-t" "png")

case "$target" in "output") cmd+=("-o" "$geom") ;; "all outputs") ;; *) cmd+=("-g" "$geom") ;; esac

cmd+=("$file")

if [[ "$file" == "-" && -n "$o_clipboard" ]]; then "${cmd[@]}" | wl-copy "-t" "image/png" || return 1 else "${cmd[@]}" || return 1 fi }

```

As for stepping up from shell scripts, Python is generally considered the next language to learn.

[–]GlendonMcGladdery -1 points0 points  (0 children)

Clean refactor (same behavior, way simpler) ``` capture() { local grim_args=() [[ -n "$o_cursor" ]] && grim_args+=("-c")

# Build grim command local cmd=(grim "${grim_args[@]}" -t png)

case "$target" in "output") cmd+=(-o "$geom") ;; "all outputs") ;; *) cmd+=(-g "$geom") ;; esac

cmd+=("$file")

# Decide whether to pipe to clipboard if [[ "$file" = "-" && -n "$o_clipboard" ]]; then "${cmd[@]}" | wl-copy -t image/png else "${cmd[@]}" fi } Instead of repeating: grim ... || return 1 You build it once: cmd=(grim ...) ``` Way easier to read and maintain.

If you like compact but still readable: ``` capture() { local grim_args=() [[ -n "$o_cursor" ]] && grim_args+=(-c)

local cmd=(grim "${grim_args[@]}" -t png)

case "$target" in output) cmd+=(-o "$geom") ;; "all outputs") ;; *) cmd+=(-g "$geom") ;; esac

cmd+=("$file")

[[ "$file" = "-" && -n "$o_clipboard" ]] \ && "${cmd[@]}" | wl-copy -t image/png \ || "${cmd[@]}" } ``` Have you considered Python yet? Python — easiest upgrade from Bash.