Skip to content

Instantly share code, notes, and snippets.

@kvenn
Created August 26, 2023 22:25
Show Gist options
  • Save kvenn/c8fec1d198356eec6a3a3983b58b1972 to your computer and use it in GitHub Desktop.
Save kvenn/c8fec1d198356eec6a3a3983b58b1972 to your computer and use it in GitHub Desktop.
shorten-logs-live-plugin
���N�A !H
import com.intellij.openapi.progress.ProcessCanceledException
import liveplugin.PluginUtil
import org.jetbrains.annotations.Nullable
import java.util.function.Function
import java.util.regex.Matcher
import java.util.regex.Pattern
import com.intellij.ide.plugins.IdeaPluginDescriptorImpl
import com.intellij.ide.plugins.PluginManager
import com.intellij.openapi.extensions.PluginId
import java.util.function.Function
import static com.intellij.openapi.util.text.StringUtil.newBombedCharSequence
import static liveplugin.PluginUtil.*
/**
https://github.com/dkandalov/live-plugin/blob/master/src/plugin-util-groovy/liveplugin/PluginUtil.groovy
*/
registerFunction("shorten", new Function<String, String>() {
// public static Pattern pattern = Pattern.compile('flutter: \\[(\\w{4})\\]\\[\\d{4}-\\d{2}-\\d{2} (\\d{2}:\\d{2}:\\d{2}\\.\\d{3,6})]Ping Dev\\.([a-z\\-]+): (.+)$')
public static Pattern pattern = Pattern.compile('flutter: \\[(\\w+)]\\[\\d{4}-\\d{2}-\\d{2} (\\d{2}:\\d{2}:\\d{2}\\.\\d{3,6})]Ping Dev\\.([a-zA-Z0-9\\-]+): (.+)$')
@Override
String apply(String text) {
try {
CharSequence textForMatching = limitAndCutNewline(text, 800, 1000)
Matcher matcher = pattern.matcher(textForMatching)
if (matcher.find()) {
// Take only the first letter of the level
String level = matcher.group(1).substring(0, 1)
// Remove the last 4 milliseconds
String time = matcher.group(2).substring(0, matcher.group(2).length() - 4)
String module = matcher.group(3)
String message = matcher.group(4)
return "[" + level + "][" + time + "] " + module + ": " + message + "\n"
}
// Didn't match - just return the original text
return text
} catch (ProcessCanceledException ex) {
show("Processing took too long for: " + text)
}
return text
}
})
static Class<?> getExtensionManager() {
IdeaPluginDescriptorImpl descriptor = PluginManager.getPlugin(PluginId.getId("GrepConsole")) as IdeaPluginDescriptorImpl
return descriptor.getPluginClassLoader().loadClass("krasa.grepconsole.plugin.ExtensionManager")
}
static void registerFunction(String functionName, Function<String, String> function) {
Class<?> clazz = getExtensionManager()
clazz.getMethod("registerFunction", String.class, Function.class)
.invoke(null, functionName, function)
show("'" + functionName + "' registered")
}
static Object unregisterFunction(Class<?> clazz, String functionName) {
clazz.getMethod("unregisterFunction", String.class).invoke(null, functionName)
show("'" + functionName + "' unregistered")
}
static CharSequence limitAndCutNewline(String text, int maxLength, int milliseconds) {
int endIndex = text.length()
if (text.endsWith("\n")) {
--endIndex
}
if (maxLength >= 0) {
endIndex = Math.min(endIndex, maxLength)
}
def substring = text.substring(0, endIndex)
if (milliseconds > 0) {
return newBombedCharSequence(substring, milliseconds)
}
return substring
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment