Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how about adding some dmenu like functions to this? #73

Open
eylles opened this issue Feb 6, 2023 · 3 comments
Open

how about adding some dmenu like functions to this? #73

eylles opened this issue Feb 6, 2023 · 3 comments

Comments

@eylles
Copy link

eylles commented Feb 6, 2023

term-dmenu is as good as abandoned, and there is some space for dmenu-like software other than rofi and it's clones, not to mention that term-dmenu forces abduco as a dependency

personally i cobbled this "improvement" ove the original gist rather quickly so quite some edge cases and unexpected stuff appeared on the long run https://gist.github.com/MahouShoujoMivutilde/b1fa83bc234b68dd164cb6e843965d75?permalink_comment_id=4331478#gistcomment-4331478

so, what do you say would you like to collab on adding a refined iteration of the script from the gist into this project?

@Biont
Copy link
Owner

Biont commented Feb 7, 2023

Hi thanks for your issue. Can you please summarize again what it is you intend to add/change? I couldn't quite tell that from the script you posted.

@eylles
Copy link
Author

eylles commented Feb 8, 2023

sorry, i am sometimes a little cryptic, specially since that was late yesternight.

what i mean is that sway launcher is already so much of a dmenu replacement, at least on the "basic" use case of dmenu, a launcher for programs, however not so much on the other use case of being an interactive filter for arbitrary newline separated data (unless i missed something, granted i have not throughly read the script tho), which is what the script i posted tries to do, altho in a less than ideal way (it has issues like allowing empty stdin, the terminal lingering when fzf exits, the program itself not terminating) so i want to see if you would be up for creating a more comprehensive dmenu replacement exploiting the capabilities of fzf

@mavaa
Copy link

mavaa commented Apr 24, 2024

(Edited: Replaced a tmp file for preview text with a variable)

(Edited again: Replaced some temp files with variables. Haven't found a way of avoiding the OUT_FILE, but that might be a kitty specific issue, since I didn't find a way to make it pass the stdout of the launcer on to a variable)

I think the description of this issue is more or less the same as what was asked for in #64 ? I wanted something along these lines, and managed to adjust the script from that issue into something that takes "anythng" separated by newlines and spits out your selected item to stdout. I'm not an expert in bash stuff, so this might be doable in a simpler way, and maybe you could even consider working it into the main script with a separate flag for reading from stdin?

Still consists of two parts, the first is a wrapper script (sway-dmenu-launcher):

#!/usr/bin/env bash

export PROVIDERS_FILE="$HOME/.config/sway-desktop-launcher/stdin.conf"

export OUT_FILE='/tmp/sway-dmenu-launcher_outfile'
export HIST_FILE='/tmp/sway-dmenu-launcher_histfile'

SWAY_LAUNCER_CMD="kitty --title \"kitty-popup\" -e bash -c 'sway-launcher-desktop > $OUT_FILE'"

cleanup() {
  rm -f $OUT_FILE
  rm -f $HIST_FILE # Since I'm using this script for many different things, I don't want to keep history
}
trap cleanup EXIT
cleanup

# Check for the -p flag and write the provided preview text to $PREVIEW_TEXT
if [[ "$1" == "-p" && -n "$2" ]]; then
    export PREVIEW_TEXT="$2"
else
    export PREVIEW_TEXT="dmenu mode"
fi

# Export stdin, separated by newlines, so the terminal process can
# access it
IFS=$(printf '\n')
input=$(cat)
export input

# This function implements the `list_cmd` sway-launcher-desktop provider entry.
#
# For every newline separated entry from STDIN, it prints its 1-based index and the
# first line, followed by an ellipsis if there is more than one line.
list_cmd() {
    echo "$input" | awk '
        BEGIN {
            # RS = "\n";
            # FS = "\n";
            # This is the sway-launcher-desktop `list_cmd` field separator.
            # This way we can print a `list_cmd` line by passing each field as
            # a separate print argument
            OFS = "\034";
        }
        {
            # Fields are lines, hence NF is the number of lines
            LINE = NF > 1 ? $1"..." : $1;
            print LINE,"stdin",LINE;
        }'
}
export CAT_CONTENT=$(list_cmd)

eval "$SWAY_LAUNCER_CMD"

echo $(cat $OUT_FILE)

Second is the config file (stdin.conf):

[stdin]
list_cmd=echo "$CAT_CONTENT"
preview_cmd=echo -e $PREVIEW_TEXT
launch_cmd={1}

Using this, I've been able to replace some scripts I earlier used rofi/wofi for, for instance this one changing focus to a window:

#!/bin/bash

# Function to list windows with workspace, class, and title
list_windows() {
    swaymsg -t get_tree | jq -r '
    recurse(.nodes[]?, .floating_nodes[]?) |
    select(.type=="con" and .nodes==[] and .app_id != null or .window_properties.title != null) |
    [.id, (.name // .app_id), (.window_properties.class // .app_id)] | @csv' | while IFS=, read -r id name class; do
        # Remove quotes added by @csv
        id=$(echo $id | tr -d '"')
        name=$(echo $name | tr -d '"')
        class=$(echo $class | tr -d '"')

        # Output format: ID:|Class|Title
        # Here, for Wayland apps where class may not be available, app_id is used as a fallback
        echo "$id:|$class|$name"
    done
}

# Show window list in wofi and get selection
WOFIRESULT=$(list_windows | $HOME/bin/sway/sway-dmenu-launcher -p "Focus on window")

# Extract the ID from the selection
ID=$(echo "$WOFIRESULT" | awk -F ':|' '{print $1}')

echo $ID

# Focus the window with the given ID
swaymsg "[con_id=$ID]" focus

Or sending an application to a workspace:

#!/bin/bash

# Get a list of existing workspace names
WORKSPACES=$(swaymsg -t get_workspaces | jq -r '.[].name')

# Prompt for a workspace, showing existing ones for autocompletion
DTOP_NAME=$(echo "$WORKSPACES" | $HOME/bin/sway/sway-dmenu-launcher -p "Send to desktop")

# Check if a name was entered
if [ ! -z "$DTOP_NAME" ]; then
    # Move the previously focused window to this workspace
    swaymsg move container to workspace "$DTOP_NAME"
    # Focus back on the workspace to ensure we're viewing the moved window
    swaymsg workspace "$DTOP_NAME"
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants