Skip to content

Commit

Permalink
Add feature to consume for stdin and specify arbitrary string prefix …
Browse files Browse the repository at this point in the history
…for more config file format support. Addresses #9 and #10.
  • Loading branch information
kgilmer committed Oct 17, 2020
1 parent be3916c commit 90dff5a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
21 changes: 15 additions & 6 deletions src/config_parser.vala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* This class retrieves the i3 config file over IPC and
* produces a [Category] -> List<Keybinding> data structure
* indended to be rendered to the user.
* indended to be presented to the user.
*/
using Gee;

Expand All @@ -25,9 +25,11 @@ public class ConfigParser {
private const int PARAMETER_COUNT = 3;
private const int MIN_LINE_LENGTH = 13; // "##x//y//z//##".length
private string config;
private string line_prefix;

public ConfigParser(string config) {
public ConfigParser(string config, string line_prefix) {
this.config = config;
this.line_prefix = line_prefix;
}

public Map<string, ArrayList<Keybinding>> parse() throws PARSE_ERROR, GLib.Error, Grelier.I3_ERROR {
Expand All @@ -36,10 +38,17 @@ public class ConfigParser {
if (lines == null || lines.length == 0) return Map.empty<string, ArrayList<Keybinding>>();;

var config_map = new TreeMap<string, ArrayList<Keybinding>>();
var prefix = REMONTOIRE_LINE_WRAPPER;
if (line_prefix != "") {
prefix = line_prefix + REMONTOIRE_LINE_WRAPPER;
}

foreach (unowned string line in lines) {
string trimmedLine = line.strip();
if (lineMatch(trimmedLine)) {
if (lineMatch(trimmedLine, prefix)) {
if (line_prefix != "") {
trimmedLine = trimmedLine.substring(line_prefix.length);
}
parseLine(trimmedLine, config_map);
}
}
Expand All @@ -49,10 +58,10 @@ public class ConfigParser {
return config_map;
}

private bool lineMatch(string line) {
private bool lineMatch(string line, string prefix) {

return line.length > MIN_LINE_LENGTH &&
line.has_prefix(REMONTOIRE_LINE_WRAPPER) &&
line.has_prefix(prefix) &&
line.substring(REMONTOIRE_LINE_WRAPPER.length + 1).contains(REMONTOIRE_LINE_WRAPPER) &&
line.contains(REMONTIORE_PARAM_DELIMITER);
}
Expand Down Expand Up @@ -138,4 +147,4 @@ public class ConfigParser {
}
}
*/
}
}
36 changes: 27 additions & 9 deletions src/main.vala
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,34 @@ int main (string[] args) {
-v, --version Display version number
-s <Socket URI> Socket path for i3
-c <Path to config file> Config file
-i <Read from standard input> Read from standard input
-t <Path to style file> CSS file
-p <comment line prefix> Prefix of comment line
""");
print ("\n");
return 0;
}

if ((argMap.has_key("-s") && argMap.has_key("-c")) ||
(!argMap.has_key("-s") && !argMap.has_key("-c"))) {
printerr ("Must specify either socket URI to i3 socket or file path to config file.\n");
printerr ("Run '%s --help' to see a full list of available command line options.\n", args[0]);
return 1;
}

read_config config_reader;
string config_descriptor;
if (argMap.has_key("-s")) {
config_reader = read_socket_config;
config_descriptor = argMap.get("-s");
} else {
} else if (argMap.has_key("-c")) {
config_reader = read_file_config;
config_descriptor = argMap.get("-c");
} else if (argMap.has_key("-i")) {
config_reader = read_stdin_config;
config_descriptor = "";
} else {
printerr ("Must specify either socket URI to i3 socket or file path to config file.\n");
printerr ("Run '%s --help' to see a full list of available command line options.\n", args[0]);
return 1;
}

string line_prefix = "";
if (argMap.has_key("-p")) {
line_prefix = argMap.get("-p");
}

var app = new Gtk.Application ("org.regolith.remontoire", ApplicationFlags.FLAGS_NONE);
Expand All @@ -61,7 +68,7 @@ int main (string[] args) {
var window = app.active_window;
if (window == null) {
try {
var configParser = new ConfigParser(config_reader(config_descriptor));
var configParser = new ConfigParser(config_reader(config_descriptor), line_prefix);
window = new Remontoire.SliderWindow (app, configParser.parse(), settings);

Gtk.CssProvider css_provider = new Gtk.CssProvider ();
Expand Down Expand Up @@ -159,3 +166,14 @@ string read_file_config(string file_path) throws GLib.Error {
return str_builder.str;
}

string read_stdin_config(string unused) throws GLib.Error {
var input = new StringBuilder ();
var buffer = new char[1024];
while (!stdin.eof ()) {
string read_chunk = stdin.gets (buffer);
if (read_chunk != null) {
input.append (read_chunk);
}
}
return input.str;
}

0 comments on commit 90dff5a

Please sign in to comment.