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

Floating Menu #179

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ members = [
"plugins/shrs_derive_completion",
"plugins/shrs_run_context",
"plugins/shrs_mux",
"plugins/shrs_insulter"
"plugins/shrs_insulter",
"plugins/shrs_floating_menu"
]
11 changes: 11 additions & 0 deletions plugins/shrs_floating_menu/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "shrs_floating_menu"
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
authors = ["MrPicklePinosaur"]
description = "Floating menu"
repository = "https://github.com/MrPicklePinosaur/sh.rs"

[dependencies]
shrs = { path = "../../shrs" }
11 changes: 11 additions & 0 deletions plugins/shrs_floating_menu/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod menu;

use shrs::prelude::*;

pub struct FloatingMenuPlugin {}

impl Plugin for FloatingMenuPlugin {
fn init(&self, shell: &mut ShellConfig) {
// TODO plugins for line
}
}
63 changes: 63 additions & 0 deletions plugins/shrs_floating_menu/src/menu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use shrs::{crossterm, prelude::*};

pub struct FloatingMenu {}

impl FloatingMenu {
pub fn new() -> Self {
Self {}
}
}

impl Menu for FloatingMenu {
type MenuItem = String;
type PreviewItem = String;

fn next(&mut self) {
todo!()
}

fn previous(&mut self) {
todo!()
}

fn accept(&mut self) -> Option<&Self::MenuItem> {
todo!()
}

fn current_selection(&self) -> Option<&Self::MenuItem> {
todo!()
}

fn cursor(&self) -> u32 {
todo!()
}

fn is_active(&self) -> bool {
todo!()
}

fn activate(&mut self) {
todo!()
}

fn disactivate(&mut self) {
todo!()
}

fn items(&self) -> Vec<&(Self::PreviewItem, Self::MenuItem)> {
todo!()
}

fn set_items(&mut self, items: Vec<(Self::PreviewItem, Self::MenuItem)>) {
todo!()
}

fn render(&self, out: &mut Out) -> anyhow::Result<()> {
todo!()
}

fn required_lines(&self) -> usize {
// We don't need to scroll up at all
0
}
}
2 changes: 1 addition & 1 deletion shrs_line/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod history;
pub use history::{DefaultHistory, FileBackedHistory, History};

mod menu;
pub use menu::{DefaultMenu, Menu};
pub use menu::{DefaultMenu, Menu, Out};

mod prompt;
pub use prompt::{DefaultPrompt, Prompt, *};
Expand Down
48 changes: 21 additions & 27 deletions shrs_line/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ pub trait Menu {
type MenuItem;
type PreviewItem: Display;

/// Select the next item in menu
fn next(&mut self);
/// Select the previous item in menu
fn previous(&mut self);
/// Return the currently selected item
fn accept(&mut self) -> Option<&Self::MenuItem>;
/// Get the currently selected item
fn current_selection(&self) -> Option<&Self::MenuItem>;
/// Get the index of the currently selected item
fn cursor(&self) -> u32;
fn is_active(&self) -> bool;
fn activate(&mut self);
fn disactivate(&mut self);
fn items(&self) -> Vec<&(Self::PreviewItem, Self::MenuItem)>;
fn set_items(&mut self, items: Vec<(Self::PreviewItem, Self::MenuItem)>);

fn selected_style(&self, out: &mut Out) -> crossterm::Result<()>;
fn unselected_style(&self, out: &mut Out) -> crossterm::Result<()>;

fn render(&self, out: &mut Out) -> anyhow::Result<()>;
fn required_lines(&self) -> usize;
}
Expand All @@ -57,25 +59,31 @@ impl DefaultMenu {
column_padding: 2,
}
}

fn selected_style(&self, out: &mut Out) -> crossterm::Result<()> {
execute!(
out,
SetBackgroundColor(Color::White),
SetForegroundColor(Color::Black),
)?;
Ok(())
}

fn unselected_style(&self, out: &mut Out) -> crossterm::Result<()> {
execute!(out, ResetColor)?;
Ok(())
}
}

impl Menu for DefaultMenu {
type MenuItem = Completion;
type PreviewItem = String;

fn next(&mut self) {
if self.cursor as usize == self.selections.len().saturating_sub(1) {
self.cursor = 0;
} else {
self.cursor += 1;
}
self.cursor = (self.cursor + 1) % self.selections.len() as u32;
}
fn previous(&mut self) {
if self.cursor == 0 {
self.cursor = self.selections.len().saturating_sub(1) as u32;
} else {
self.cursor = self.cursor.saturating_sub(1);
}
self.cursor = (self.cursor - 1) % self.selections.len() as u32;
}
fn accept(&mut self) -> Option<&Self::MenuItem> {
self.disactivate();
Expand Down Expand Up @@ -107,20 +115,6 @@ impl Menu for DefaultMenu {
self.cursor = 0;
}

fn selected_style(&self, out: &mut Out) -> crossterm::Result<()> {
execute!(
out,
SetBackgroundColor(Color::White),
SetForegroundColor(Color::Black),
)?;
Ok(())
}

fn unselected_style(&self, out: &mut Out) -> crossterm::Result<()> {
execute!(out, ResetColor)?;
Ok(())
}

fn render(&self, out: &mut Out) -> anyhow::Result<()> {
let mut i = 0;
let mut column_start: usize = 0;
Expand Down