Skip to content

Commit

Permalink
Merge ba93fba into 0ab0f19
Browse files Browse the repository at this point in the history
  • Loading branch information
dynobo committed Feb 15, 2024
2 parents 0ab0f19 + ba93fba commit c1c692f
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 71 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## v0.4.4 (2024-02-14)

- Fix duplicate IDs in included sections.
- Fix error if launched with `--no-section-sort`
- Focus search field on start.
- Move generic CLI commands into separate cheatsheet and include it in terminal apps.

## v0.4.3 (2024-02-13)

- Fix background color in GTK 4.6.
Expand Down
43 changes: 43 additions & 0 deletions keyhint/config/cli.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
id = "cli"
source = ""

[match]
regex_process = "a^"
regex_title = "a^"

[section]

[section."Shell History"]
"Ctrl + r" = "Reverse cmd search / find next"
"`history | grep <term>`" = "Search for \"<term>\""
"`history 10`" = "Print last 10 cmds"
"!3" = "Execute cmd 3 from history"

[section."Data Wrangling"]
"`awk '{print $1}'`" = "Column base manipulations"
"`sed -E 's/.*(d+)/\\1/'`" = "Line base manipulations"
"`paste -sd,`" = "Concatenate lines"
"`tail -n10`" = "Last lines"
"`head -n10`" = "First lines"
"`uniq -c`" = "Count unique lines"
"`sort -nk1,1" = "Sort by column 1"

[section.Tools]
bc = "Calculator"
"gtop / htop" = "System Monitors"
xplr = "File manager"
moc = "Music Player in Console"
nvtop = "NVidia Card usage"
fd = "Easy to use find clone"
hyperfine = "Benchmarking"
tl = "TooLong log file viewer"
bat = "cat clone to view files with style"
eza = "ls clone with coloring"
duf = "Basic disk usage"
ncdu = "Recursive disk usage analyzer"
s-tui = "Stress test and monitor"

[section.Navigating]
pwd = "Print working dir"
cd = "Change to home dir"
"`cd -`" = "Change to previous dir"
22 changes: 1 addition & 21 deletions keyhint/config/foot.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
id = "foot-terminal"
source = "https://codeberg.org/dnkl/foot#user-content-shortcuts"
include = ["cli"]

[match]
regex_process = "foot"
Expand All @@ -13,24 +14,3 @@ regex_title = ".*"
"Ctrl + r / s" = "Search for previous / next match"
"Ctrl + Shift + u" = "URL mode"
t = "Toggle URL in label"

[section.Zsh]
"Ctrl + r" = "Reverse cmd search / find next"

[section."Data Wrangling"]
"`awk '{print $1}'`" = "Column base manipulations"
"`sed -E 's/.*(d+)/\\1/'`" = "Line base manipulations"
"`paste -sd,`" = "Concatenate lines"
"`tail -n10`" = "Last lines"
"`head -n10`" = "First lines"
"`uniq -c`" = "Count unique lines"
"`sort -nk1,1" = "Sort by column 1"

[section.Tools]
bc = "Calculator"
"gtop / htop" = "System Monitors"
xplr = "File manager"
moc = "Music Player in Console"
nvtop = "NVidia Card usage"
fd = "Find alternative"
hyperfine = "Benchmarking"
24 changes: 6 additions & 18 deletions keyhint/config/gnome-terminal.toml
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
id = "gnome-terminal"
source = "https://help.gnome.org/users/gnome-terminal/stable/adv-keyboard-shortcuts.html.en"
include = ["cli"]

[match]
regex_process = "gnome-terminal"
regex_title = ".*"

[section]
[section.Navigating]
pwd = "Print working dir"
cd = "Change to home dir"
"`cd -`" = "Change to previous dir"

[section.History]
"`history | grep <term>`" = "Search for \"<term>\""
"`history 10`" = "Print last 10 cmds"
"!3" = "Execute cmd 3 from history"
"Ctrl + r" = "Reverse cmd search / find next"

[section.Tools]
"`awk '{print $1}'`" = "Column base manipulations"
"`sed -E 's/.*(d+)/\\1/'`" = "Line base manipulations"
"`paste -sd,`" = "Concatenate lines"
bc = "Calculator"
"gtop / htop" = "System Monitors"
ranger = "File manager"
moc = "Music Player in Console"
[section.Tabs]
"Shift + Ctrl + T" = "New Tab"
"Shift + Ctrl + N" = "New Window"
"Shift + Ctrl + W" = "Close Tab"
"Shift + Ctrl + Q" = "Close Window"
1 change: 1 addition & 0 deletions keyhint/config/kitty.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
id = "kitty"
source = "https://sw.kovidgoyal.net/kitty/overview/"
include = ["cli"]

