Workbench - Improved search box
Can now find exact matches using = or use regexp if starting with /
This commit is contained in:
parent
b7448b213d
commit
6d39c1e565
1 changed files with 36 additions and 1 deletions
|
@ -4,6 +4,9 @@ package mmm.tech.base.workbench;
|
|||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
|
@ -224,10 +227,42 @@ public class TBWBGui
|
|||
this.recipes.clear( );
|
||||
}
|
||||
|
||||
Predicate< String > matcher;
|
||||
if ( newText.charAt( 0 ) == '=' && newText.length( ) > 1 ) {
|
||||
final String searchText = newText.substring( 1 );
|
||||
matcher = ( x ) -> x.equals( searchText );
|
||||
} else {
|
||||
Pattern pattern;
|
||||
if ( newText.charAt( 0 ) == '/' && newText.length( ) > 1 ) {
|
||||
try {
|
||||
pattern = Pattern.compile( newText.substring( 1 ) ,
|
||||
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE );
|
||||
} catch ( final PatternSyntaxException e ) {
|
||||
pattern = null;
|
||||
}
|
||||
} else {
|
||||
pattern = null;
|
||||
}
|
||||
|
||||
if ( pattern != null ) {
|
||||
matcher = pattern.asPredicate( );
|
||||
} else {
|
||||
final String[] parts = newText.split( "\\s+" );
|
||||
matcher = ( x ) -> {
|
||||
for ( int i = 0 ; i < parts.length ; i++ ) {
|
||||
if ( !x.contains( parts[ i ] ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
final int nRecipes = fullList.size( );
|
||||
for ( int i = 0 ; i < nRecipes ; i++ ) {
|
||||
final I_CraftingRecipeWrapper recipe = fullList.get( i );
|
||||
if ( I18n.format( recipe.getName( ) ).toLowerCase( ).contains( newText ) ) {
|
||||
if ( matcher.test( I18n.format( recipe.getName( ) ).toLowerCase( ) ) ) {
|
||||
this.recipes.add( recipe );
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue