Skip to content

Commit

Permalink
Add replace limit for sed
Browse files Browse the repository at this point in the history
  • Loading branch information
godwhoa committed Nov 2, 2018
1 parent 412a81d commit f6b8d1a
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions plugins/sed/sed.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sed

import (
"strconv"
"strings"

"github.com/godwhoa/oodle/oodle"
Expand All @@ -17,8 +18,8 @@ func SedHelp() oodle.Command {
return oodle.Command{
Prefix: ".",
Name: "sed",
Description: "Simple sed-like find and replace. Since it is simple you can't replace '/'",
Usage: "s/{find}/{replace}/ ending '/' is optional",
Description: "Simple sed-like find and replace. Since it is simple you can't replace '/' or do escaping",
Usage: "s/{find}/{replace}/{replace limit} replace limit defaults to all",
Fn: func(nick string, args []string) (reply string, err error) {
return ".sed is not an actual command. Do s/{find}/{replace} to use it. This exists since murii might do .help sed", nil
},
Expand All @@ -40,13 +41,20 @@ func Sed(sender oodle.Sender) oodle.Trigger {
if len(args) < 3 {
return
}
rlimit := -1
if len(args) > 3 {
i, err := strconv.Atoi(args[3])
if err == nil {
rlimit = i
}
}
// apply find n replace on user's last msg.
lastmsg, ok := usermsgs[nick]
if !ok {
sender.Sendf("Your last message was not found.")
return
}
newmsg := strings.Replace(lastmsg, args[1], args[2], -1)
newmsg := strings.Replace(lastmsg, args[1], args[2], rlimit)
sender.Sendf("<%s> %s", nick, newmsg)
} else {
// store it as user's last msg.
Expand Down

0 comments on commit f6b8d1a

Please sign in to comment.