3

There is an addon in fcitx where you can associate a word with some other string like "smile (・∀・)" entered in at ~/.config/fcitx/data/QuickPhrase.mb. When you type smile it outputs (・∀・)`

I want the same thing but a function rather than a word; I want it to output a current timestamp.

For example: when I input "time", it outputs the current time "2016-08-03 11:15",one minute later ,when I input "time", it outputs "2016-08-03 11:16"

dindom
  • 33

2 Answers2

5

In this post:

  1. Introduction
  2. QuickPhrase_Time.py script
  3. xclip shortcut

1.Introduction

The plugin to which OP refers to, is QuickPhrase and can be installed via sudo apt-get install fcitx-modules fcitx-module-quickphrase-editor. It uses ~/.config/fcitx/data/QuickPhrase.mb to store phrases.

The main objective here is to have easy way of inserting a string which contains current time into text filed that user is currently editing. Below there are two solutions.


2. QuickPhrase_Time.py script

This script continuously edits the line in config file which has time_now phrase , and appends current time to the line. This script is meant to be launched automatically on login to GUI.

Usage is simple:

 python /path/to/QuickPhrase_Timer.py

Script source

Also available as Github Gist , updated versions likely will go there.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import subprocess
import time
import os


def run_cmd(cmdlist):
    """ Reusable function for running external commands """
    new_env = dict( os.environ ) 
    new_env['LC_ALL'] = 'C' 
    try:
        stdout = subprocess.check_output(cmdlist,env=new_env)
    except subprocess.CalledProcessError:
        pass
    else:
        if stdout:
            return stdout

if __name__ == '__main__':
    user_home = os.path.expanduser('~')
    config_file = '.config/fcitx/data/QuickPhrase.mb'
    config_full_path = os.path.join(user_home,config_file)
    found = None

    while True:
         lines = []
         time.sleep(1)
         with open(config_full_path) as f:
              for line in f:
                  lines.append(line.strip())
                  if line.startswith('time_now'):
                      found = True
         # print(lines)
         if found:
             with open(config_full_path,'w') as f:
                  for line in lines:
                      if line.startswith('time_now'):
                         time_now = run_cmd(['date', 
                                             '+%Y-%m-%d %I:%M'
                                             ]).decode().strip()
                         line = 'time_now ' + time_now + '\n'
                      else:
                         line = line + '\n'
                      f.write(line)

3. xclip shortcut

In case the above python script won't work for you, here's a workaround: bind the command below to a keyboard shortcut

xclip -sel clip <<< $( date +%Y-%m-%d\ %I:%M )

Essentially, what this does is copies output of date to your clipboard, which you can then release via Ctrl+V shortcut ( which is common for most applications as paste shortcut).

This approach doesn't rely on fctix or any other input method, hence is more flexible and reliable.

Note that xclip is not installed by default. Obtain it via sudo apt-get install xclip

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • Thinks @Serg ! "QuickPhrase_Timer.py" is a good point, but not a good solution,because it's keep running all the time. – dindom Aug 23 '16 at 10:36
  • @dindom it keeps running all the time but the CPU usage is very minimal, if that's what you're concerned about. – Sergiy Kolodyazhnyy Aug 23 '16 at 10:40
  • 2
    @dindom I doubt there is a method where you do not have something running all the time. It is a low footprint that matters. – Rinzwind Aug 23 '16 at 10:46
0

You may use lua to extend quickphrase for fcitx/fcitx5.

The lua relevant API is based on old Google Pinyin lua on Windows. You may refer to this doc to see what extra API are available: https://fcitx.github.io/fcitx5-lua/modules/ime.html (only ime module works for fcitx4, the fcitx module is only in fcitx5.)

fcitx4

In fcitx4 lua is a built-in addon. Your distribution may split it in to a sperate package. On ubuntu it's called fcitx-module-lua.

You may create a .lua file under ~/.config/fcitx/lua .

fcitx5

Basically the API is the same, but the path is slightly different. And now it's a separate package, fcitx5-lua. On ubuntu it's called fcitx5-module-lua.

You may put the .lua file under ~/.local/share/fcitx5/lua/imeapi/extensions instead.

Here is a script that works for both fcitx/fcitx5, taken from https://github.com/lilydjwg/fcitx-lua-scripts/blob/master/date.lua

function LookupDate(input)
  local fmt
  if input == '' then
    fmt = "%Y年%m月%d日"
  elseif input == 't' then
    fmt = "%Y-%m-%d %H:%M:%S"
  elseif input == 'd' then
    fmt = "%Y-%m-%d"
  end
  return os.date(fmt)
end

ime.register_command("dt", "LookupDate", "日期时间输入")

Fcitx5 also comes with a built-in lua script https://github.com/fcitx/fcitx5-chinese-addons/blob/master/im/pinyin/pinyin.lua , but that checks the input method name is Pinyin or Shuangpin.

You may also take it as a reference since it contains more than just date/time and support more fancy format.

csslayer
  • 156