espanso / espanso

Cross-platform Text Expander written in Rust
https://espanso.org
GNU General Public License v3.0
9.85k stars 272 forks source link

Cannot cat files without producing some output prior (wayland only) #1980

Open DevGuyRash opened 3 months ago

DevGuyRash commented 3 months ago

Description

Code

global_vars:
  - name: find_file
    type: shell
    params:
      cmd: >
        CONFIG_PATH=$(readlink -f "$CONFIG");
        FILEPATH=$(find "$CONFIG_PATH" -regextype posix-extended -iregex ".*$ESPANSO_FILE\.(md|txt)" -type f | head -n 1);
        if [[ -n "$FILEPATH" ]]; then
          cat "$FILEPATH";
        else
          echo "File not found: $ESPANSO_FILE";
        fi;
      debug: true

matches:
  - regex: ";(?P<file>[^;]+);"
    replace: "{{find_file}}"

Issue

When running in a wayland environment with the backend set to clipboard, espanso does not output anything when using cat as part of the shell extension. I have even created a simpler version of this that only runs cat on a file I created specifically for this. Both that simple version and this version work when run in terminal line by line.

I have found that if you add even a single letter before {{find_file}} in the replace, or if you echo out a single letter prior to trying to cat, then it will work as expected.

After checking the logs, even though nothing is produced whenever it tries to cat, the contents of the file actually do still show up in the logs under stdout. They are just not being pasted into wherever my cursor is.

This script works fine when the backend is inject and on x11

Settings

Espanso Settings

[espanso config: "/home/.../.config/espanso/config/default.yml"]

backend: Auto
enable: true
paste_shortcut: Some("CTRL+V")
inject_delay: None
key_delay: None
apply_patch: true
word_separators: [" ", ",", ".", "?", "!", "\r", "\n", "\u{16}"]

preserve_clipboard: true
clipboard_threshold: 100
disable_x11_fast_inject: false
pre_paste_delay: 100
paste_shortcut_event_delay: 10
toggle_key: None
auto_restart: true
restore_clipboard_delay: 300 
post_form_delay: 200 
post_search_delay: 200 
backspace_limit: 5
search_trigger: None
search_shortcut: Some("ALT+SPACE")
keyboard_layout: Some(RMLVOConfig { rules: Some("evdev"), model: Some("pc105"), layout: Some("us"), variant: Some(""), options: Some("") })

show_icon: true
show_notifications: true
secure_input_notification: true

x11_use_xclip_backend: false
x11_use_xdotool_backend: false
win32_exclude_orphan_events: true
win32_keyboard_layout_cache_interval: 2000

PC Settings

Steps to reproduce

  1. Launch on espanso on wayland
  2. Create a match meant to use the shell extension and have it try to cat a file with no additional output other than the cat.
  3. Try the match out

Expected behavior

It should paste the contents of the file into where my cursor is

Screenshots

No response

Logs

23:09:24 [worker(316078)] [INFO] exiting worker process...
23:09:24 [daemon(292901)] [INFO] spawning the worker process...
23:09:24 [worker(318657)] [INFO] reading configs from: "/home/rashino/.config/espanso"
23:09:24 [worker(318657)] [INFO] reading packages from: "/home/rashino/.config/espanso/match/packages"
23:09:24 [worker(318657)] [INFO] using runtime dir: "/home/rashino/.cache/espanso"
23:09:24 [worker(318657)] [INFO] system info: Arch Linux vrolling - kernel: 6.6.32-1-lts
23:09:24 [worker(318657)] [INFO] binded to IPC unix socket: /home/rashino/.cache/espanso/espansoworkerv2.sock
23:09:24 [worker(318657)] [INFO] using WaylandAppInfoProvider
23:09:24 [worker(318657)] [INFO] monitoring the status of the daemon process
23:09:24 [worker(318657)] [INFO] detection module will use this keyboard layout: [R=evdev, M=pc105, L=us, V=, O=]
23:09:24 [worker(318657)] [INFO] using EVDEVSource
23:09:25 [worker(318657)] [INFO] Querying modifier status...
23:09:26 [worker(318657)] [INFO] inject module will use this keyboard layout: [R=evdev, M=pc105, L=us, V=, O=]
23:09:26 [worker(318657)] [INFO] using EVDEVInjector
23:09:26 [worker(318657)] [INFO] using WaylandFallbackClipboard
23:18:17 [worker(318657)] [INFO] debug information for command> CONFIG_PATH=$(readlink -f "$CONFIG"); FILEPATH=$(find "$CONFIG_PATH" -regextype posix-extended -iregex ".*$ESPANSO_FILE\.(md|txt)" -type f | head -n 1); if [[ -n "$FILEPATH" ]]; then
cat "$FILEPATH";
else
echo "File not found: $ESPANSO_FILE";
fi;

