2 * Copyright 2000-2016 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.execution.runners
18 import com.intellij.execution.ExecutionHelper
19 import com.intellij.execution.process.ProcessHandler
20 import com.intellij.execution.ui.RunContentDescriptor
21 import com.intellij.openapi.project.Project
22 import java.util.stream.Collectors
27 open class ConsoleTitleGen @JvmOverloads constructor(private val myProject: Project, private val consoleTitle: String, private val shouldAddNumberToTitle: Boolean = true) {
29 fun makeTitle(): String {
31 if (shouldAddNumberToTitle) {
32 val activeConsoleNames = getActiveConsoles(consoleTitle)
34 for (name in activeConsoleNames) {
39 val num = Integer.parseInt(name.substring(consoleTitle.length + 1, name.length - 1))
44 catch (ignored: Exception) {
50 return consoleTitle + "(" + (max + 1) + ")"
58 open protected fun getActiveConsoles(consoleTitle: String): List<String> {
59 val consoles = ExecutionHelper.collectConsolesByDisplayName(myProject) { dom -> dom.contains(consoleTitle) }
61 return consoles.filter({ input ->
62 val handler = input.processHandler
63 handler != null && !handler.isProcessTerminated
64 }).map({ it.displayName });