[import {unit-common.th1ish}]
const App = object {
    f: [Fossil.createContext],
    opt:object{
        limit: (ARGV.flags.limit || ARGV.flags.n) || 5        
    }
}

if(!App.f.db || !App.f.db.repo){
    throw "No repository opened. Use -R=... to open a repo DB."
}

var sql = [api.Buffer 1024]
[sql.append {
    SELECT
    --  NULL AS sortId,
    -- blob.rid AS rid,
      substr(uuid,1,12) AS uuid,
    -- CAST(strftime('%s',event.mtime) AS INTEGER) AS mtime,
      datetime(event.mtime,'localtime') AS timestampString,
      coalesce(euser, user) AS user,
    -- blob.rid IN leaf AS isLeaf,
    -- bgcolor AS bgColor,
      event.type AS eventType,
      (SELECT group_concat(substr(tagname,5), ',') FROM tag, tagxref
        WHERE tagname GLOB 'sym-*' AND tag.tagid=tagxref.tagid
          AND tagxref.rid=blob.rid AND tagxref.tagtype>0) as tags,
    --  tagid as tagId,
      coalesce(ecomment, comment) AS comment
    --  brief as brief
     FROM event JOIN blob 
    WHERE blob.rid=event.objid
}]
//[sql.append bufTL] // bug (th1ish): appending wrongly when arg is-a Buffer

[sql.append ' ORDER BY mtime DESC']
if('integer' === typename App.opt.limit){
    [sql.append ' LIMIT ' App.opt.limit]
}

[print 'The ' App.opt.limit ' most recent timeline event(s) for '
    App.f.db.repo.filename ':']
[App.f.db.each object{
    sql:sql,
    mode:1,
    callback:proc(){
        //(1===rowNumber) && [print [columnNames.join '\t']]
        if(0){ [print [this.join '\t']] }
        else{
           const li = array[this.3, " @ ", this.1, " [",
                        this.0,"] by [",this.2, "]"];
            this.4 && [li.push " in branch [" this.4 "]"]
            [li.push '\n\t' this.5]
            [print [li.join '']]

        }
    }
}]
