V: clipboard : Can't copy on Linux unless using new_primary

Created on 11 May 2020  路  4Comments  路  Source: vlang/v

Wayland support can be implemented by executing wl-copy and wl-paste from wl-clipboard it needs to be installed but if someone need it he can do that and for packaging the binaries of it can be bundled with the v application
As for Termux on android there is an official addon that can be installed Termux:API which have termux-clipboard-get and termux-clipboard-set

A go library use these and to check for the existence of these programs it uses the standard Go library function exec.LookPath() which search for a program in all paths specified in the system environment.
Refer to Go source for how it was implemented, I tried porting it but with no luck I'm just starting to learn V. So i'm submitting this for those who want to contribute

Feature Request

Most helpful comment

@asvvvad os.find_abs_path_of_executable is the thing you're looking for.

// find_exe_path walks the environment PATH, just like most shell do, it returns
// the absolute path of the executable if found
pub fn find_abs_path_of_executable(exepath string) ?string {
    if os.is_abs_path(exepath) {
        return exepath
    }
    mut res := ''
    env_path_delimiter := if os.user_os() == 'windows' { ';' } else { ':' }
    paths := os.getenv('PATH').split(env_path_delimiter)
    for p in paths {
        found_abs_path := os.join_path( p, exepath )
        if os.exists( found_abs_path ) && os.is_executable( found_abs_path ) {
            res = found_abs_path
            break
        }
    }
    if res.len>0 {
        return res
    }
    return error('failed to find executable')
}

All 4 comments

@asvvvad os.find_abs_path_of_executable is the thing you're looking for.

// find_exe_path walks the environment PATH, just like most shell do, it returns
// the absolute path of the executable if found
pub fn find_abs_path_of_executable(exepath string) ?string {
    if os.is_abs_path(exepath) {
        return exepath
    }
    mut res := ''
    env_path_delimiter := if os.user_os() == 'windows' { ';' } else { ':' }
    paths := os.getenv('PATH').split(env_path_delimiter)
    for p in paths {
        found_abs_path := os.join_path( p, exepath )
        if os.exists( found_abs_path ) && os.is_executable( found_abs_path ) {
            res = found_abs_path
            break
        }
    }
    if res.len>0 {
        return res
    }
    return error('failed to find executable')
}

Thanks for the response and sorry for not writing an actual reply sooner.
That's what I was looking however I had to make a custom function to check for the existence of prog in $PATH I just started learning about V so it's confusing due to the missing documentation
Anyway,
What I did was: Make a simple wrapper for the clipboard with added support for these functions, unfortunately I can't test it but it should work.

It took me a while to get it work and the last but that kept giving a segmentation fault and there was no instructions on how to do it neither but after reading the clipboard_test.v (which is unfinished) and clipboard_linux.v I figured to use new_primary and it worked

Suggestions:

os.v

// exists_in_path returns true if prog exists in the system's path
fn exists_in_path(prog string) bool {
    os.find_abs_path_of_executable(prog) or {
        return false
    }
    return true
}

it allows for if !(exists_in_path('wl-copy') && exists_in_path('wl-paste')) { which I needed for this and may be needed by someone else. I couldn't find any easy way to do this but for a new function

clipboard.v:

// create a new clipboard
pub fn new() &Clipboard {
    $if linux {
        return new_primary()
    }

    return new_clipboard()
}

The clipboard test seem to work however it doesnt allow copying on the script I wrote which I just uploaded and you can inspect here

Looks like you solved it yourself! Thanks for contributing to V.

Oh yes I did sorry for not closing was looking for an answer to the primary clipboard thing but since the test worked it's on my side probably. Thanks for closing it ^^

Was this page helpful?
0 / 5 - 0 ratings

Related issues

elimisteve picture elimisteve  路  3Comments

taojy123 picture taojy123  路  3Comments

penguindark picture penguindark  路  3Comments

oleg-kachan picture oleg-kachan  路  3Comments

cjmxp picture cjmxp  路  3Comments