Keep keyboard focus in input

This commit is contained in:
Neale Pickett 2014-10-25 04:21:53 +00:00
parent 2a0c9231d7
commit 03dd0a9b81
4 changed files with 57 additions and 20 deletions

View File

@ -14,7 +14,7 @@
<input type="hidden" name="auth" value="" id="authtok"> <input type="hidden" name="auth" value="" id="authtok">
<input type="hidden" name="type" value="command"> <input type="hidden" name="type" value="command">
<input type="hidden" name="target" id="target"> <input type="hidden" name="target" id="target">
<input name="text" autofocus> <input name="text" id="text" autofocus>
<input type="Submit" value="Send"> <input type="Submit" value="Send">
</form> </form>
<div id="kiboze"></div> <div id="kiboze"></div>

View File

@ -29,10 +29,20 @@ func ReadString(fn string) string {
return strings.TrimSpace(string(octets)) return strings.TrimSpace(string(octets))
} }
func tail(w http.ResponseWriter, pos int64) { func tail(w http.ResponseWriter, filename string, pos int64) {
logfn := path.Join(ServerDir, "log") var err error
f, err := os.Open(logfn) currentfn := path.Join(ServerDir, "current")
if filename == "" {
filename, err = os.Readlink(currentfn)
if err != nil {
log.Fatal(err)
}
}
filepath := path.Join(ServerDir, filename)
f, err := os.Open(filepath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -43,7 +53,7 @@ func tail(w http.ResponseWriter, pos int64) {
log.Fatal(err) log.Fatal(err)
} }
defer watcher.Close() defer watcher.Close()
watcher.Add(logfn) watcher.Add(filepath)
for { for {
printid := false printid := false
@ -52,22 +62,33 @@ func tail(w http.ResponseWriter, pos int64) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if newpos < pos { if newpos != pos {
// File has been truncated! log.Fatal("Lost my position in the log, somehow (log truncated?)")
pos = 0
f.Seek(0, 0)
} }
bf := bufio.NewScanner(f) bf := bufio.NewScanner(f)
for bf.Scan() { for bf.Scan() {
t := bf.Text() t := bf.Text()
pos += int64(len(t)) + 1 // XXX: this breaks if we ever see \r\n pos += int64(len(t)) + 1 // XXX: this breaks if we ever see \r\n
parts := strings.Split(t, " ")
if (len(parts) >= 4) && (parts[3] == "NEXTLOG") {
watcher.Remove(filepath)
filename = parts[4]
filepath = path.Join(ServerDir, filename)
f.Close()
f, err = os.Open(filename)
if err != nil {
log.Fatal(err)
}
watcher.Add(filename)
}
fmt.Fprintf(w, "data: %s\n", t) fmt.Fprintf(w, "data: %s\n", t)
printid = true printid = true
} }
if printid { if printid {
_, err = fmt.Fprintf(w, "id: %d\n\n", pos) _, err = fmt.Fprintf(w, "id: %s/%d\n\n", filename, pos)
} }
if err != nil { if err != nil {
break break
@ -127,8 +148,14 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
handleCommand(w, r.Form.Get("text"), r.FormValue("target")) handleCommand(w, r.Form.Get("text"), r.FormValue("target"))
default: default:
w.Header().Set("Content-Type", "text/event-stream") w.Header().Set("Content-Type", "text/event-stream")
id, _ := strconv.ParseInt(os.Getenv("HTTP_LAST_EVENT_ID"), 0, 64) parts := strings.Split(os.Getenv("HTTP_LAST_EVENT_ID"), "/")
tail(w, id) if len(parts) == 2 {
filename := path.Base(parts[0])
pos, _ := strconv.ParseInt(parts[1], 0, 64)
tail(w, filename, pos)
} else {
tail(w, "", 0)
}
} }
} }

View File

@ -6,6 +6,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"log" "log"
"logfile"
"net" "net"
"os" "os"
"strings" "strings"
@ -24,6 +25,7 @@ type Message struct {
var running bool = true var running bool = true
var nick string var nick string
var gecos string var gecos string
var maxlogsize uint
var logq chan Message var logq chan Message
func isChannel(s string) bool { func isChannel(s string) bool {
@ -45,13 +47,11 @@ func (m Message) String() string {
} }
func logLoop() { func logLoop() {
logf, err := os.OpenFile("log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) logf := logfile.NewLogfile(int(maxlogsize))
if err != nil {
log.Fatal(err)
}
defer logf.Close() defer logf.Close()
for m := range logq { for m := range logq {
fmt.Fprintf(logf, "%d %s\n", time.Now().Unix(), m.String()) logf.Log(m.String())
} }
} }
@ -220,6 +220,7 @@ func usage() {
func main() { func main() {
dotls := flag.Bool("notls", true, "Disable TLS security") dotls := flag.Bool("notls", true, "Disable TLS security")
outqdir := flag.String("outq", "outq", "Output queue directory") outqdir := flag.String("outq", "outq", "Output queue directory")
flag.UintVar(&maxlogsize, "logsize", 8000, "Log entries before rotating")
flag.StringVar(&gecos, "gecos", "Bob The Merry Slug", "Gecos entry (full name)") flag.StringVar(&gecos, "gecos", "Bob The Merry Slug", "Gecos entry (full name)")
flag.Parse() flag.Parse()

11
wirc.js
View File

@ -127,9 +127,15 @@ function addMessage(txt) {
var args = parts.slice(5); var args = parts.slice(5);
var msg = txt.substr(lhs.length + 2) var msg = txt.substr(lhs.length + 2)
var forumElement = getForumElement(forum); var forumElement;
var p = document.createElement("p"); var p = document.createElement("p");
if (command == "NICK") {
forumElement = getForumElement(".");
} else {
forumElement = getForumElement(forum);
}
addMessagePart(p, "timestamp", ts.toLocaleTimeString()); addMessagePart(p, "timestamp", ts.toLocaleTimeString());
switch (command) { switch (command) {
@ -188,6 +194,9 @@ function init() {
source.onmessage = newmsg; source.onmessage = newmsg;
document.getElementById("command").onsubmit = handleCommand; document.getElementById("command").onsubmit = handleCommand;
var txte = document.getElementById("text");
txte.addEventListener("blur", txte.focus);
} }
window.onload = init; window.onload = init;