23:18:17 [worker(318657)] [INFO] exit status: 'exit status: 0'
23:18:17 [worker(318657)] [INFO] stdout: 'From the following data that I provide:

1. List all key (data points|highlights|takeaways)
1.1 Prefer not to use sub-bulletpoints unless necessary
1.2 If the data involves concepts or terms, list their definition based on the data as a bullet point in the form **<term or concept> Definition**: <definition>
1.2.1 All definitions go at the top of the list
1.3 Prefer not to use anaphoric or cataphoric references
1.4. Remember that key highlights should not be excessively long. Prefer succinct and concise points.
2. Determine if those key data points capture all the nuances and would satisfy a "summary" of the data if only those key points were read.
3. Suggest improvements or additions based on context, using the data as the source
4. Create a final version of the key points in list format and output them by themselves inside of a code block
Here is the data:
~~~

~~~
'
23:18:17 [worker(318657)] [INFO] stderr: ''
23:18:17 [worker(318657)] [INFO] this debug information was shown because the 'debug' option is true.

Your environment

Self-service

smeech commented 3 months ago

My apologies, but I'm not sure if I understand the problem.

You're currently using backend: auto, which will use inject for short expansions, and switch to clipboard for those longer than the number of characters specified in your clipboard_threshold: 100 setting. If clipboard doesn't work in that situation, nothing will be returned.

If the routine works using the inject backend, simply use it. You probably need an app-specific configuration for all expansions in that particular program, or, in the unlikely situation where this is the only expansion failing, add the line force_mode: keys to the regex:, replace: pair.

DevGuyRash commented 3 months ago

The issue with inject for long form content is that on many platforms when a newline is injected it causes the form to submit. For example, when prompting on chatgpt or on other chat-oriented applications. It also creates issues in IDEs when programming due to the settings of the IDEs which try to insert the matching delimiter characters. It is also much slower.

App-specific configurations are unfortunately unsupported on wayland. I will try the force_mode setting later today. This issue is specifically on wayland and is a bug with the clipboard. To better establish the issue:

Lets say I have an espanso match that uses the shell extension and simply outputs whatever the cmd is. Given the following file and match, there are two ways to output the file's contents that will work:

foo.txt:

Hello, World!

bar.yml:

  - regex: ':file'
    replace: "{{replace}}"
    vars:
      - name: replace
        type: shell
        params:
          cmd: cat foo.txt

Method 1: Modifying cmd

Given this file, here are the outcomes of the cmd:

# This original one would fail to output anything at all
cat foo.txt

# This one would output the file correctly and the clipboard would work
echo -n "prefix:";
cat foo.txt;

Unless something is output prior to a cat, it will fail to output anything. The logs will show that stdout contains the contents of the file even upon failure.

Method 2: Modifying replace

The alternative is to leave the match the same and add a prefix character (even just a single one). The file will then also correctly output its contents:

  - regex: ':file'
    replace: "a{{replace}}"  # The letter 'a' was added. It has to be before
    vars:
      - name: replace
        type: shell
        params:
          cmd: cat foo.txt

The overall issue here is that the clipboard only seems to work on wayland when there is a prefix character to initialize it.

smeech commented 3 months ago

Dear @DevGuyRash

Apologies for hijacking this thread, but I've been reviewing your "spell-check" PR at https://github.com/espanso/hub/pull/89, and raised a number of issues that need fixing before we can merge.

I don't know if you've been notified of these (and it would be interesting to hear, as you're not the only person who hasn't responded!), so thought I'd try and make direct contact.

Let me know if you need any help with the amendments. Whilst I could probably make them, I think it's preferable if it's your own work.

With best wishes,