-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial progress bar implementation
resolves #193
- Loading branch information
1 parent
2025b6c
commit 7388b5d
Showing
9 changed files
with
286 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
vulnz/src/main/java/io/github/jeremylong/vulnz/cli/ui/IProgressMonitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) 2024 Jeremy Long. All Rights Reserved. | ||
*/ | ||
package io.github.jeremylong.vulnz.cli.ui; | ||
|
||
public interface IProgressMonitor extends AutoCloseable { | ||
|
||
public void addMonitor(String name); | ||
|
||
public void updateProgress(String name, int current, int max); | ||
} |
48 changes: 48 additions & 0 deletions
48
vulnz/src/main/java/io/github/jeremylong/vulnz/cli/ui/JLineAppender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) 2024 Jeremy Long. All Rights Reserved. | ||
*/ | ||
package io.github.jeremylong.vulnz.cli.ui; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import ch.qos.logback.classic.layout.TTLLLayout; | ||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.core.AppenderBase; | ||
import ch.qos.logback.core.Layout; | ||
import org.jline.terminal.Terminal; | ||
|
||
public class JLineAppender extends AppenderBase<ILoggingEvent> { | ||
|
||
private Layout<ILoggingEvent> layout; | ||
|
||
public void setLayout(Layout<ILoggingEvent> layout) { | ||
this.layout = layout; | ||
} | ||
|
||
@Override | ||
protected void append(ILoggingEvent event) { | ||
Terminal terminal = ProgressMonitor.getTerminal(); | ||
if (terminal != null) { | ||
terminal.writer().println(layout.doLayout(event)); | ||
terminal.flush(); | ||
} else { | ||
if (event.getLevel() == Level.TRACE || event.getLevel() == Level.DEBUG) { | ||
System.err.println(layout.doLayout(event)); | ||
} else { | ||
System.out.println(layout.doLayout(event)); | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
vulnz/src/main/java/io/github/jeremylong/vulnz/cli/ui/JlineShutdownHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) 2024 Jeremy Long. All Rights Reserved. | ||
*/ | ||
package io.github.jeremylong.vulnz.cli.ui; | ||
|
||
import org.jline.terminal.Terminal; | ||
|
||
import java.io.IOException; | ||
|
||
public class JlineShutdownHook extends Thread { | ||
|
||
public void run() { | ||
try { | ||
ProgressMonitor.closeTerminal(); | ||
} catch (IOException e) { | ||
// ignore | ||
} | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
vulnz/src/main/java/io/github/jeremylong/vulnz/cli/ui/ProgressMonitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) 2023-2024 Jeremy Long. All Rights Reserved. | ||
*/ | ||
package io.github.jeremylong.vulnz.cli.ui; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import org.jline.terminal.Terminal; | ||
import org.jline.terminal.TerminalBuilder; | ||
import org.jline.utils.AttributedString; | ||
import org.jline.utils.Status; | ||
|
||
public class ProgressMonitor implements IProgressMonitor { | ||
|
||
boolean enabled; | ||
Map<String, Integer> rows = new HashMap<>(); | ||
|
||
private static Terminal terminal = null; | ||
private Status status; | ||
|
||
static Terminal getTerminal() { | ||
return terminal; | ||
} | ||
|
||
@SuppressFBWarnings("CT_CONSTRUCTOR_THROW") | ||
public ProgressMonitor(boolean enabled) throws IOException { | ||
|
||
} | ||
|
||
@SuppressFBWarnings({"CT_CONSTRUCTOR_THROW", "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD"}) | ||
public ProgressMonitor(boolean enabled, String name) throws IOException { | ||
this.enabled = enabled; | ||
if (enabled) { | ||
addMonitor(name); | ||
terminal = TerminalBuilder.terminal(); | ||
status = new Status(terminal); | ||
} | ||
} | ||
|
||
@Override | ||
public void addMonitor(String name) { | ||
this.rows.put(name, 0); | ||
} | ||
|
||
private List<AttributedString> determineStatusBar() { | ||
int maxNameWidth = rows.keySet().stream().mapToInt(String::length).max().orElse(0); | ||
return rows.entrySet().stream().map(entry -> { | ||
String name = entry.getKey(); | ||
int percent = entry.getValue(); | ||
int remaining = terminal.getWidth(); | ||
remaining = Math.min(remaining, 100); | ||
StringBuilder string = new StringBuilder(remaining); | ||
remaining -= maxNameWidth; | ||
string.append(name); | ||
int spaces = maxNameWidth - name.length(); | ||
if (spaces > 0) { | ||
string.append(String.join("", Collections.nCopies(spaces, " "))); | ||
} | ||
if (percent >= 100) { | ||
string.append(" complete"); | ||
} else { | ||
String spacer = percent < 10 ? " " : ""; | ||
string.append(spacer).append(String.format(" %d%% [", percent)); | ||
remaining -= 10; | ||
int completed = remaining * percent / 100; | ||
int filler = remaining - completed; | ||
System.out.println("completed: " + completed + " filler: " + filler + " remaining: " + remaining); | ||
string.append(String.join("", Collections.nCopies(completed, "="))).append('>') | ||
.append(String.join("", Collections.nCopies(filler, " "))).append(']'); | ||
} | ||
String s = string.toString(); | ||
return new AttributedString(s); | ||
}).sorted().collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public void updateProgress(String name, int current, int max) { | ||
int percent = (int) (current * 100 / max); | ||
rows.put(name, percent); | ||
if (enabled) { | ||
|
||
status.update(new ArrayList<AttributedString>()); | ||
status.resize(); | ||
List<AttributedString> displayedRows = determineStatusBar(); | ||
status.update(displayedRows, true); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
if (enabled) { | ||
if (status != null) { | ||
status.close(); | ||
} | ||
closeTerminal(); | ||
enabled = false; | ||
} | ||
} | ||
|
||
static void closeTerminal() throws IOException { | ||
if (terminal != null) { | ||
terminal.close(); | ||
terminal = null; | ||
} | ||
} | ||
} |
77 changes: 0 additions & 77 deletions
77
vulnz/src/main/java/io/github/jeremylong/vulnz/cli/ui/Screen.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.