package find class FindBehaviour( val form : FindForm ) : TableBehaviour( form.title ), Promptable { var finished = false var message = "" val refresh = Action( "Refresh", this:>refresh ) .icon("refresh").shortcut( Key.F5.noMods() ) init { columns = listOf( TableColumn("Name", 350), TableColumn("Folder") ) } override meth toolbar() = listOf( form.searchText, form.startFolder, separator, form.includeFiles, form.includeFolders, form.includeLinks, separator, form.includeHidden, separator, refresh ) override meth attached( container : Container ) { super.attached( container ) refresh() } meth refresh() { rows.clear() finished = false message = "Collecting data. Please wait..." val types = listOf() var firstType = true var typesString = StringBuilder().apply { if (form.includeFiles.value) { if (firstType) { firstType = false } else { append(",") } append("f") } if (form.includeLinks.value) { if (firstType) { firstType = false } else { append(",") } append("l") } if (form.includeFolders.value) { if (firstType) { firstType = false } else { append(",") } append("d") } }.toString() var command : CommandBase = $( find . -maxdepth ${form.maxDepth.value} -type $typesString ) if ( ! form.includeHidden.value ) { command = command | $( grep -v '/\.' ) } if (!form.searchText.value.isBlank()) { // We ALWAYS pass a regex to grep, so if info.regex == false, then we need to escape special characters. val searchText = if ( form.useRegex.value ) { form.searchText.value } else { escapeExtendedGrepSpecialCharacters( form.searchText.value ) } // Search only in the filename, not in the folder names that preceed it. // This is why we ALWAYS pass a regex to grep. val grepRegex = "$searchText[^/]*$" val caseFlag = if (form.caseSensitive.value) "" else "-i" command = command | $( grep -E $caseFlag '${grepRegex}' ) } command = command | $( sort --ignore-case ) //println( "${info.startFolder.value} Command : $command" ) evalInNewThread( command.dir( form.startFolder.value ), "Find", this:>success, this:>failure ) } meth success( output : String ) { //println( "Success :\n$output") message = "" for (line in output.split( "\n" ) ) { if (line.size() < 2) continue val stripped = line.substring( 2 ) val file = File( form.startFolder.value, stripped ) rows.add( FindRow( file ) ) } if (rows.isEmpty()) { message = "No items found" } else { finished = true } } meth failure( message : String ) { finished = false this.message = "Failed : $message" } override meth promptForm() = form }