Skip to content

Instantly share code, notes, and snippets.

@tylerlmz1
Created June 4, 2019 09:15
Show Gist options
  • Select an option

  • Save tylerlmz1/c8537af3656b8793fdc4b06d77ff3806 to your computer and use it in GitHub Desktop.

Select an option

Save tylerlmz1/c8537af3656b8793fdc4b06d77ff3806 to your computer and use it in GitHub Desktop.

Revisions

  1. tylerlmz1 created this gist Jun 4, 2019.
    99 changes: 99 additions & 0 deletions snoit.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    #snoit.ps1
    #snoit stands for 'search n open in typora'

    #### For Linux
    # $typora_path = 'typora'


    #### For Windows
    $typora_path = 'C:\Program Files\Typora\typora.exe'

    function welcome_message{
    Write-Host "[Open folder in Typora]"
    Write-Host " "
    }
    function search_return_open {
    #prompt for search string
    Write-Host "(-2 to Clear-Host)" -ForegroundColor Gray
    [string]$search_string = Read-Host -Prompt "Search folder"
    Write-Host " "

    #Check user input for $search_string
    if($search_string.Length -eq 0){
    Write-Host "Nothing was inputted" -ForegroundColor Red
    Write-Host " "
    return
    }elseif($search_string -eq -2){
    Clear-Host
    welcome_message
    return

    }

    #get results' full paths into an array
    $folder_result=@()
    $folder_result+=Get-ChildItem -Recurse -Directory |
    Where-Object -Property Name -Like "*$search_string*" |
    Select-Object -expandproperty FullName

    #Check $folder_result
    if ($folder_result.count -eq 0){
    Write-Host "No results" -ForegroundColor Red
    Write-Host " "
    return
    }

    ### print search result along their index numbers ###
    $script_current_folder = Get-Location | Split-Path -Leaf
    $current_dir_name_length = $script_current_folder.Length

    for ($i = 0; $i -le ($folder_result.length - 1); $i++) {
    $path_string = $folder_result[$i]
    $leaf_path = Split-Path $path_string -Leaf
    $truncated_path_to_print = $path_string.Substring($path_string.IndexOf($script_current_folder)+$current_dir_name_length)

    Write-Host $i : $leaf_path -ForegroundColor Yellow
    Write-Host ".${truncated_path_to_print}"
    Write-Host " "
    }

    #prompt for folder to open
    Write-Host "(-1 to search again)" -ForegroundColor Gray
    Write-Host "(-2 to Clear-Host)" -ForegroundColor Gray
    $index_number =Read-Host -Prompt "index number N "
    Write-Host " "
    $folder_to_open_path = $folder_result[$index_number]

    ### Handling user input of $index_number ###

    #reject if string is inputted
    try{
    [int]$index_number = $index_number
    }
    catch{
    Write-Host "String not allowed`n" -ForegroundColor Red
    return
    }

    #sentinel quit/launch folder/invalid index number
    if($index_number -eq -2){
    Clear-Host
    welcome_message
    }elseif($index_number -eq -1){
    Write-Host "going back to search" -ForegroundColor Red
    Write-Host " "
    } elseif ($index_number -lt $folder_result.length){
    & $typora_path $folder_to_open_path | out-null #launch target folder in typora
    Clear-Host
    welcome_message
    } else {
    Write-Host "Invalid index number" -ForegroundColor Red
    Write-Host " "
    }

    }

    welcome_message
    while (1) {
    search_return_open
    }