Skip to content

Instantly share code, notes, and snippets.

@evil5hadow
Forked from RichardBronosky/README.MD
Created January 15, 2019 12:37
Show Gist options
  • Save evil5hadow/a55ce980ec79c44cadd061774d5b40d0 to your computer and use it in GitHub Desktop.
Save evil5hadow/a55ce980ec79c44cadd061774d5b40d0 to your computer and use it in GitHub Desktop.

Revisions

  1. @RichardBronosky RichardBronosky revised this gist Dec 12, 2018. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions README.MD
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@

    # cb
    ## A leak-proof tee to the clipboard

    @@ -11,8 +12,9 @@ It's like your normal copy and paste commands, but unified and able to sense whe
    ### Copy

    ```bash
    $ date | cb
    $ echo -n $(date) | cb
    # clipboard contains: Tue Jan 24 23:00:00 EST 2017
    # but, because of `echo -n` there is no newline at the end
    ```

    ### Paste
    @@ -21,9 +23,11 @@ $ date | cb
    # clipboard retained from the previous block
    $ cb
    Tue Jan 24 23:00:00 EST 2017
    # above there is a newline after the output for readability
    # below there is no newline because cb recognized that it was outputing to a pipe and any alterations would "contaminate" the data
    $ cb | cat
    Tue Jan 24 23:00:00 EST 2017
    $ cb > foo
    Tue Jan 24 23:00:00 EST 2017$ cb > foo
    # look carefully at this ^^ that's a new $ prompt at the end of the content exactly as copied
    $ cat foo
    Tue Jan 24 23:00:00 EST 2017
    ```
  2. @draketyl draketyl revised this gist Nov 27, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cb
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ else
    t1=0
    fi

    if grep -q Microsoft /proc/version; then
    if [[ -f /proc/version ]] && grep -q Microsoft /proc/version; then
    os="WSL"
    else
    unameOut="$(uname -s)"
  3. @draketyl draketyl revised this gist Nov 27, 2018. 1 changed file with 0 additions and 0 deletions.
    Empty file modified cb
    100644 → 100755
    Empty file.
  4. @RichardBronosky RichardBronosky revised this gist Aug 3, 2018. 4 changed files with 75 additions and 117 deletions.
    75 changes: 75 additions & 0 deletions cb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    #!/bin/bash

    if [[ -p /dev/stdin ]]; then # stdin is a pipe
    p0=1
    else
    p0=0
    fi

    if [[ -t 0 ]]; then # stdin is a tty
    t0=1
    else
    t0=0
    fi

    if [[ -t 1 ]]; then # stdout is a tty
    t1=1
    else
    t1=0
    fi

    if grep -q Microsoft /proc/version; then
    os="WSL"
    else
    unameOut="$(uname -s)"
    case "${unameOut}" in
    Linux*) os=LINUX;;
    Darwin*) os=MAC;;
    CYGWIN*) os=CYGWIN;;
    esac
    fi

    LINUX_copy(){
    cat | xclip -selection clipboard
    }

    LINUX_paste(){
    xclip -selection clipboard -o
    }

    WSL_copy(){
    cat | /mnt/c/Windows/System32/clip.exe
    }

    WSL_paste(){
    /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe Get-Clipboard | sed 's/\r//'
    }

    CYGWIN_copy(){
    cat > /dev/clipboard
    }

    CYGWIN_paste(){
    cat /dev/clipboard
    }


    MAC_copy(){
    cat | pbcopy
    }

    MAC_paste(){
    pbpaste
    }

    if [[ $p0 -eq 1 || $t0 -eq 0 ]]; then # stdin is pipe-ish
    ${os}_copy # so send it to the clipboard
    if [[ $t1 -eq 0 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
    ${os}_paste # so pass that pipe/redirection the content of the clipboard (enables `man tee` like chaining)
    fi
    else # stdin is not a pipe
    ${os}_paste # so output the clipboard
    if [[ $t1 -eq 1 ]]; then # stdout is a tty (so we don't have to be strict about not altering the output)
    echo # prevent the prompt from being on the same line
    fi
    fi
    39 changes: 0 additions & 39 deletions cb.cygwin.sh
    Original file line number Diff line number Diff line change
    @@ -1,39 +0,0 @@
    #!/bin/bash

    if [[ -p /dev/stdin ]]; then # stdin is a pipe
    p0=1
    else
    p0=0
    fi

    if [[ -t 0 ]]; then # stdin is a tty
    t0=1
    else
    t0=0
    fi

    if [[ -t 1 ]]; then # stdout is a tty
    t1=1
    else
    t1=0
    fi

    _copy(){
    cat > /dev/clipboard
    }

    _paste(){
    cat /dev/clipboard
    }

    if [[ $p0 -eq 1 || $t0 -eq 0 ]]; then # stdin is pipe-ish
    _copy # so send it to the clipboard
    if [[ $t1 -eq 0 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
    _paste # so pass that pipe/redirection the content of the clipboard (enables `man tee` like chaining)
    fi
    else # stdin is not a pipe
    _paste # so output the clipboard
    if [[ $t1 -eq 1 ]]; then # stdout is a tty (so we don't have to be strict about not altering the output)
    echo # prevent the prompt from being on the same line
    fi
    fi
    39 changes: 0 additions & 39 deletions cb.linux.sh
    Original file line number Diff line number Diff line change
    @@ -1,39 +0,0 @@
    #!/bin/bash

    if [[ -p /dev/stdin ]]; then # stdin is a pipe
    p0=1
    else
    p0=0
    fi

    if [[ -t 0 ]]; then # stdin is a tty
    t0=1
    else
    t0=0
    fi

    if [[ -t 1 ]]; then # stdout is a tty
    t1=1
    else
    t1=0
    fi

    _copy(){
    cat | xclip -selection clipboard
    }

    _paste(){
    xclip -selection clipboard -o
    }

    if [[ $p0 -eq 1 || $t0 -eq 0 ]]; then # stdin is pipe-ish
    _copy # so send it to the clipboard
    if [[ $t1 -eq 0 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
    _paste # so pass that pipe/redirection the content of the clipboard (enables `man tee` like chaining)
    fi
    else # stdin is not a pipe
    _paste # so output the clipboard
    if [[ $t1 -eq 1 ]]; then # stdout is a tty (so we don't have to be strict about not altering the output)
    echo # prevent the prompt from being on the same line
    fi
    fi
    39 changes: 0 additions & 39 deletions cb.osx.sh
    Original file line number Diff line number Diff line change
    @@ -1,39 +0,0 @@
    #!/bin/bash

    if [[ -p /dev/stdin ]]; then # stdin is a pipe
    p0=1
    else
    p0=0
    fi

    if [[ -t 0 ]]; then # stdin is a tty
    t0=1
    else
    t0=0
    fi

    if [[ -t 1 ]]; then # stdout is a tty
    t1=1
    else
    t1=0
    fi

    _copy(){
    cat | pbcopy
    }

    _paste(){
    pbpaste
    }

    if [[ $p0 -eq 1 || $t0 -eq 0 ]]; then # stdin is pipe-ish
    _copy # so send it to the clipboard
    if [[ $t1 -eq 0 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
    _paste # so pass that pipe/redirection the content of the clipboard (enables `man tee` like chaining)
    fi
    else # stdin is not a pipe
    _paste # so output the clipboard
    if [[ $t1 -eq 1 ]]; then # stdout is a tty (so we don't have to be strict about not altering the output)
    echo # prevent the prompt from being on the same line
    fi
    fi
  5. @RichardBronosky RichardBronosky revised this gist Aug 3, 2018. 3 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  6. @RichardBronosky RichardBronosky revised this gist Jun 26, 2017. 3 changed files with 36 additions and 12 deletions.
    16 changes: 12 additions & 4 deletions cb (for Cygwin)
    Original file line number Diff line number Diff line change
    @@ -18,14 +18,22 @@ else
    t1=0
    fi

    _copy(){
    cat > /dev/clipboard
    }

    _paste(){
    cat /dev/clipboard
    }

    if [[ $p0 -eq 1 || $t0 -eq 0 ]]; then # stdin is pipe-ish
    cat > /dev/clipboard # so send it to the pasteboard
    _copy # so send it to the clipboard
    if [[ $t1 -eq 0 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
    cat /dev/clipboard # so pass that pipe/redirection the content of the pasteboard (enables `man tee` like chaining)
    _paste # so pass that pipe/redirection the content of the clipboard (enables `man tee` like chaining)
    fi
    else # stdin is not a pipe
    cat /dev/clipboard # so output the pasteboard
    _paste # so output the clipboard
    if [[ $t1 -eq 1 ]]; then # stdout is a tty (so we don't have to be strict about not altering the output)
    echo # prevent the prompt from being on the same line
    fi
    fi
    fi
    16 changes: 12 additions & 4 deletions cb (for Linux)
    Original file line number Diff line number Diff line change
    @@ -18,14 +18,22 @@ else
    t1=0
    fi

    _copy(){
    cat | xclip -selection clipboard
    }

    _paste(){
    xclip -selection clipboard -o
    }

    if [[ $p0 -eq 1 || $t0 -eq 0 ]]; then # stdin is pipe-ish
    cat | xclip -selection clipboard # so send it to the pasteboard
    _copy # so send it to the clipboard
    if [[ $t1 -eq 0 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
    xclip -selection clipboard -o # so pass that pipe/redirection the content of the pasteboard (enables `man tee` like chaining)
    _paste # so pass that pipe/redirection the content of the clipboard (enables `man tee` like chaining)
    fi
    else # stdin is not a pipe
    xclip -selection clipboard -o # so output the pasteboard
    _paste # so output the clipboard
    if [[ $t1 -eq 1 ]]; then # stdout is a tty (so we don't have to be strict about not altering the output)
    echo # prevent the prompt from being on the same line
    fi
    fi
    fi
    16 changes: 12 additions & 4 deletions cb (for OSX)
    Original file line number Diff line number Diff line change
    @@ -18,14 +18,22 @@ else
    t1=0
    fi

    _copy(){
    cat | pbcopy
    }

    _paste(){
    pbpaste
    }

    if [[ $p0 -eq 1 || $t0 -eq 0 ]]; then # stdin is pipe-ish
    cat | pbcopy # so send it to the pasteboard
    _copy # so send it to the clipboard
    if [[ $t1 -eq 0 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
    pbpaste # so pass that pipe/redirection the content of the pasteboard (enables `man tee` like chaining)
    _paste # so pass that pipe/redirection the content of the clipboard (enables `man tee` like chaining)
    fi
    else # stdin is not a pipe
    pbpaste # so output the pasteboard
    _paste # so output the clipboard
    if [[ $t1 -eq 1 ]]; then # stdout is a tty (so we don't have to be strict about not altering the output)
    echo # prevent the prompt from being on the same line
    fi
    fi
    fi
  7. @RichardBronosky RichardBronosky revised this gist Mar 13, 2017. 3 changed files with 69 additions and 6 deletions.
    29 changes: 25 additions & 4 deletions cb (for Cygwin)
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,31 @@
    #!/bin/bash

    if [[ -p /dev/stdin ]]; then # stdin is a pipe
    cat > /dev/clipboard # so send it to the clipboard
    if [[ ! -t 1 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
    cat /dev/clipboard # so pass that pipe/redirection the content of the clipboard (enables `man tee` like chaining)
    p0=1
    else
    p0=0
    fi

    if [[ -t 0 ]]; then # stdin is a tty
    t0=1
    else
    t0=0
    fi

    if [[ -t 1 ]]; then # stdout is a tty
    t1=1
    else
    t1=0
    fi

    if [[ $p0 -eq 1 || $t0 -eq 0 ]]; then # stdin is pipe-ish
    cat > /dev/clipboard # so send it to the pasteboard
    if [[ $t1 -eq 0 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
    cat /dev/clipboard # so pass that pipe/redirection the content of the pasteboard (enables `man tee` like chaining)
    fi
    else # stdin is not a pipe
    cat /dev/clipboard # so output the clipboard
    cat /dev/clipboard # so output the pasteboard
    if [[ $t1 -eq 1 ]]; then # stdout is a tty (so we don't have to be strict about not altering the output)
    echo # prevent the prompt from being on the same line
    fi
    fi
    23 changes: 22 additions & 1 deletion cb (for Linux)
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,31 @@
    #!/bin/bash

    if [[ -p /dev/stdin ]]; then # stdin is a pipe
    p0=1
    else
    p0=0
    fi

    if [[ -t 0 ]]; then # stdin is a tty
    t0=1
    else
    t0=0
    fi

    if [[ -t 1 ]]; then # stdout is a tty
    t1=1
    else
    t1=0
    fi

    if [[ $p0 -eq 1 || $t0 -eq 0 ]]; then # stdin is pipe-ish
    cat | xclip -selection clipboard # so send it to the pasteboard
    if [[ ! -t 1 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
    if [[ $t1 -eq 0 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
    xclip -selection clipboard -o # so pass that pipe/redirection the content of the pasteboard (enables `man tee` like chaining)
    fi
    else # stdin is not a pipe
    xclip -selection clipboard -o # so output the pasteboard
    if [[ $t1 -eq 1 ]]; then # stdout is a tty (so we don't have to be strict about not altering the output)
    echo # prevent the prompt from being on the same line
    fi
    fi
    23 changes: 22 additions & 1 deletion cb (for OSX)
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,31 @@
    #!/bin/bash

    if [[ -p /dev/stdin ]]; then # stdin is a pipe
    p0=1
    else
    p0=0
    fi

    if [[ -t 0 ]]; then # stdin is a tty
    t0=1
    else
    t0=0
    fi

    if [[ -t 1 ]]; then # stdout is a tty
    t1=1
    else
    t1=0
    fi

    if [[ $p0 -eq 1 || $t0 -eq 0 ]]; then # stdin is pipe-ish
    cat | pbcopy # so send it to the pasteboard
    if [[ ! -t 1 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
    if [[ $t1 -eq 0 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
    pbpaste # so pass that pipe/redirection the content of the pasteboard (enables `man tee` like chaining)
    fi
    else # stdin is not a pipe
    pbpaste # so output the pasteboard
    if [[ $t1 -eq 1 ]]; then # stdout is a tty (so we don't have to be strict about not altering the output)
    echo # prevent the prompt from being on the same line
    fi
    fi
  8. @RichardBronosky RichardBronosky revised this gist Jan 25, 2017. 1 changed file with 17 additions and 14 deletions.
    31 changes: 17 additions & 14 deletions README.MD
    Original file line number Diff line number Diff line change
    @@ -17,23 +17,26 @@ $ date | cb

    ### Paste

    # clipboard retained from the previous block
    $ cb
    Tue Jan 24 23:00:00 EST 2017
    $ cb | cat
    Tue Jan 24 23:00:00 EST 2017
    $ cb > foo
    $ cat foo
    Tue Jan 24 23:00:00 EST 2017
    ```bash
    # clipboard retained from the previous block
    $ cb
    Tue Jan 24 23:00:00 EST 2017
    $ cb | cat
    Tue Jan 24 23:00:00 EST 2017
    $ cb > foo
    $ cat foo
    Tue Jan 24 23:00:00 EST 2017
    ```

    ### Chaining

    $ date | cb | tee updates.log
    Tue Jan 24 23:11:11 EST 2017
    $ cat updates.log
    Tue Jan 24 23:11:11 EST 2017
    # clipboard contains: Tue Jan 24 23:11:11 EST 2017

    ```bash
    $ date | cb | tee updates.log
    Tue Jan 24 23:11:11 EST 2017
    $ cat updates.log
    Tue Jan 24 23:11:11 EST 2017
    # clipboard contains: Tue Jan 24 23:11:11 EST 2017
    ```

    [1]: https://gist.github.com/RichardBronosky/56d8f614fab2bacdd8b048fb58d0c0c7
    [2]:
  9. @RichardBronosky RichardBronosky revised this gist Jan 25, 2017. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions README.MD
    Original file line number Diff line number Diff line change
    @@ -10,9 +10,11 @@ It's like your normal copy and paste commands, but unified and able to sense whe

    ### Copy

    $ date | cb
    # clipboard contains: Tue Jan 24 23:00:00 EST 2017

    ```bash
    $ date | cb
    # clipboard contains: Tue Jan 24 23:00:00 EST 2017
    ```

    ### Paste

    # clipboard retained from the previous block
  10. @RichardBronosky RichardBronosky renamed this gist Jan 25, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  11. @RichardBronosky RichardBronosky renamed this gist Jan 25, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  12. @RichardBronosky RichardBronosky revised this gist Jan 25, 2017. 1 changed file with 38 additions and 0 deletions.
    38 changes: 38 additions & 0 deletions cb.MD
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    # cb
    ## A leak-proof tee to the clipboard

    This script is modeled after `tee` (see [`man tee`][2]).

    It's like your normal copy and paste commands, but unified and able to sense when you want it to be chainable

    ## Examples


    ### Copy

    $ date | cb
    # clipboard contains: Tue Jan 24 23:00:00 EST 2017

    ### Paste

    # clipboard retained from the previous block
    $ cb
    Tue Jan 24 23:00:00 EST 2017
    $ cb | cat
    Tue Jan 24 23:00:00 EST 2017
    $ cb > foo
    $ cat foo
    Tue Jan 24 23:00:00 EST 2017

    ### Chaining

    $ date | cb | tee updates.log
    Tue Jan 24 23:11:11 EST 2017
    $ cat updates.log
    Tue Jan 24 23:11:11 EST 2017
    # clipboard contains: Tue Jan 24 23:11:11 EST 2017


    [1]: https://gist.github.com/RichardBronosky/56d8f614fab2bacdd8b048fb58d0c0c7
    [2]:
    http://man7.org/linux/man-pages/man1/tee.1.html
  13. @RichardBronosky RichardBronosky created this gist Jan 25, 2017.
    10 changes: 10 additions & 0 deletions cb (for Cygwin)
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    #!/bin/bash

    if [[ -p /dev/stdin ]]; then # stdin is a pipe
    cat > /dev/clipboard # so send it to the clipboard
    if [[ ! -t 1 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
    cat /dev/clipboard # so pass that pipe/redirection the content of the clipboard (enables `man tee` like chaining)
    fi
    else # stdin is not a pipe
    cat /dev/clipboard # so output the clipboard
    fi
    10 changes: 10 additions & 0 deletions cb (for Linux)
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    #!/bin/bash

    if [[ -p /dev/stdin ]]; then # stdin is a pipe
    cat | xclip -selection clipboard # so send it to the pasteboard
    if [[ ! -t 1 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
    xclip -selection clipboard -o # so pass that pipe/redirection the content of the pasteboard (enables `man tee` like chaining)
    fi
    else # stdin is not a pipe
    xclip -selection clipboard -o # so output the pasteboard
    fi
    10 changes: 10 additions & 0 deletions cb (for OSX)
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    #!/bin/bash

    if [[ -p /dev/stdin ]]; then # stdin is a pipe
    cat | pbcopy # so send it to the pasteboard
    if [[ ! -t 1 ]]; then # also, stdout is not a tty (meaning it must be a pipe or redirection)
    pbpaste # so pass that pipe/redirection the content of the pasteboard (enables `man tee` like chaining)
    fi
    else # stdin is not a pipe
    pbpaste # so output the pasteboard
    fi