This commit is contained in:
davidon-top 2023-12-09 13:54:37 +00:00
parent cea9ee6e51
commit d8b009be6b
Signed by: DavidOnTop
GPG key ID: FAB914DDC2F180EB
2 changed files with 106 additions and 94 deletions

View file

@ -1,43 +0,0 @@
name: CI
on:
push:
branches:
- 'main'
- 'feat/*'
pull_request:
branches:
- 'main'
- 'feat/*'
env:
CARGO_TERM_COLOR: always
jobs:
clippy_check:
name: Clippy
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Install toolchain
uses: dtolnay/rust-toolchain@nightly
with:
components: clippy
- name: Run clippy
run: cargo clippy -- -D warnings
format:
name: Format
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Install toolchain
uses: dtolnay/rust-toolchain@nightly
with:
components: rustfmt
- name: Run cargo fmt
run: cargo fmt --all -- --check

View file

@ -32,7 +32,7 @@ enum Item {
#[serde(default)]
value: usize,
options: Vec<String>,
}
},
}
impl Item {
@ -66,7 +66,8 @@ impl<'rofi> Mode<'rofi> {
self.api.set_display_name("edit");
self.entries.clear();
self.items.iter().for_each(|item| {
self.entries.push(format!("{}: {}", item.name, item.item.get_value()));
self.entries
.push(format!("{}: {}", item.name, item.item.get_value()));
});
self.entries.push("Apply".to_string());
}
@ -83,36 +84,50 @@ impl<'rofi> Mode<'rofi> {
self.entries.push("false".to_string());
},
Item::Int { value, min, max } => {
let min = min.map(|min| format!("Min: {};", min)).unwrap_or("".to_string());
let max = max.map(|max| format!("Max: {};", max)).unwrap_or("".to_string());
let min = min
.map(|min| format!("Min: {};", min))
.unwrap_or("".to_string());
let max = max
.map(|max| format!("Max: {};", max))
.unwrap_or("".to_string());
self.message = format!("Old value: {};\r{} {}", value, min, max);
},
Item::Float { value, min, max } => {
let min = min.map(|min| format!("Min: {};", min)).unwrap_or("".to_string());
let max = max.map(|max| format!("Max: {};", max)).unwrap_or("".to_string());
let min = min
.map(|min| format!("Min: {};", min))
.unwrap_or("".to_string());
let max = max
.map(|max| format!("Max: {};", max))
.unwrap_or("".to_string());
self.message = format!("Old value: {};\r{} {}", value, min, max);
},
Item::String { .. } => {},
Item::Enum { options, .. } => {
options.iter().for_each(|option| self.entries.push(option.clone()));
options
.iter()
.for_each(|option| self.entries.push(option.clone()));
},
}
self.entries.push("Cancel".to_string());
return item.item.get_value();
}
fn finish_edit(&mut self, selected: Option<usize>, custom_input: Option<&mut rofi_mode::String>) {
fn finish_edit(
&mut self,
selected: Option<usize>,
custom_input: Option<&mut rofi_mode::String>,
) {
match self.state {
State::Main => {panic!("finish_edit called in main state")},
State::Main => {
panic!("finish_edit called in main state")
},
State::Editing(item) => {
let item = &mut self.items[item];
match item.item {
Item::Bool { .. } => {
match selected {
Some(0) => item.item = Item::Bool { value: true },
Some(1) => item.item = Item::Bool { value: false },
_ => {},
}
Item::Bool { .. } => match selected {
Some(0) => item.item = Item::Bool { value: true },
Some(1) => item.item = Item::Bool { value: false },
_ => {},
},
Item::Int { value, min, max } => {
let mut new_value = value.clone();
@ -129,7 +144,11 @@ impl<'rofi> Mode<'rofi> {
new_value = max;
}
}
item.item = Item::Int { value: new_value, min, max };
item.item = Item::Int {
value: new_value,
min,
max,
};
},
Item::Float { value, min, max } => {
let mut new_value = value.clone();
@ -146,11 +165,17 @@ impl<'rofi> Mode<'rofi> {
new_value = max;
}
}
item.item = Item::Float { value: new_value, min, max };
item.item = Item::Float {
value: new_value,
min,
max,
};
},
Item::String { .. } => {
if let Some(input) = custom_input {
item.item = Item::String { value: input.into() };
item.item = Item::String {
value: input.into(),
};
}
},
Item::Enum { ref mut value, .. } => {
@ -191,34 +216,69 @@ impl<'rofi> rofi_mode::Mode<'rofi> for Mode<'rofi> {
api.set_display_name("edit");
let args = std::env::args().into_iter().collect::<Vec<_>>();
if args.contains(&"--edit-help".to_string()) {
eprintln!(r#"
eprintln!(
r#"
--edit-stdin Read input from stdin, reads json until 2 newlines
--edit-file <file> Read input from file, json file
--edit-input <input> Read input, should be a string containing json
--edit-example Prints examples
"#);
"#
);
return Err(());
}
if args.contains(&"--edit-example".to_string()) {
let mut its: Vec<ItemContainer> = Vec::new();
its.push(ItemContainer { name: "bool item".to_string(), item: Item::Bool { value: false } });
its.push(ItemContainer { name: "int item".to_string(), item: Item::Int { value: 0, min: Some(0), max: None } });
its.push(ItemContainer { name: "float item".to_string(), item: Item::Float { value: 0.0, min: None, max: Some(69.0) } });
its.push(ItemContainer { name: "string item".to_string(), item: Item::String { value: "hello".to_string() } });
its.push(ItemContainer { name: "enum items".to_string(), item: Item::Enum { value: 0, options: vec!["opt1".into(), "opt2".into(), "other_opt".into()] } });
its.push(ItemContainer {
name: "bool item".to_string(),
item: Item::Bool { value: false },
});
its.push(ItemContainer {
name: "int item".to_string(),
item: Item::Int {
value: 0,
min: Some(0),
max: None,
},
});
its.push(ItemContainer {
name: "float item".to_string(),
item: Item::Float {
value: 0.0,
min: None,
max: Some(69.0),
},
});
its.push(ItemContainer {
name: "string item".to_string(),
item: Item::String {
value: "hello".to_string(),
},
});
its.push(ItemContainer {
name: "enum items".to_string(),
item: Item::Enum {
value: 0,
options: vec!["opt1".into(), "opt2".into(), "other_opt".into()],
},
});
eprintln!("Example input:\n{}\n\nOutput is the same with changed value keys\nKeys with value of null can be omited", serde_json::to_string_pretty(&its).unwrap());
return Err(());
}
let items: Vec<ItemContainer> = if args.contains(&"--edit-stdin".to_string()) {
let res = read_stdin();
serde_json::from_str(&res).unwrap()
} else if let Some(file_pos) = args.iter().position(|arg| *arg == "--edit-file".to_string()) {
} else if let Some(file_pos) = args
.iter()
.position(|arg| *arg == "--edit-file".to_string())
{
let fp = args[file_pos + 1].clone();
let res = std::fs::read_to_string(fp).unwrap();
serde_json::from_str(&res).unwrap()
} else if let Some(input_pos) = args.iter().position(|arg| *arg == "--edit-input".to_string()) {
} else if let Some(input_pos) = args
.iter()
.position(|arg| *arg == "--edit-input".to_string())
{
let res = args[input_pos + 1].clone();
serde_json::from_str(&res).unwrap()
} else {
@ -236,18 +296,18 @@ impl<'rofi> rofi_mode::Mode<'rofi> for Mode<'rofi> {
Ok(s)
}
fn entries(&mut self) -> usize {
self.entries.len()
}
fn entries(&mut self) -> usize { self.entries.len() }
fn entry_content(&self, line: usize) -> rofi_mode::String {
self.entries[line].clone().into()
}
fn entry_content(&self, line: usize) -> rofi_mode::String { self.entries[line].clone().into() }
fn react(&mut self, event: rofi_mode::Event, input: &mut rofi_mode::String) -> rofi_mode::Action {
fn react(
&mut self,
event: rofi_mode::Event,
input: &mut rofi_mode::String,
) -> rofi_mode::Action {
match event {
rofi_mode::Event::Cancel { .. } => {
if let State::Editing(item) = self.state {
if let State::Editing(_item) = self.state {
*input = "".into();
self.entries_from_items();
} else {
@ -256,7 +316,7 @@ impl<'rofi> rofi_mode::Mode<'rofi> for Mode<'rofi> {
}
},
rofi_mode::Event::Ok { selected, .. } => {
if let State::Editing(item) = self.state {
if let State::Editing(_item) = self.state {
self.finish_edit(Some(selected), None);
} else {
if selected == self.entries() - 1 {
@ -267,24 +327,21 @@ impl<'rofi> rofi_mode::Mode<'rofi> for Mode<'rofi> {
}
}
},
rofi_mode::Event::CustomInput { selected, .. } => {
if let State::Editing(item) = self.state {
rofi_mode::Event::CustomInput { .. } => {
if let State::Editing(_item) = self.state {
self.finish_edit(None, Some(input));
*input = "".into();
} else {
*input = rofi_mode::format!("");
}
},
rofi_mode::Event::Complete { selected } => {
match selected {
None => {},
Some(item) => {
*input = rofi_mode::format!("{}", self.entries[item]);
}
}
rofi_mode::Event::Complete { selected } => match selected {
None => {},
Some(item) => {
*input = rofi_mode::format!("{}", self.entries[item]);
},
},
rofi_mode::Event::DeleteEntry { selected } => {},
rofi_mode::Event::CustomCommand { number, selected } => {},
_ => {},
}
rofi_mode::Action::Reload
}
@ -293,9 +350,7 @@ impl<'rofi> rofi_mode::Mode<'rofi> for Mode<'rofi> {
matcher.matches(&self.entries[line])
}
fn message(&mut self) -> rofi_mode::String {
self.message.clone().into()
}
fn message(&mut self) -> rofi_mode::String { self.message.clone().into() }
}
rofi_mode::export_mode!(Mode<'_>);