Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add option to output colors #2251

Merged
merged 8 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bindings/prql-elixir/native/prql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ impl From<CompileOptions> for prql_compiler::Options {
format: o.format,
target: target_from_atom(o.target),
signature_comment: o.signature_comment,
// TODO: add support for this
color: false,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions bindings/prql-java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub extern "system" fn Java_org_prql_prql4j_PrqlCompiler_toSql(
format: format != 0,
target: prql_dialect,
signature_comment: signature != 0,
// TODO: add support for this
color: false,
};
let result = prql_compiler::compile(&prql_query, &opt);
java_string_with_exception(result, &env)
Expand Down
2 changes: 2 additions & 0 deletions bindings/prql-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ impl From<CompileOptions> for prql_compiler::Options {
format: o.format,
target,
signature_comment: o.signature_comment,
// TODO: offer this option in the API
color: false,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions bindings/prql-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,5 +323,7 @@ fn convert_options(o: &Options) -> Result<prql_compiler::Options, prql_compiler:
format: o.format,
target,
signature_comment: o.signature_comment,
// TODO: add support for this
color: false,
})
}
2 changes: 2 additions & 0 deletions bindings/prql-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ fn convert_options(
format: o.format,
target,
signature_comment: o.signature_comment,
// TODO: offer support
color: false,
})
}

Expand Down
3 changes: 2 additions & 1 deletion prql-compiler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ let prql = "from employees | select [name, age]";
let opts = &Options {
format: false,
target: Target::Sql(Some(Dialect::SQLite)),
signature_comment: false
signature_comment: false,
color: false,
};
let sql = compile(&prql, opts).unwrap();
assert_eq!("SELECT name, age FROM employees", sql);
Expand Down
2 changes: 2 additions & 0 deletions prql-compiler/prqlc/src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub fn run(command: &mut WatchArgs) -> Result<()> {
format: !command.no_format,
target: prql_compiler::Target::Sql(None),
signature_comment: !command.no_signature,
// TODO: potentially offer this as an arg?
color: false,
};
let path = Path::new(&command.path);

Expand Down
14 changes: 12 additions & 2 deletions prql-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ pub static PRQL_VERSION: Lazy<Version> =
/// let opts = Options {
/// format: false,
/// target: Target::Sql(Some(Dialect::SQLite)),
/// signature_comment: false
/// signature_comment: false,
/// color: false,
/// };
/// let sql = compile(&prql, &opts).unwrap();
/// println!("PRQL: {}\nSQLite: {}", prql, &sql);
Expand All @@ -125,7 +126,7 @@ pub fn compile(prql: &str, options: &Options) -> Result<String, ErrorMessages> {
.and_then(semantic::resolve)
.and_then(|rq| sql::compile(rq, options))
.map_err(error::downcast)
.map_err(|e| e.composed("", prql, false))
.map_err(|e| e.composed("", prql, options.color))
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -188,6 +189,9 @@ pub struct Options {
///
/// Defaults to true.
pub signature_comment: bool,

/// Whether to use ANSI colors in error messages.
pub color: bool,
}

impl Default for Options {
Expand All @@ -196,6 +200,7 @@ impl Default for Options {
format: true,
target: Target::Sql(None),
signature_comment: true,
color: false,
}
}
}
Expand All @@ -219,6 +224,11 @@ impl Options {
pub fn some(self) -> Option<Self> {
Some(self)
}

pub fn with_color(mut self, color: bool) -> Self {
self.color = color;
self
}
}

#[doc = include_str!("../README.md")]
Expand Down