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