Skip to content

Commit

Permalink
Leader (#140)
Browse files Browse the repository at this point in the history
* support for leader keys

* update version

* increase timeout to 1 sec

* update example config
  • Loading branch information
RasmusLindroth authored May 10, 2022
1 parent 741b116 commit 80ede1e
Show file tree
Hide file tree
Showing 8 changed files with 433 additions and 100 deletions.
34 changes: 33 additions & 1 deletion config.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,39 @@ show-help=true
# default=true
redraw-ui=true

# The leader is used as a shortcut to run commands as you can do in Vim. By
# default this is disabled and you enable it by setting a leader-key. It can
# only consist of one char and I like to use comma as leader key. So to set it
# you write leader-key=,
# default=
leader-key=

# Number of milliseconds before the leader command resets. So if you tap the
# leader-key by mistake or are to slow it empties all the input after X
# milliseconds.
# default=1000
leader-timeout=1000

# You set actions for the leader-key with one or more leader-action. It consists
# of two parts first the action then the shortcut. And they're seperated by a
# comma.
#
# Available commands: home, direct, local, federated, compose, blocking,
# bookmarks, saved, favorited, boosts, favorites, following, followers, muting,
# profile, notifications, lists
#
# The shortcuts are up to you, but keep them quite short and make sure they
# don't collide. If you have one shortcut that is "f" and an other one that is
# "fav", the one with "f" will always run and "fav" will never run.
#
# Some examples:
# leader-action=local,lo
# leader-action=lists,li
# leader-action=federated,fed
# leader-action=direct,d
#


[media]
# Your image viewer.
# default=xdg-open
Expand Down Expand Up @@ -349,7 +382,6 @@ list-selected-text=xrdb:background
# look for "var KeyNames = map[Key]string{"
#
# https://github.com/gdamore/tcell/blob/master/key.go
#

# Keys for moving down
# default="",'j','J',"Down"
Expand Down
101 changes: 101 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,34 @@ type Config struct {
Input Input
}

type LeaderAction struct {
Command LeaderCommand
Shortcut string
}

type LeaderCommand uint

const (
LeaderNone LeaderCommand = iota
LeaderHome
LeaderDirect
LeaderLocal
LeaderFederated
LeaderCompose
LeaderBlocking
LeaderBookmarks
LeaderSaved
LeaderFavorited
LeaderBoosts
LeaderFavorites
LeaderFollowing
LeaderFollowers
LeaderMuting
LeaderProfile
LeaderNotifications
LeaderLists
)

type General struct {
Confirmation bool
DateTodayFormat string
Expand All @@ -60,6 +88,9 @@ type General struct {
ShowIcons bool
ShowHelp bool
RedrawUI bool
LeaderKey rune
LeaderTimeout int64
LeaderActions []LeaderAction
}

type Style struct {
Expand Down Expand Up @@ -545,6 +576,75 @@ func parseGeneral(cfg *ini.File) General {
general.ListProportion = listProp
general.ContentProportion = contentProp

leaderString := cfg.Section("general").Key("leader-key").MustString("")
leaderRunes := []rune(leaderString)
if len(leaderRunes) > 1 {
leaderRunes = []rune(strings.TrimSpace(leaderString))
}
if len(leaderRunes) > 1 {
fmt.Println("error parsing leader-key. Error: leader-key can only be one char long")
os.Exit(1)
}
if len(leaderRunes) == 1 {
general.LeaderKey = leaderRunes[0]
}
if general.LeaderKey != rune(0) {
general.LeaderTimeout = cfg.Section("general").Key("leader-timeout").MustInt64(1000)
lactions := cfg.Section("general").Key("leader-action").ValueWithShadows()
var las []LeaderAction
for _, l := range lactions {
parts := strings.Split(l, ",")
if len(parts) != 2 {
fmt.Printf("leader-action must consist of two parts seperated by a comma. Your value is: %s\n", strings.Join(parts, ","))
}
for i, p := range parts {
parts[i] = strings.TrimSpace(p)
}
la := LeaderAction{}
switch parts[0] {
case "home":
la.Command = LeaderHome
case "direct":
la.Command = LeaderDirect
case "local":
la.Command = LeaderLocal
case "federated":
la.Command = LeaderFederated
case "compose":
la.Command = LeaderCompose
case "blocking":
la.Command = LeaderBlocking
case "bookmarks":
la.Command = LeaderBookmarks
case "saved":
la.Command = LeaderSaved
case "favorited":
la.Command = LeaderFavorited
case "boosts":
la.Command = LeaderBoosts
case "favorites":
la.Command = LeaderFavorites
case "following":
la.Command = LeaderFollowing
case "followers":
la.Command = LeaderFollowers
case "muting":
la.Command = LeaderMuting
case "profile":
la.Command = LeaderProfile
case "notifications":
la.Command = LeaderNotifications
case "lists":
la.Command = LeaderLists
default:
fmt.Printf("leader-action %s is invalid\n", parts[0])
os.Exit(1)
}
la.Shortcut = parts[1]
las = append(las, la)
}
general.LeaderActions = las
}
return general
}

Expand Down Expand Up @@ -893,6 +993,7 @@ func parseInput(cfg *ini.File) Input {
func parseConfig(filepath string) (Config, error) {
cfg, err := ini.LoadSources(ini.LoadOptions{
SpaceBeforeInlineComment: true,
AllowShadows: true,
}, filepath)
conf := Config{}
if err != nil {
Expand Down
34 changes: 33 additions & 1 deletion config/default_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,39 @@ show-help=true
# default=true
redraw-ui=true
# The leader is used as a shortcut to run commands as you can do in Vim. By
# default this is disabled and you enable it by setting a leader-key. It can
# only consist of one char and I like to use comma as leader key. So to set it
# you write leader-key=,
# default=
leader-key=
# Number of milliseconds before the leader command resets. So if you tap the
# leader-key by mistake or are to slow it empties all the input after X
# milliseconds.
# default=1000
leader-timeout=1000
# You set actions for the leader-key with one or more leader-action. It consists
# of two parts first the action then the shortcut. And they're seperated by a
# comma.
#
# Available commands: home, direct, local, federated, compose, blocking,
# bookmarks, saved, favorited, boosts, favorites, following, followers, muting,
# profile, notifications, lists
#
# The shortcuts are up to you, but keep them quite short and make sure they
# don't collide. If you have one shortcut that is "f" and an other one that is
# "fav", the one with "f" will always run and "fav" will never run.
#
# Some examples:
# leader-action=local,lo
# leader-action=lists,li
# leader-action=federated,fed
# leader-action=direct,d
#
[media]
# Your image viewer.
# default=xdg-open
Expand Down Expand Up @@ -351,7 +384,6 @@ list-selected-text=xrdb:background
# look for "var KeyNames = map[Key]string{"
#
# https://github.com/gdamore/tcell/blob/master/key.go
#
# Keys for moving down
# default="",'j','J',"Down"
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/rivo/tview"
)

const version = "1.0.4"
const version = "1.0.5"

func main() {
util.MakeDirs()
Expand Down
Loading

0 comments on commit 80ede1e

Please sign in to comment.