-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Make Output struct to help with indentation
- Loading branch information
Showing
8 changed files
with
180 additions
and
114 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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
mod bash; | ||
mod json; | ||
pub(self) mod util; | ||
mod util; | ||
mod zsh; | ||
|
||
use std::path::Path; | ||
|
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 |
---|---|---|
@@ -1,5 +1,60 @@ | ||
/// Wrap in single quotes (and escape single quotes inside) so that it's safe | ||
/// for Bash and Zsh to read | ||
pub fn quote(s: &str) -> String { | ||
format!("'{}'", s.replace('\'', r#"'"'"'"#)) | ||
pub fn quote_bash<S: AsRef<str>>(s: S) -> String { | ||
format!("'{}'", s.as_ref().replace('\'', r#"'"'"'"#)) | ||
} | ||
|
||
/// Helper to write indented text to a string | ||
pub struct Output { | ||
text: String, | ||
indent_str: String, | ||
indent_level: usize, | ||
/// If true, need to indent when the next string is written | ||
line_ended: bool, | ||
} | ||
|
||
impl Output { | ||
/// Make a new [Output]. `indent_str` is the string to indent with (e.g. | ||
/// `"\t"`). | ||
pub fn new(indent_str: String) -> Output { | ||
Output { | ||
text: String::from(""), | ||
indent_str, | ||
indent_level: 0, | ||
line_ended: false, | ||
} | ||
} | ||
|
||
/// Increase the indentation level by 1 | ||
pub fn indent(&mut self) { | ||
self.indent_level += 1; | ||
} | ||
|
||
/// Decrease the indentation level by 1 | ||
pub fn dedent(&mut self) { | ||
self.indent_level -= 1; | ||
} | ||
|
||
/// Write some text (without a newline) | ||
pub fn write<S: AsRef<str>>(&mut self, s: S) { | ||
if self.line_ended { | ||
for _ in 0..self.indent_level { | ||
self.text.push_str(&self.indent_str); | ||
} | ||
self.line_ended = false; | ||
} | ||
self.text.push_str(s.as_ref()); | ||
} | ||
|
||
/// Write some text (with a newline) | ||
pub fn writeln<S: AsRef<str>>(&mut self, s: S) { | ||
self.write(s); | ||
self.text.push('\n'); | ||
self.line_ended = true; | ||
} | ||
|
||
/// Consume this [Output], returning the text written to it | ||
pub fn text(self) -> String { | ||
self.text | ||
} | ||
} |
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.