Make the output of `transmission-remote -l` useable and readable. Written in V.

Tested with transmission-daemon 4.0.6




  

import arrays
import os
import rand
import regex
import strconv

println(" ")
mut listing := os.execute('transmission-remote -l')

mut list_arr := []string{len: 36, cap: 300, init: " -- "}
mut re := regex.regex_opt("\n")!
list_arr = re.split(listing.output)

//      delete from `list_arr` (1st=heading; 2nd last=`sum`; last=empty string):
list_arr.delete(0)
list_arr.delete(list_arr.len - 1)
list_arr.delete(list_arr.len - 1)

mut date_sanit := ""
mut date_array := []i64{len: 300}
mut avail_array := []i64{len: 300}

for key, value in list_arr {
    mut query := r'^\s+\d+'
    mut rexp := regex.regex_opt(query)!
    mut res := rexp.find_all_str(value)
    query = r'\s+'
    rexp = regex.regex_opt(query)!
    mut nummber := rexp.replace(res[0], "")

    mut remote_string := "transmission-remote -t ${nummber} -i "
    mut torr_info := os.execute('${remote_string}')

    mut torr_info_arr := []string{len: 0, cap: 3000, init: ""}
    mut re2 := regex.regex_opt("\n")!
    torr_info_arr = re2.split(torr_info.output)
    mut date_added := torr_info_arr[23]

    if date_added.contains("added:") { } else {    
        //      add placeholder_date until torr starts downloading:
        random_date := rand.i64_in_range(20490000000000, 20499999999999)!
        date_array[key] = random_date
    }
    
    query_mantissa := r'\.\d'
    mut re_mantissa := regex.regex_opt(query_mantissa)!
    mut avail_sanit := re_mantissa.replace_simple(torr_info_arr[15], "")
    query_digits := r'([^0-9])'
    mut re_digits := regex.regex_opt(query_digits)!
    avail_sanit = re_digits.replace_simple(avail_sanit, "")
    //      avoid crash on empty string:
    if avail_sanit == "" {  
        avail_sanit = "00"
    }
    avail_array[key] = strconv.atoi64(avail_sanit)!

    for i in 1 .. 13 {
        month_array := ["Zero", "Jan", "Feb", "Mar", "Apr", "May", "Jun", 
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
        query = month_array[i]       
        rexp = regex.regex_opt(query)!
        mut stringified := i.str()         
        stringified = strconv.format_str(stringified, 
        strconv.BF_param{pad_ch:`0`, len0:2, positive:true})
        if date_added.contains(query) {
            date_sanit = rexp.replace_simple(date_added, stringified)
            mut date_sanit2 := re_digits.replace_simple(date_sanit, "")
            mut aa := date_sanit2.runes()
            aa.insert(0, [aa[10], aa[11], aa[12], aa[13]])
            aa.trim(14) 
            ab := aa.string()
            date_int := strconv.atoi64(ab)!
            date_array[key] = date_int
        }  
    }
}

//      truncate date_array to remove elements with no date
date_array = unsafe { date_array[0..list_arr.len] }
mut torr_result := ""

//      print header:
println(" ID |HAVE AVAIL|   UP DOWN   ETA|   TITLE")

for _ in date_array {
    oldest := arrays.idx_min(date_array)!    
    torr_result = list_arr[oldest]
    //      after match, rewrite date field so it no longer gets detected as oldest date:
    date_array[oldest] = 99999999999998
    //      sanitise result:
    mut query := r'\s+'
    mut rexp := regex.regex_opt(query)!
    torr_result = rexp.replace(torr_result, " ")
    mut torr_result_arr := []string{len: 12, cap: 100, init: ""}
    re = regex.regex_opt(" ")!
    torr_result_arr = re.split(torr_result)
    torr_result_arr.delete(0)
    //      fix alignment of elements in array when nothing has been downloaded:
    //      (also to stop `out of bounds access` error with just-added torr., 
    //      by increasing length of short array)
    if torr_result_arr[2] == "None" {
        torr_result_arr.insert(3, "None")
    }

    //      merge elements when status/state is `Up & Down`
    if torr_result_arr.len > 10 {
        if torr_result_arr[9].contains("Up") && torr_result_arr[10].contains("&") 
        && torr_result_arr[11].contains("Down") {
            torr_result_arr[9] = "UD"
            torr_result_arr.delete(11)
            torr_result_arr.delete(10)
        }
    }

    mut query_digit := r'\d+'
    mut rexp_digit := regex.regex_opt(query_digit)!
    if rexp_digit.matches_string(torr_result_arr[4]) {
        torr_result_arr[4] = torr_result_arr[4]+torr_result_arr[5]
        for j:=6; j < torr_result_arr.len; j++ {
            torr_result_arr[j-1] = torr_result_arr[j]
        }
    } 

    if torr_result_arr.len > 10 {
        for j:=10; j < torr_result_arr.len; j++ {
            torr_result_arr[9] = torr_result_arr[9] + "_" + torr_result_arr[j]
        }
    }

    //      output results:
    //      ID (and get rid of `*` that randomly gets put next to some IDs):
    mut stringified := torr_result_arr[0]
    query = r'([^0-9])'
    rexp = regex.regex_opt(query)!
    stringified = rexp.replace(stringified, "")
    stringified = strconv.format_str(stringified,
    strconv.BF_param{pad_ch:` `, len0:3, positive:true})
    print("${stringified} |")

    //      have %:
    stringified = strconv.format_str(torr_result_arr[1],
    strconv.BF_param{pad_ch:` `, len0:4, positive:true})
    print("${stringified} ")

    //      availability %:
    avail_int64 := avail_array[oldest]
    avail_string := avail_int64.str()
    stringified = strconv.format_str(avail_string,
    strconv.BF_param{pad_ch:` `, len0:4, positive:true})
    print("${stringified}%|")

    //      uploading rate:
    stringified = strconv.format_str(torr_result_arr[5],
    strconv.BF_param{pad_ch:` `, len0:7, positive:true})
    //      get rid of decimal point and trailing digits
    query = r'\.\d+'
    rexp = regex.regex_opt(query)!
    stringified = rexp.replace(stringified, "")
    print("${stringified}")

    //      downloading rate:
    stringified = strconv.format_str(torr_result_arr[6],
    strconv.BF_param{pad_ch:` `, len0:7, positive:true, rm_tail_zero:true})
    //      get rid of decimal point and trailing digits
    query = r'\.\d+'
    rexp = regex.regex_opt(query)!
    stringified = rexp.replace(stringified, "")
    print("${stringified}")

    //      eta:
    stringified = strconv.format_str(torr_result_arr[4],
    strconv.BF_param{pad_ch:` `, len0:6, positive:true})
    if stringified == "Unknown" { stringified = "   ?? " }
    print("${stringified}|")

    //      status/state:
    stringified = torr_result_arr[8]
    //      shortening `status` element:
    if stringified == "Up" { stringified = "U" }
    if stringified == "Seeding" { stringified = "U" }
    if stringified == "Downloading" { stringified = "D" }
    if stringified == "Idle" { stringified = "I" }
    if stringified == "Stopped" { stringified = "P" }
    stringified = strconv.format_str(stringified,
    strconv.BF_param{pad_ch:` `, len0:3, align:.left, positive:true})
    print("${stringified}")

    mut k := []string{len: 12, cap: 1000, init: ""}
    k = torr_result_arr[9].split("")
    //      title (truncate so output fits on one line):
    mut x := 0
    if k.len > 69 { x = 69 } else { x = k.len }
    for i in 0..x {
       print(k[i])
    }
    println(" ")
}