[match]
regex_process = "kitty"
Expand Down
12 changes: 1 addition & 11 deletions keyhint/config/tilix.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
id = "tilix"
source = "https://github.com/gnunn1/tilix/blob/master/data/resources/ui/shortcuts.ui"
include = ["cli"]

[match]
regex_process = "tilix"
Expand Down Expand Up @@ -27,14 +28,3 @@ F12 = "View session sidebar"
"Shift + Ctrl + G / H" = "Find next / previous"
"Shift + Ctrl + Up Down" = "Scroll up / down"
"Shift + PageUp PageDown" = "Page up / down"

[section."Navigate cmds"]
pwd = "Print working dir"
cd = "Change to home dir"
"`cd -`" = "Change to previous dir"

[section."History cmds"]
"`history | grep <term>`" = "Search for \"<term>\""
"`history 10`" = "Print last 10 cmds"
"!3" = "Execute cmd 3 from history"
"Ctrl + r" = "Reverse cmd search / find next"
1 change: 1 addition & 0 deletions keyhint/config/tmux.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
id = "tmux"
source = "https://github.com/gnunn1/tilix/blob/master/data/resources/ui/shortcuts.ui"
include = ["cli"]

[match]
regex_process = "foot"
Expand Down
27 changes: 12 additions & 15 deletions keyhint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import subprocess
import tomllib
import traceback
from copy import deepcopy
from pathlib import Path

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -63,21 +64,17 @@ def load_user_sheets() -> list[dict]:
def _expand_includes(sheets: list[dict]) -> list[dict]:
new_sheets = []
for s in sheets:
if includes := s.get("include", []):
for include in includes:
included_sheets = [c for c in sheets if c["id"] == include]
if not included_sheets:
message = (
f"Cheatsheet ID '{included_sheets}' "
f"included by '{s['id']}' not found!"
)
raise ValueError(message)
included_sheet = included_sheets[0]
included_sheet["section"] = {
f"{included_sheet['id']} - {k}": v
for k, v in included_sheet["section"].items()
}
s["section"].update(included_sheets[0]["section"])
for include in s.get("include", []):
included_sheets = [c for c in sheets if c["id"] == include]
if not included_sheets:
message = f"Cheatsheet '{include}' included by '{s['id']}' not found!"
raise ValueError(message)
included_sheet = deepcopy(included_sheets[0])
included_sheet["section"] = {
f"[{included_sheet['id']}] {k}": v
for k, v in included_sheet["section"].items()
}
s["section"].update(included_sheet["section"])
new_sheets.append(s)
return new_sheets

Expand Down
31 changes: 25 additions & 6 deletions keyhint/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
Does the rendering of the cheatsheets as well interface actions.
Sheet
Section
Binding
Shortcut + Label
Naming Hierarchy:
1. Keyhint works with multiple cheatsheets, in short "Sheets".
2. A single "Sheet" corresponds to one application and is rendered as one page in UI.
The sheet to render can be selected in the dropdown field. Each sheet has a
"Sheet ID" which must be unique.
3. A "Sheet" consists of multiple "Sections", which group together shortcuts or commands
into a blocks. Each section has a section title.
4. A "Section" consists of multiple "Bindings"
5. A "Binding" consists of a "Shortcut", which contains the key combination or command,
and a "Label" which describes the combination/command.
"""

import logging
Expand Down Expand Up @@ -85,6 +91,7 @@ def __init__(self, cli_args: dict) -> None:

# Make sure the window is focused
self.present()
self.focus_search_entry()

def init_actions(self) -> None:
"""Add actions used by main menu."""
Expand Down Expand Up @@ -161,6 +168,16 @@ def on_fullscreen_state_event(self, _: Gtk.Widget, __: GObject.Parameter) -> Non
self.header_bar_fullscreen.set_visible(True)
else:
self.header_bar_fullscreen.set_visible(False)
self.focus_search_entry()

def focus_search_entry(self) -> None:
"""Focus search entry depending on state."""
if self.is_fullscreen():
self.search_entry_fullscreen.grab_focus()
self.search_entry_fullscreen.set_position(-1)
else:
self.search_entry.grab_focus()
self.search_entry.set_position(-1)

def on_key_pressed(
self,
Expand Down Expand Up @@ -430,9 +447,11 @@ def populate_sheet_container(self) -> None:
sections = sheet["section"]
if not self.cli_args.get("no-section-sort", False):
# Sort sections by number of bindings to make the layout more dense
sections = sorted(sections.items(), key=lambda x: len(x[1]), reverse=True)
sections = dict(
sorted(sections.items(), key=lambda x: len(x[1]), reverse=True)
)

for section, bindings in sections:
for section, bindings in sections.items():
ls = Gio.ListStore()
for shortcut, label in bindings.items():
ls.append(BindingsRow(shortcut=shortcut, label=label, section=section))
Expand Down

0 comments on commit c1c692f

Please sign in to comment.