Skip to content

Commit

Permalink
#66 feature: rebuild completer after pointing to a tables folder
Browse files Browse the repository at this point in the history
  • Loading branch information
nergal-perm committed Feb 2, 2024
1 parent 6b0a9ce commit d98cacd
Showing 1 changed file with 48 additions and 7 deletions.
55 changes: 48 additions & 7 deletions src/main/java/ru/ewc/decita/manual/Shell.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.reader.ParsedLine;
import org.jline.reader.impl.DefaultParser;
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import ru.ewc.decita.input.PlainTextContentReader;

/**
* I am the shell for manual library testing. My main responsibility is to hide Java complexities
Expand All @@ -41,10 +45,20 @@
* @since 0.2.2
*/
public final class Shell {
/**
* Set of predefined autocompletion options, contains all available commands.
*/
public static final List<String> COMMANDS = List.of("sources", "decide");

/**
* Object holding current terminal.
*/
private final Terminal terminal;

/**
* Object that reads the user's input.
*/
private final LineReader reader;
private LineReader reader;

/**
* Object that can write anything to console.
Expand All @@ -63,10 +77,11 @@ public final class Shell {
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
private Shell() throws IOException {
final Terminal terminal = TerminalBuilder.terminal();
this.writer = terminal.writer();
this.reader = LineReaderBuilder.builder()
.terminal(terminal)
this.terminal = TerminalBuilder.terminal();
this.writer = this.terminal.writer();
this.reader = LineReaderBuilder
.builder()
.terminal(this.terminal)
.completer(new StringsCompleter("sources", "hello"))
.parser(new DefaultParser())
.build();
Expand All @@ -77,8 +92,6 @@ private Shell() throws IOException {
)
);
}
// @todo #61 Rebuild Completer each time the folder with tables is loaded.
// That will allow autocompletion for table names, so it will be easier to issue commands.

/**
* The method to run a simplified manual testing utility.
Expand Down Expand Up @@ -145,9 +158,37 @@ private void decideFor(final String table) {
*/
private void pointToSources(final String path) {
this.folder = URI.create(String.format("file://%s", path.replace("'", "")));
final Set<String> tables = this.extractFilenamesFromPath();
this.reader = LineReaderBuilder
.builder()
.terminal(this.terminal)
.completer(new StringsCompleter(completionOptionsFor(tables)))
.build();
this.writer.printf("Let's import tables from %s%n", path);
}

/**
* Returns a complete set of all the table names, read from the user-specified folder.
*
* @return Set of Strings, representing decision table names.
*/
private Set<String> extractFilenamesFromPath() {
return new PlainTextContentReader(this.folder, ".csv", ";").allTables().keySet();
}

/**
* Computes a list of all autocompletion options base on a predefined set of available commands
* and a list of tables in a user-specified folder.
*
* @param tables List of table names.
* @return A collection of string elements, each of which is the autocomplete option.
*/
private static List<String> completionOptionsFor(final Set<String> tables) {
final List<String> result = new ArrayList<>(tables);
result.addAll(Shell.COMMANDS);
return result;
}

/**
* Extracts a single parameter for the user command.
*
Expand Down

2 comments on commit d98cacd

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on d98cacd Feb 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 61-9cd75bbc disappeared from src/main/java/ru/ewc/decita/manual/Shell.java), that's why I closed #66. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on d98cacd Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to retrieve PDD puzzles from the code base and submit them to github. If you think that it's a bug on our side, please submit it to yegor256/0pdd:

Input/output error @ io_fread - /tmp/0pdd-xml-cache/nergal-perm/java-decita

Please, copy and paste this stack trace to GitHub:

Errno::EIO
Input/output error @ io_fread - /tmp/0pdd-xml-cache/nergal-perm/java-decita
/app/objects/storage/cached_storage.rb:32:in `read'
/app/objects/storage/cached_storage.rb:32:in `load'
/app/objects/storage/once_storage.rb:30:in `load'
/app/objects/storage/safe_storage.rb:33:in `load'
/app/objects/storage/upgraded_storage.rb:31:in `load'
/app/objects/storage/sync_storage.rb:31:in `block in load'
/app/objects/storage/sync_storage.rb:31:in `synchronize'
/app/objects/storage/sync_storage.rb:31:in `load'
/app/objects/jobs/job.rb:37:in `proceed'
/app/objects/jobs/job_starred.rb:32:in `proceed'
/app/objects/jobs/job_recorded.rb:31:in `proceed'
/app/objects/jobs/job_emailed.rb:33:in `proceed'
/app/objects/jobs/job_commiterrors.rb:33:in `proceed'
/app/objects/jobs/job_detached.rb:48:in `exclusive'
/app/objects/jobs/job_detached.rb:36:in `block in proceed'
/app/objects/jobs/job_detached.rb:36:in `fork'
/app/objects/jobs/job_detached.rb:36:in `proceed'
/app/0pdd.rb:531:in `process_request'
/app/0pdd.rb:367:in `block in <top (required)>'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1706:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1706:in `block in compile!'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1019:in `block (3 levels) in route!'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1037:in `route_eval'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1019:in `block (2 levels) in route!'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1068:in `block in process_route'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1066:in `catch'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1066:in `process_route'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1017:in `block in route!'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1014:in `each'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1014:in `route!'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1138:in `block in dispatch!'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1109:in `catch'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1109:in `invoke'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1133:in `dispatch!'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:949:in `block in call!'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1109:in `catch'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1109:in `invoke'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:949:in `call!'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:938:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.8/lib/rack/deflater.rb:44:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/rack-protection-3.0.6/lib/rack/protection/xss_header.rb:20:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/rack-protection-3.0.6/lib/rack/protection/path_traversal.rb:18:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/rack-protection-3.0.6/lib/rack/protection/json_csrf.rb:28:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/rack-protection-3.0.6/lib/rack/protection/base.rb:53:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/rack-protection-3.0.6/lib/rack/protection/base.rb:53:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/rack-protection-3.0.6/lib/rack/protection/frame_options.rb:33:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.8/lib/rack/logger.rb:17:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.8/lib/rack/common_logger.rb:38:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:261:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:254:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.8/lib/rack/head.rb:12:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.8/lib/rack/method_override.rb:24:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:219:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:2018:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1576:in `block in call'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1792:in `synchronize'
/app/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.6/lib/sinatra/base.rb:1576:in `call'
/app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.8/lib/rack/handler/webrick.rb:95:in `service'
/app/vendor/bundle/ruby/3.1.0/gems/webrick-1.8.1/lib/webrick/httpserver.rb:140:in `service'
/app/vendor/bundle/ruby/3.1.0/gems/webrick-1.8.1/lib/webrick/httpserver.rb:96:in `run'
/app/vendor/bundle/ruby/3.1.0/gems/webrick-1.8.1/lib/webrick/server.rb:310:in `block in start_thread'

Please sign in to comment.