변경

2013-09-26 10 views
2

나는 끔찍한 그놈의 API 문서를 진압하고이 확장 함께했다 봤는데 그놈 쉘 확장 쿼리를 만들려면 :변경

const St = imports.gi.St; 
const Main = imports.ui.main; 
const Tweener = imports.ui.tweener; 
const GLib = imports.gi.GLib; 

let label; 

function init() { 
    label = new St.Bin({ style_class: 'panel-label' }); 

    let stuff = GLib.spawn_command_line_sync("cat /home/user/temp/hello")[1].toString(); 
    let text = new St.Label({ text: stuff }); 

    label.set_child(text); 
} 

function enable() { 
    Main.panel._rightBox.insert_child_at_index(label, 0); 
} 

function disable() { 
    Main.panel._rightBox.remove_child(label); 
} 

이것은 hello 파일에 어떤 읽고에 표시한다을 상단 패널. 그러나 hello 파일의 내용을 변경하면 그 새로운 내용이 표시되도록 Gnome을 다시 시작해야합니다. 이제 동적으로이 작업을 수행하는 방법이 있지만 문서에서 아무 것도 찾을 수 없습니다. 패널의 메시지는 기본적으로 항상 파일에있는 내용을 미러링해야합니다. 어떤 아이디어가 이것을 어떻게?

답변

4
당신은 당신의 hello 파일의 Gio.File 핸들을 가져올 것이다

다음 monitor을 :

let helloFile = Gio.File.new_for_path('/home/user/temp/hello'); 
let monitor = helloFile.monitor(Gio.FileMonitorFlags.NONE, null); 
monitor.connect('changed', function (file, otherFile, eventType) { 
    // change your UI here 
}); 
+0

이 위대하다! 이 물건이 어딘가에 분명히 문서화되어 있었으면 좋겠다. (한 가지 더 질문 : 명령의 출력을 어떻게 모니터링 할 것인가? 핑을 실행하고 상단 패널의 각 패킷 시간을 표시한다고 가정 해 보겠습니다. 도움을 주셔서 감사합니다. – mart1n

+0

그게 전부입니다. 이 질문에 대한 답을 얻을 수는 있지만이 C 코드를 사용하면 시작할 수 있습니다. https://github.com/ptomato/gnome-inform7/blob/master/src/spawn.c#L215 C 코드 , 불행히도 올바른 GJS 문서 대신 C API를 읽는 것이 가장 좋습니다. https://developer.gnome.org/gio/stable/GFileMonitor.html#GFileMonitor-changed – ptomato

0

이 나를 위해 일했다. 30 초마다 레이블 값을 새로 고칩니다.

  • 는 다음 가져 오기

    const를 주회 돌이 = imports.mainloop 추가; 당신의 init 메소드에서

  • Mainloop.timeout_add(30000, function() { 
    let stuff = GLib.spawn_command_line_sync("your_command")[1].toString(); 
    let label = new St.Label({ text: stuff }); 
    button.set_child(label);return true});